Microsoft KB Archive/251339: Difference between revisions
(importing KB archive) |
m (Text replacement - """ to """) |
||
Line 75: | Line 75: | ||
<li>On the '''Project''' menu, click '''References''' to display the '''References''' dialog box. Select '''Microsoft Scripting Runtime''' and then click '''OK'''.</li> | <li>On the '''Project''' menu, click '''References''' to display the '''References''' dialog box. Select '''Microsoft Scripting Runtime''' and then click '''OK'''.</li> | ||
<li>Add two CommandButton controls to Form1.</li> | <li>Add two CommandButton controls to Form1.</li> | ||
<li><p>Add the following code to the '''General Declarations''' section of Form1, replacing the references to | <li><p>Add the following code to the '''General Declarations''' section of Form1, replacing the references to "MyMachine" with the name of your computer:</p> | ||
<pre class="codesample">Option Explicit | <pre class="codesample">Option Explicit | ||
Line 84: | Line 84: | ||
' The following statement creates C:\NewFolder and copies the contents | ' The following statement creates C:\NewFolder and copies the contents | ||
' (files and subfolders) of c:\MyFolder into it. | ' (files and subfolders) of c:\MyFolder into it. | ||
fs.CopyFolder | fs.CopyFolder "C:\myFolder", "C:\NewFolder" | ||
End Sub | End Sub | ||
Line 90: | Line 90: | ||
' Comment the following line to avoid Error 5: Invalid Call or Procedure | ' Comment the following line to avoid Error 5: Invalid Call or Procedure | ||
fs.CopyFolder | fs.CopyFolder "\\myMachine\myFolder", "C:\myNewFolder" | ||
' The following line copies the subfolders of myFolder into myNewFolder | ' The following line copies the subfolders of myFolder into myNewFolder | ||
fs.CopyFolder | fs.CopyFolder "\\myMachine\myFolder\*", "C:\myNewFolder" | ||
' The following line copies the files from myFolder into myNewFolder | ' The following line copies the files from myFolder into myNewFolder | ||
fs.CopyFile | fs.CopyFile "\\myMachine\myFolder\*", "C:\myNewFolder" | ||
End Sub | End Sub | ||
Private Sub Form_Load() | Private Sub Form_Load() | ||
Set fs = New FileSystemObject | Set fs = New FileSystemObject | ||
Command1.Caption = | Command1.Caption = "Without Wildcard" | ||
Command2.Caption = | Command2.Caption = "Wildcard" | ||
End Sub | End Sub | ||
</pre></li> | </pre></li> |
Latest revision as of 13:51, 21 July 2020
Article ID: 251339
Article Last Modified on 5/13/2003
APPLIES TO
- Microsoft Visual Basic 6.0 Learning Edition
- Microsoft Visual Basic 6.0 Professional Edition
- Microsoft Visual Basic 6.0 Enterprise Edition
This article was previously published under Q251339
SYMPTOMS
When attempting to use the CopyFolder method of the FileSytemObject, you get the following error message:
CAUSE
You specified a Universal Naming Convention (UNC) path for the source but did not include the wildcard (*) character.
MORE INFORMATION
When the CopyFolder method of the FileSystemObject is used with a UNC path, it requires the use of a wildcard character in the source specification. However, when the CopyFolder method is used with a wildcard, it only copies the subfolders of the last named folder in the path and does not copy any files from that folder. Therefore, if you want to copy the entire contents, you must use the CopyFile method in addition to the CopyFolder method when using a UNC path and the wildcard specification for the source.
The mapped path usage of CopyFolder can be specified with or without wildcard characters in the source path. If it is used without the wildcard for mapped drive paths, it copies the entire contents of the folder, including the files and subfolders.
Steps to Reproduce Behavior
- Start Windows Explorer and create two folders, C:\MyFolder and C:\MyNewFolder. Add some files and subfolders to C:\MyFolder.
- Start a new Standard EXE project in Visual Basic. Form1 is created by default.
- On the Project menu, click References to display the References dialog box. Select Microsoft Scripting Runtime and then click OK.
- Add two CommandButton controls to Form1.
Add the following code to the General Declarations section of Form1, replacing the references to "MyMachine" with the name of your computer:
Option Explicit Dim fs As FileSystemObject Private Sub Command1_Click() ' Without Wildcard ' The following statement creates C:\NewFolder and copies the contents ' (files and subfolders) of c:\MyFolder into it. fs.CopyFolder "C:\myFolder", "C:\NewFolder" End Sub Private Sub Command2_Click() ' Wildcard ' Comment the following line to avoid Error 5: Invalid Call or Procedure fs.CopyFolder "\\myMachine\myFolder", "C:\myNewFolder" ' The following line copies the subfolders of myFolder into myNewFolder fs.CopyFolder "\\myMachine\myFolder\*", "C:\myNewFolder" ' The following line copies the files from myFolder into myNewFolder fs.CopyFile "\\myMachine\myFolder\*", "C:\myNewFolder" End Sub Private Sub Form_Load() Set fs = New FileSystemObject Command1.Caption = "Without Wildcard" Command2.Caption = "Wildcard" End Sub
- Run the program. Click the Wildcard button to reproduce the error.
- Edit Command2_Click, commenting out the line indicated in the code. Repeat the previous step and the directories and files are copied properly.
NOTE: If you step through the code, you can see that CopyFolder only copies the folders and that CopyFile is necessary to copy the files. - If you reset the test by deleting the contents of C:\NewFolder, you can repeat the test, only clicking on the Without Wildcard button to see that this syntax also copies the files and subfolders.
REFERENCES
MSDN Help Index : CopyFolder method
Keywords: kbprb KB251339