好的,所以我对VBScript很陌生,所以对我很轻松。
我正在尝试构建一个VBScript,它将从主备份文件夹中不再存在的冗余备份文件夹中删除文件。我设想一些东西,它将每个文件放在冗余文件夹中,并检查主备份文件夹中是否存在具有相同名称的文件。如果没有,则删除冗余文件夹中的文件。这似乎很容易,但我认为我缺乏VB知识让我退缩。
编辑:这将在Server 2003上运行。
这是我到目前为止所拥有的:
'variables
dim fso1
dim fso2
dim redundantFolder
dim primaryFolder
dim redundantFile
dim primaryFile
dim counter
'creates file system object
Set fso1 = CreateObject("Scripting.FileSystemObject")
set fso2 = CreateObject("Scripting.FileSystemObject")
'sets the folder object to redundant backup folder
Set redundantFolder = fso1.GetFolder("C:\Users\bmcwilliams\Desktop\Dev\TestCompleted")
'sets folder object to primary folder
Set primaryFolder = fso2.getFolder("C:\Users\bmcwilliams\Desktop\Dev\TestUnCompleted")
'deletes files that aren't found in primary folder
For Each redundantFile in redundantFolder
For Each primaryFile in primaryFolder
if primaryFile.name == redundantFile.name
flag = false
Next
我知道“For Each”代码块中的逻辑是不完整的,而且完全错误,但就我的小脑子允许我去的那样。
提前致谢。非常感谢任何帮助。
编辑:这是最终的代码。我希望这对另一位初学者有所帮助:
'creates file system object
Set fso1 = CreateObject("Scripting.FileSystemObject")
Set fso2 = CreateObject("Scripting.FileSystemObject")
'sets the folder object to redundant backup folder
Set redundantFolder = fso1.GetFolder("C:\temp\redundant")
'sets folder object to primary folder
Set primaryFolder = fso2.GetFolder("C:\temp\primary")
'deletes files that aren't found in primary folder
For Each redundantFile In redundantFolder.Files
If Not fso1.FileExists(primaryFolder.Path + "\" + redundantFile.Name) Then
redundantFile.Delete
End If
Next
答案 0 :(得分:1)
'creates file system object
Set fso = CreateObject("Scripting.FileSystemObject")
'sets the folder object to redundant backup folder
Set redundantFolder = fso.GetFolder("C:\temp\redundant")
'sets folder object to primary folder
Set primaryFolder = fso.GetFolder("C:\temp\primary")
'deletes files that aren't found in primary folder
For Each redundantFile In redundantFolder.Files
If Not fso.FileExists(fso.BuildPath(primaryFolder.Path,redundantFile.Name)) Then
redundantFile.Delete
End If
Next