2012-12-05 63 views
0

我想將一個文件添加到一個文件夾,並將其保存爲VBScript中的壓縮文件夾。爲什麼這個VBScript不會將文件添加到ZIP文件?

我已經寫了下面的代碼,但它只創建ZIP文件,並沒有將文件添加到它。這個代碼有什麼問題?

Option Explicit 
dim wshShell 
Const MoveMode = True 
Const BackupDir = "D:\csv\Image\" 
Const Outfilename = "MyZip.zip" 
Const TimeoutMins = 10 ' Timeout for individual file compression operation 
'Set wshShell = CreateObject("WScript.shell") 
Dim FSO : set FSO = CreateObject("Scripting.FileSystemObject") 
Dim Folder : Set Folder = FSO.GetFolder("D:\csv\Image") 
Dim Files : Set Files = Folder.Files 

Dim File 
Dim Counter : Counter=0 
Dim Timeout : Timeout = 0 

FSO.CreateTextFile "D:\csv\" & OutFilename,true '.WriteLine "PK" & Chr(5) & Chr(6) &    String(18, 0) 

Dim Shell : Set Shell = CreateObject("Shell.Application") 
Dim ZipFile: Set ZipFile = Shell.NameSpace("D:\csv\"& OutFilename) 

If Not ZipFile Is Nothing Then 
    Shell.NameSpace("D:\csv\"&Outfilename).CopyHere "D:\csv\Image\calender.png" 
End If 
+0

你能告訴我們你有什麼了嗎? – redhotspike

+0

可能重複[可以Windows'內置ZIP壓縮腳本?](http://stackoverflow.com/questions/30211/can-windows-built-in-zip-compression-be-scripted) –

+1

我認爲'CopyHere()'是異步的(如果這是你的完整的代碼清單),你的腳本過程在任何文件被複制之前結束。 – 2012-12-06 09:10:14

回答

1

This節目試圖將數據複製到它之前創建一個新的ZIP文件之後,等待500毫秒。你有沒有嘗試過使用WScript.Sleep(500)?

Dim FSO : set FSO = CreateObject("Scripting.FileSystemObject") 
Dim Shell : Set Shell = CreateObject("Shell.Application") 

If Not FSO.FileExists("D:\csv\" & OutFilename) Then 
    NewZip("D:\csv\" & OutFilename) 
End If 

If Not ZipFile Is Nothing Then 
    Shell.NameSpace("D:\csv\" & Outfilename).CopyHere BackupDir & "calender.png" 
End If 

Sub NewZip(sNewZip) 
    'This script is provided under the Creative Commons license located 
    'at http://creativecommons.org/licenses/by-nc/2.5/ . It may not 
    'be used for commercial purposes with out the expressed written consent 
    'of NateRice.com 

    Set oNewZipFSO = CreateObject("Scripting.FileSystemObject") 
    Set oNewZipFile = oNewZipFSO.CreateTextFile(sNewZip) 

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

    oNewZipFile.Close 
    Set oNewZipFSO = Nothing 

    Wscript.Sleep(500) 
End Sub 

(未經測試)

相關問題