如何使用VBScript将文件夹的每个子文件夹压缩为ZIP存档?

时间:2017-08-01 02:12:47

标签: batch-file vbscript zip

我有这样的文件夹。

  • 主文件夹
    • SUB1
    • SUB6
    • SUB8

我想使用VBScript将这些文件夹压缩为sub1.zipsub6.zipsub8.zip

我正在使用此VBScript和批处理文件:

'Get command-line arguments.
Set objArgs = WScript.Arguments
InputFolder = objArgs(0)
ZipFile = objArgs(1)

'Create empty ZIP file.
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)

Set objShell = CreateObject("Shell.Application")

Set source = objShell.NameSpace(InputFolder).Items

objShell.NameSpace(ZipFile).CopyHere(source)

'Required!
wScript.Sleep 2000

使用批处理文件转换每个文件夹,如下所示:

set /p "xxx=enter your subfolder name"
cscript.exe zip.vbs "%xxx%" "%xxx%.zip"

如何在不删除文件夹的情况下将每个子文件夹压缩为ZIP存档文件?

我不是程序员。我不知道如何使用 for 循环来输入文本文件。我没有任何外部软件,如 WinRAR 7-Zip

2 个答案:

答案 0 :(得分:0)

是否有必要从批处理文件中调用VBScript?如果没有,您可以尝试此代码。此代码获取当前文件夹中所有文件夹的列表并压缩它们。因此,如果您在主文件夹中运行它,您将获得sub1.zip,sub6.zip,sub8.zip和原始文件夹保持不变。但是,子文件夹必须只包含文件,否则创建的zip文件将为空。

Set fso = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Shell.Application")
Set currentFolder = fso.GetFolder(".")

For each folder in currentFolder.SubFolders 
'Create empty ZIP file.
zipName = folder.Name + ".zip"
fso.CreateTextFile(zipName, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
Set zipFile = fso.GetFile(zipName)
Set source = objShell.NameSpace(folder.Path).Items

objShell.NameSpace(zipFile.Path).CopyHere(source)

Next

'Required!
wScript.Sleep 2000

答案 1 :(得分:0)

如果你在批处理文件中这样做:

set /p "xxx=enter your subfolder name"
copy %xxx% %xxx%1
cscript.exe zip.vbs "%xxx%" "%xxx%.zip"

应该做的伎俩