扫描计算机以查找文件和输出文件夹位置

scan computer for a file and output folder location

我需要以 CSV 格式列出所有位于同一域中的计算机(IP 或 PC 名称)。扫描每台计算机以查找特定文件夹名称。该文件夹将为 arcXXXof。 x 是每台 PC 的散列和更改。如果找到该文件夹​​,则需要将文件夹路径输出到 CSV 文件并附加扫描的每台计算机。我的编程能力有限,我真的只知道Java。由于这将是来自服务器的 运行,因此它需要在本地机器上 运行 的本地管理权限。我的经理建议我使用 VBS,但我以前从未用过它。

我当前的障碍出现错误"expected then"这是我的循环。

子递归(strFolderPath) 昏暗的对象文件夹 Set objFolder = objFSO.GetFolder(strFolderPath) '读取从递归中拉出的文件夹

Dim objSubFolder

dim folderStart 'grabs the first 2 characters of the file name. Should match 'of' if correct folder
Dim folderEnd 'grabs the last 6 (test) characters of the folder name, should match arc.txt if correct

Global checkEnd
set checkEnd = "arc" 'checks for "arc" at ending

Global checkStart
set checkStart = "of" 'used to check if folder name is correct path



For Each objSubFolder in objFolder  'for every Folder scanned
'Scans the name of the Folder, objSubFolder, for an ending of “arc", and beginning of “of” (testing)
set folderName = objSubFolder.name
Set folderEnd = right(folderName, 3) 
set folderStart = left(folderName, 2)
dim folderName

    if folderName = testFolderName
    then WScript.Echo objSubFolder
    'If folderEnd = checkEnd and
    'If folderStart = checkStart

    'Add Folder location to array, set array to next object
    'Then fLocations(i) = object.GetAbsolutePathName(objSubFolder) and i = i+1 
else
    End If
Next
'recursive for searching new folder
For Each objSubFolder in objFolder.Subfolders
    Call Recurse(objSubFolder.Path)
Next

好的,您可以使用正则表达式来匹配名称。在您的全局范围内预先定义它:

Dim re
Set re = New RegExp
re.IgnoreCase = True
re.Pattern = "^arc\w{3}of$"

我正在使用 \w,相当于 [a-zA-Z_0-9]。如果您只需要数字 (\d) 或这三个字符的其他内容,您可以更改此设置。

然后,在您的 Recurse 函数中,根据它测试文件夹名称:

For Each objSubFolder in objFolder.SubFolders

    ' See if this folder name matches our regex...
    If re.Test(objSubFolder.Name) Then

        ' Match. Just display for now...
        WScript.Echo objSubFolder.Path

    End If

    ' Test its subfolders...
    Recurse objSubFolder.Path

Next

提示:在开发过程中从代码中删除 On Error Resume Next,否则您可能会遗漏各种错误并导致各种头痛。