使用VBScript复制和粘贴文件
我正在使用VBScript查看文件夹并复制子文件夹中的所有excel文件。在我遇到一个没有excel文件的子文件夹之前,代码工作得很好。如何让代码跳过不包含excel文件的任何子文件夹?谢谢 代码如下:使用VBScript复制和粘贴文件,vbscript,Vbscript,我正在使用VBScript查看文件夹并复制子文件夹中的所有excel文件。在我遇到一个没有excel文件的子文件夹之前,代码工作得很好。如何让代码跳过不包含excel文件的任何子文件夹?谢谢 代码如下: Set FSO = CreateObject("Scripting.FileSystemObject") ShowSubfolders FSO.GetFolder("C:\Users\jonathan\Documents\Prints Tester"), 3 Const Destination
Set FSO = CreateObject("Scripting.FileSystemObject")
ShowSubfolders FSO.GetFolder("C:\Users\jonathan\Documents\Prints Tester"), 3
Const DestinationFile = "C:\Users\jonathan\Documents\TestEnd\*.xls"
'Script that goes into the subfolder to find the files for copying
Sub ShowSubFolders(Folder, Depth)
If Depth > 0 then
For Each Subfolder in Folder.SubFolders
'Wscript.Echo Subfolder.Path
Dim FolderPath
FolderPath = Subfolder.Path
Dim SourceFile
SourceFile = FolderPath & "\*.xls"
Set fso = CreateObject("Scripting.FileSystemObject")
'Check to see if the file already exists in the destination folder
If fso.FileExists(DestinationFile) Then
'Check to see if the file is read-only
If Not fso.GetFile(DestinationFile).Attributes And 1 Then
'The file exists and is not read-only. Safe to replace the file.
fso.CopyFile SourceFile, "C:\Users\jonathan\Documents\TestEnd\", True
Else
'The file exists and is read-only.
'Remove the read-only attribute
fso.GetFile(DestinationFile).Attributes = fso.GetFile(DestinationFile).Attributes - 1
'Replace the file
fso.CopyFile SourceFile, "C:\Users\jonathan\Documents\TestEnd\", True
'Reapply the read-only attribute
fso.GetFile(DestinationFile).Attributes = fso.GetFile(DestinationFile).Attributes + 1
End If
Else
'The file does not exist in the destination folder. Safe to copy file to this folder.
fso.CopyFile SourceFile, "C:\Users\jonathan\Documents\TestEnd\", True
End If
Set fso = Nothing
ShowSubFolders Subfolder, Depth -1
Next
End if
End Sub
为了解决这个问题,我阅读了@Dave在本文中提到的建议: 我所需要的只是一个
On Error Resume Next
,让代码在出错后继续运行。这是完成的工作代码,它将跳过没有excel文件的文件夹
Set FSO = CreateObject("Scripting.FileSystemObject")
ShowSubfolders FSO.GetFolder("C:\Users\jonathan\Documents\Prints Tester"), 3
Const DestinationFile = "C:\Users\jonathan\Documents\TestEnd\*.xls"
'Script that goes into the subfolder to find the files for copying
Sub ShowSubFolders(Folder, Depth)
If Depth > 0 then
For Each Subfolder in Folder.SubFolders
'Wscript.Echo Subfolder.Path
Dim FolderPath
FolderPath = Subfolder.Path
Dim SourceFile
SourceFile = FolderPath & "\*.xls"
Set fso = CreateObject("Scripting.FileSystemObject")
'Check to see if the file already exists in the destination folder
If fso.FileExists(DestinationFile) Then
'Check to see if the file is read-only
If Not fso.GetFile(DestinationFile).Attributes And 1 Then
'The file exists and is not read-only. Safe to replace the file.
fso.CopyFile SourceFile, "C:\Users\jonathan\Documents\TestEnd\", True
Else
'The file exists and is read-only.
'Remove the read-only attribute
fso.GetFile(DestinationFile).Attributes = fso.GetFile(DestinationFile).Attributes - 1
'Replace the file
fso.CopyFile SourceFile, "C:\Users\jonathan\Documents\TestEnd\", True
'Reapply the read-only attribute
fso.GetFile(DestinationFile).Attributes = fso.GetFile(DestinationFile).Attributes + 1
End If
Else
'The file does not exist in the destination folder. Safe to copy file to this folder.
On Error Resume Next
fso.CopyFile SourceFile, "C:\Users\jonathan\Documents\TestEnd\", True
End If
Set fso = Nothing
ShowSubFolders Subfolder, Depth -1
Next
End if
End Sub
值得一读这里@戴夫,谢谢你给我指了指那个帖子。我所要做的就是让代码跳过那些没有excel文件的文件夹,然后添加一个“下一步出错时继续”