我根据超级用户以前的回答编写了这个脚本,但现在只需要存档特定文件类型。我尝试使用通配符,但似乎没有成功。
如果我想将此脚本更改为按特定文件类型而不是所有文件系统对象进行过滤,需要更改什么以及如何更改?例如,如果我只想存档 DLL 文件或 DLL 和 EXE 文件,该如何更改?
'To use this at command-line, call `CScript.exe zip_it.vbs SourceDirectory PathToOutputZipFileIncludingDotZipExt`
Set parameters = WScript.Arguments
Set FS = CreateObject("Scripting.FileSystemObject")
SourceDir = FS.GetAbsolutePathName(parameters(0))
ZipFile = FS.GetAbsolutePathName(parameters(1))
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
Set shell = CreateObject("Shell.Application")
Set source_objects = shell.NameSpace(SourceDir).Items
shell.NameSpace(ZipFile).CopyHere(source_objects)
wScript.Sleep 400
答案1
你可以使用过滤器。神奇之处在于:
SHCONTF_NONFOLDERS = 64
colFolderItems.Filter SHCONTF_NONFOLDERS, "*.ext"
这是您的代码的上下文:
'To use this at command-line, call `CScript.exe zip_it.vbs SourceDirectory PathToOutputZipFileIncludingDotZipExt "file.pattern"`
Set parameters = WScript.Arguments
Set FS = CreateObject("Scripting.FileSystemObject")
SourceDir = FS.GetAbsolutePathName(parameters(0))
ZipFile = FS.GetAbsolutePathName(parameters(1))
FileFilter = parameters(2)
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
Set shell = CreateObject("Shell.Application")
Set source_objects = shell.NameSpace(SourceDir).Items
SHCONTF_NONFOLDERS = 64
source_objects.Filter SHCONTF_NONFOLDERS, FileFilter
shell.NameSpace(ZipFile).CopyHere(source_objects)
wScript.Sleep 400
可以使用“*.txt”来调用它以仅选择文本文件,或使用“*.exe”来仅收集 exe 文件。另请注意,如果没有包含文件过滤器,它就无法正常播放。
但是,您的代码设计为每次都重新创建 ZIP,因此如果您想重复调用其他文件类型,则需要先测试文件是否存在。此更改可实现此目的:
If Not FS.FileExists (ZipFile) Then
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
End If
如果文件已存在于 zip 文件中,则这会产生副作用,即弹出(并几乎立即隐藏)覆盖提示。通过此更改,您可以执行以下操作:
cscript //nologo zip_it.vbs thefolder thefile.zip "*.txt"
cscript //nologo zip_it.vbs thefolder thefile.zip "*.doc"
如果没有过滤器,它可以被更改为迭代文件类型或扩展名并收集所有文件,如下所示:
'To use this at command-line, call `CScript.exe zip_it.vbs SourceDirectory PathToOutputZipFileIncludingDotZipExt "file.pattern" "file2.pattern"`
Set parameters = WScript.Arguments
Set FS = CreateObject("Scripting.FileSystemObject")
SourceDir = FS.GetAbsolutePathName(parameters(0))
ZipFile = FS.GetAbsolutePathName(parameters(1))
SHCONTF_NONFOLDERS = 64
If Not FS.FileExists (ZipFile) Then
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
End If
Set shell = CreateObject("Shell.Application")
If parameters.Count > 2 Then
For lParams = 2 to (parameters.Count-1)
FileFilter = parameters(lParams)
Set source_objects = shell.NameSpace(SourceDir).Items
source_objects.Filter SHCONTF_NONFOLDERS, FileFilter
shell.NameSpace(ZipFile).CopyHere(source_objects)
wScript.Sleep 400
Next
Else
Set source_objects = shell.NameSpace(SourceDir).Items
shell.NameSpace(ZipFile).CopyHere(source_objects)
wScript.Sleep 400
End If
这可以这样调用:
cscript //nologo zip_it.vbs thefolder thefile.zip "*.doc" "*.txt"