【问题标题】:scan computer for a file and output folder location扫描计算机以查找文件和输出文件夹位置
【发布时间】:2015-06-30 13:07:32
【问题描述】:

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

我当前遇到的问题是“预期的”错误,这是我的循环。

子递归(strFolderPath) 暗淡 objFolder 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

【问题讨论】:

  • 我很抱歉没有很好地解释。脚本需要搜索文件并输出它的文件夹路径,而不是文件本身。
  • 最好让每台 PC 的力量为您工作,并部署一个登录脚本,在用户登录时搜索用户的 PC,然后写入共享网络文本文件。需要搜索多少台电脑?
  • 这可能很方便!但是,我仍然不知道该怎么做:)大约需要搜索 120 台计算机。
  • 您的问题可能被认为过于宽泛,不适合 SO。您可能想用您正在努力解决的特定 VBScript 问题来更新您的问题。你有错误吗?递归有问题吗?从文本文件中读取计算机名称?等等。发布整个脚本并要求填补空白可能会让您投反对票或接近投票。专注于您遇到的特定问题,我们会尽力提供帮助。
  • 感谢我调整了帖子以引用特定问题。

标签: vbscript server sysadmin


【解决方案1】:

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

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,否则您可能会错过各种错误并引起各种头痛。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-25
    • 1970-01-01
    • 2015-08-27
    • 2013-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多