2013-02-28 36 views
6

我想在VBScript中压缩一个文件夹,它似乎不起作用。我确定我正在创建头文件。向上拉一个文件夹

它正确创建实际文件,只是不压缩文件夹。

任何人有任何想法:

Sub ArchiveFolder (folder) 

    Dim fso, wShell, sApp, zipFile 

    Set fso = CreateObject("Scripting.FileSystemObject") 
    Set wShell = CreateObject("WScript.Shell") 
    Set sApp = CreateObject("Shell.Application") 
    Set zipFile = fso.CreateTextFile(folder & ".zip") 

    ' Write zip file header. 
    zipFile.Write "PK" & Chr(5) & Chr(6) & String(18, 0) 
    zipFile.Close 

    sApp.NameSpace(folder & ".zip").CopyHere folder 

End Sub 
+0

[查看本(http://stackoverflow.com/questions/28043589/) zip实用程序是用JScript编写的,但它可以称之为外部进程,或者如果将它与vbscript代码一起放入wsh文件中。 – npocmaka 2015-01-31 11:09:48

回答

9

答案我发现here。神奇的是在最后Do..Loop脚本等待壳牌做它的工作。

ArchiveFolder "sub\foo.zip", "..\baz" 

Sub ArchiveFolder (zipFile, sFolder) 

    With CreateObject("Scripting.FileSystemObject") 
     zipFile = .GetAbsolutePathName(zipFile) 
     sFolder = .GetAbsolutePathName(sFolder) 

     With .CreateTextFile(zipFile, True) 
      .Write Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, chr(0)) 
     End With 
    End With 

    With CreateObject("Shell.Application") 
     .NameSpace(zipFile).CopyHere .NameSpace(sFolder).Items 

     Do Until .NameSpace(zipFile).Items.Count = _ 
       .NameSpace(sFolder).Items.Count 
      WScript.Sleep 1000 
     Loop 
    End With 

End Sub 
+0

上面的代码工作正常,但创建zip文件后无法将文件复制到它..saying访问被拒绝。手动也我试图打开文件得到相同的错误。请建议 – sanjeeb 2017-07-04 12:31:33

1

检查论证。 folder必须是要放入zip文件的对象的路径。如果它是一个文件夹对象,则必须使用folder.Path,因为文件夹对象的默认方法是Name,而CopyHere找不到名称相同的对象。

你可以添加一些调试语句给你的函数来检查:

WScript.Echo TypeName(folder) 
If fso.FolderExists(folder) Then 
    WScript.Echo folder & " exists." 
Else 
    WScript.Echo folder & " doesn't exist." 
End If 
相关问题