0

好的,所以我对 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
4

1 回答 1

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
于 2012-11-14T14:20:29.753 回答