2015-05-14 60 views
0

我想使用批处理文件压缩文件夹。这里是我的代码zip.bat:用于压缩同一目录中的文件夹的批处理文件

CScript zip.vbs E:\app E:\app.zip 

zip.vbs具有下面的代码:

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 2000000 

此代码是正确ziiping的文件夹,但我不想提任何驱动器名称。我想在任何驱动器中,如果我保留我的bat文件n应用程序文件夹,然后运行bat文件后,它应该压缩文件夹。 有没有这个代码?

回答

0

您需要添加FileSystemObject并使用GetAbsolutePathName函数 - shell.application命名空间不接受相对路径。

尽管使用睡眠是一个坏主意 - 它会使脚本太慢或将会中断压缩。更好的想法是计算来源和目标中的项目,因为压缩是事务性的,文件数量是一个可靠的方法来检查操作是否完成。

您可以检查也是我zipjs.bat也使用Shell.Application但(它甚至不会创建临时文件)。它能够处理相对路径,以及相对稳健的一切都装在一个混合jscript\.bat脚本(我已经写了最常用的脚本之一 - 所以我有一些反馈和修正许多错误的:-)需要)这里

更多信息:How can I compress (/ zip) and uncompress (/ unzip) files and folders with batch file without using any external tools?

Set objArgs = WScript.Arguments 
set fso=CreateObject("Scripting.FileSystemObject"): 
InputFolder = fso.GetAbsolutePathName(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 2000000 
+0

ü可以分享一些简单的代码。你的那个提供不起作用 – Dimple

+0

@Dimple - 有些东西不适用于zipjs.bat?你能提供更多的信息吗?将更新我的答案。 – npocmaka

相关问题