我是VBScript的新手,我正在尝试编写一个简单的脚本,将目录中的文件解压缩到新目录。到目前为止,这就是我所拥有的(并且效果很好):
'USER VAR REPRESENTS WINDOWS USERNAME
Set oShell = CreateObject( "WScript.Shell" )
user=oShell.ExpandEnvironmentStrings("%UserName%")
'FOLDER TO BE EXTRACTED
ZipFile="C:\Users\"&user&"\Downloads\Test.zip"
'LOCATION TO EXTRACT FILES
ExtractTo="C:\Users\"&user&"\desktop"
'EXTRACT ZIP FILE
Set objShell = CreateObject("Shell.Application")
Set FilesInZip=objShell.NameSpace(ZipFile).items
objShell.NameSpace(ExtractTo).CopyHere(FilesInZip)
Set fso = Nothing
Set objShell = Nothing
Set oShell = Nothing
现在,如果可能,如果找不到“桌面”文件夹,或者找不到“Test.zip”文件,我想在C盘中搜索它们,然后继续提取等等。看过一些例子,但我无法理解如何复制它们。如何在整个C驱动器和子文件夹中搜索这些文件?
提前感谢帮助,我们将不胜感激!
答案 0 :(得分:1)
通常,递归搜索可以这样完成:
Function SearchFolder(fldr, name)
Set SearchFolder = Nothing
For Each f In fldr.Files
If LCase(f.Name) = LCase(name) Then
Set SearchFolder = f
Exit Function
End If
Next
For Each sf In fldr.SubFolders
Set result = SearchFolder(sf, name)
If Not result Is Nothing Then
Set SearchFolder = result
Exit Function
End If
Next
End Function
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = SearchFolder(fso.GetFolder("C:\"), "Test.zip")
但是,以这种方式搜索整个驱动器需要相当长的时间。此外,还有几个用户无法访问的文件夹,因此如果您要实现此类搜索,则必须考虑到这一点。