交叉检查两个文件夹中的文件
Cross check files in two folders
我有两个文件夹:主文件夹和新文件夹。两个文件夹中的文件共享相同的文件名。我需要交叉检查这两个文件夹之间的文件。
- 如果Master文件夹没有文件,我从New文件夹导入。
- 如果主文件夹确实有文件,我会在新文件中附加主文件和数据。
使用下面的代码,我可以附加数据。但是它无法导入文件。该脚本没有显示任何错误消息。我只是不知道出了什么问题。
On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Set SrcFolder = fso.GetFolder("C:\Users\Vault Keeper\Desktop\NewFile")
DestFolder = "C:\Users\Vault Keeper\Desktop\MasterFile"
For Each NewFile in SrcFolder.files
If fso.FileExists(DestFolder & "\" & NewFile.name) Then
txt = NewFile.OpenAsTextStream(1).ReadAll
Set MasterFile = FSO.OpenTextFile(DestFolder & "\" & NewFile.name, 8)
MasterFile.Write txt
Else
fso.CopyFile "C:\Users\Vault Keeper\Desktop\NewFile" & "\" & NewFile.name, _
DestFolder
End If
Next
您可以使用这个检查文件
Dim objFSO As Scripting.FileSystemObject
If objFSO Is Nothing Then Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(fileNameAndPath) Then
'code here...
End If
当您让脚本闭嘴不管出了什么问题时,您并不知道出了什么问题,这并不奇怪 (On Error Resume Next
)。删除该行(无论如何使用全局 OERN 都是一种糟糕的做法)并查看您得到了什么错误。
错误很可能是 "permission denied",因为您正在尝试将文件复制到现有目标文件夹,而该路径没有以反斜杠结尾:
DestFolder = "C:\Users\Vault Keeper\Desktop\MasterFile" '<-- no trailing backslash
...
fso.CopyFile "C:\Users\Vault Keeper\Desktop\NewFile" & "\" & NewFile.name, DestFolder
If source contains wildcard characters or destination ends with a path separator (), it is assumed that destination is an existing folder in which to copy matching files. Otherwise, destination is assumed to be the name of a file to create. In either case, three things can happen when an individual file is copied.
- If destination does not exist, source gets copied. This is the usual case.
- If destination is an existing file, an error occurs if overwrite is false. Otherwise, an attempt is made to copy source over the existing file.
- If destination is a directory, an error occurs.
将反斜杠附加到目标路径,错误应该消失。另外,由于您已经有了 File
object, I'd recommend to use its Copy
方法而不是 CopyFile
:
NewFile.Copy DestFolder & "\"
我有两个文件夹:主文件夹和新文件夹。两个文件夹中的文件共享相同的文件名。我需要交叉检查这两个文件夹之间的文件。
- 如果Master文件夹没有文件,我从New文件夹导入。
- 如果主文件夹确实有文件,我会在新文件中附加主文件和数据。
使用下面的代码,我可以附加数据。但是它无法导入文件。该脚本没有显示任何错误消息。我只是不知道出了什么问题。
On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
Set SrcFolder = fso.GetFolder("C:\Users\Vault Keeper\Desktop\NewFile")
DestFolder = "C:\Users\Vault Keeper\Desktop\MasterFile"
For Each NewFile in SrcFolder.files
If fso.FileExists(DestFolder & "\" & NewFile.name) Then
txt = NewFile.OpenAsTextStream(1).ReadAll
Set MasterFile = FSO.OpenTextFile(DestFolder & "\" & NewFile.name, 8)
MasterFile.Write txt
Else
fso.CopyFile "C:\Users\Vault Keeper\Desktop\NewFile" & "\" & NewFile.name, _
DestFolder
End If
Next
您可以使用这个检查文件
Dim objFSO As Scripting.FileSystemObject
If objFSO Is Nothing Then Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(fileNameAndPath) Then
'code here...
End If
当您让脚本闭嘴不管出了什么问题时,您并不知道出了什么问题,这并不奇怪 (On Error Resume Next
)。删除该行(无论如何使用全局 OERN 都是一种糟糕的做法)并查看您得到了什么错误。
错误很可能是 "permission denied",因为您正在尝试将文件复制到现有目标文件夹,而该路径没有以反斜杠结尾:
DestFolder = "C:\Users\Vault Keeper\Desktop\MasterFile" '<-- no trailing backslash
...
fso.CopyFile "C:\Users\Vault Keeper\Desktop\NewFile" & "\" & NewFile.name, DestFolder
If source contains wildcard characters or destination ends with a path separator (), it is assumed that destination is an existing folder in which to copy matching files. Otherwise, destination is assumed to be the name of a file to create. In either case, three things can happen when an individual file is copied.
- If destination does not exist, source gets copied. This is the usual case.
- If destination is an existing file, an error occurs if overwrite is false. Otherwise, an attempt is made to copy source over the existing file.
- If destination is a directory, an error occurs.
将反斜杠附加到目标路径,错误应该消失。另外,由于您已经有了 File
object, I'd recommend to use its Copy
方法而不是 CopyFile
:
NewFile.Copy DestFolder & "\"