简体   繁体   English

VBS压缩脚本

[英]VBS zipping script

I found (apparently working for everybody) script which only needs to be modified (paths): 我发现(显然为每个人工作)脚本,只需要修改(路径):

Sub NewZip(pathToZipFile)
  'WScript.Echo "Newing up a zip file (" & pathToZipFile & ") "

  Dim fso
  Set fso = CreateObject("Scripting.FileSystemObject")
  Dim file
  Set file = fso.CreateTextFile(pathToZipFile)

  file.Write Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, 0)

  file.Close
  Set fso = Nothing
  Set file = Nothing

  WScript.Sleep 500
End Sub

Sub CreateZip(pathToZipFile, dirToZip)
  'WScript.Echo "Creating zip  (" & pathToZipFile & ") from (" & dirToZip & ")"

  Dim fso
  Set fso= Wscript.CreateObject("Scripting.FileSystemObject")

  pathToZipFile = fso.GetAbsolutePathName(pathToZipFile)
  dirToZip = fso.GetAbsolutePathName(dirToZip)

  If fso.FileExists(pathToZipFile) Then
    'WScript.Echo "That zip file already exists - deleting it."
    fso.DeleteFile pathToZipFile
  End If

  If Not fso.FolderExists(dirToZip) Then
    'WScript.Echo "The directory to zip does not exist."
    Exit Sub
  End If

  NewZip pathToZipFile

  dim sa
  set sa = CreateObject("Shell.Application")

  Dim zip
  Set zip = sa.NameSpace(pathToZipFile)

  'WScript.Echo "opening dir  (" & dirToZip & ")"

  Dim d
  Set d = sa.NameSpace(dirToZip)

  ' Look at http://msdn.microsoft.com/en-us/library/bb787866(VS.85).aspx
  ' for more information about the CopyHere function.
  zip.CopyHere d.items, 4

  Do Until d.Items.Count <= zip.Items.Count
    Wscript.Sleep(200)
  Loop
End Sub

Can anybody give example how this script should look like with real paths? 谁能举例说明该脚本在真实路径下的外观吗? I'm trying but it's not working for me. 我正在尝试,但对我不起作用。

This script consists solely of two subroutines and thus will never execute anything. 该脚本仅包含两个子例程,因此将永远不会执行任何操作。

Add the following line to the bottom of the file and it should work (given that the code in the subs is sound, I have not tested): 将以下行添加到该文件的底部,它应该可以工作(假设subs中的代码是正确的,我尚未测试):

CreateZip "c:\output\test.zip" "c:\input\"

This will do the following, in order: 这将按顺序执行以下操作:

  1. Check to see if the output file exists. 检查输出文件是否存在。 If it does, it will delete it. 如果是这样,它将删除它。
  2. Check to see if the input folder exists, if it does not, the script will exit. 检查输入文件夹是否存在,如果不存在,脚本将退出。
  3. Call the NewZip sub which just creates an "empty" .zip file. 调用NewZip子项,该子项仅创建一个“空” .zip文件。
  4. Copy files to the zip file. 将文件复制到zip文件。
  5. Pause for a while, probably to ensure you don't try to access the ZIP before it's done copying. 暂停一会儿,可能是为了确保您不要在复制完成后尝试访问ZIP。

The contents of the input folder will now be in the zip file in the output folder. 现在, input文件夹的内容将位于output文件夹的zip文件中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM