使用 WMI 检查文件是否不在同一文件夹中

Check if file is not at the same folder using WMI

我正在使用正在本地磁盘中查找指定文件的脚本。当它找到文件时,它会 renames/removes 个接近指定文件的文件。 (我的意思是在同一个目录等)

示例代码:

Sub RenameFolder( oldName, newName )
    Dim filesys
    Set filesys = WScript.CreateObject("Scripting.FileSystemObject")

    If filesys.FolderExists( oldName ) Then
        filesys.MoveFolder oldName, newName
    End If
End Sub

strComputer = "."
Set objWMIService = GetObject("winmgmts:\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService.ExecQuery _
    ("Select * from CIM_DataFile Where Filename = 'myfile' and Extension = 'exe'")

For Each objFile in colFiles

RenameFolder objFile.Drive & objFile.Path & "files\test",  objFile.Drive & objFile.Path & "files\test_old"

我想添加一个条件,它会检查是否在与 myfile.exe 相同的目录中,还有另一个名为 otherfile.exe.

的文件

如果存在 - 什么都不做,否则 - 像上面的代码一样重命名指定的文件夹。

您要查找的是 FileExists 方法。以下是我建议您在代码中使用它的方式:

Sub RenameFolder( oldName, newName )
    Dim filesys
    Set filesys = WScript.CreateObject("Scripting.FileSystemObject")

    If filesys.FolderExists( oldName ) Then
        filesys.MoveFolder oldName, newName
    End If
End Sub

strComputer = "."
Set objWMIService = GetObject("winmgmts:\" & strComputer & "\root\cimv2")

Set colFiles = objWMIService.ExecQuery _
    ("Select * from CIM_DataFile Where Filename = 'myfile' and Extension = 'exe'")

For Each objFile in colFiles
    If Not filesys.FileExists(objFile.Drive & objFile.Path & "otherfile.exe") Then
        'No else clause needed since we are checking if the file _doesn't_ exist.
        RenameFolder objFile.Drive & objFile.Path & "files\test",  objFile.Drive & objFile.Path & "files\test_old"
    End If
Next

编辑:将我的示例更改为直接在提问者的代码中工作。