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

问题描述:

我有这样的文件夹。如何使用VBScript将文件夹的每个子文件夹压缩为ZIP压缩文件?

  • 主文件夹
    • SUB1
    • SUB6
    • SUB8

我想这些文件夹压缩到sub1.zipsub6.zipsub8.zip使用VBScri PT。

我用这个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压缩文件,但不删除夹?

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

+0

你知道,7-Zip是免费的,'7za.exe'(架 - 单独版本)根本就不需要安装,只需复制到批处理文件所在的目录中即可?使用'7za.exe',整个压缩任务是批处理文件中的单个命令行。 – Mofi

是否需要从批处理文件调用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 

如果您在批处理文件做这样的:

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

应该做的伎俩