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

时间:2015-06-30 13:07:32

标签: vbscript server sysadmin

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

我目前遇到的错误是“预期然后”这是我的循环。

Sub Recurse(strFolderPath)     昏暗的objFolder     设置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

1 个答案:

答案 0 :(得分:0)

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

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,否则您可能会错过所有类型的错误并导致各种令人头疼的问题。