Windows 调用 vbscript 的批处理文件不会等待它完成
Windows batch file calling vbscript doesn't wait for it finish
我有一个用于压缩一些文件的 VBScript。从批处理文件调用时,批处理不会等待脚本完成,您似乎需要在 vbscript 中休眠以便为压缩提供足够的时间,否则您最终会得到一个空的或损坏的 zip 文件。有没有办法让cscript同步执行?
脚本如下:
Set objArgs = WScript.Arguments
InputFolder = objArgs(0)
ZipFile = objArgs(1)
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)
wScript.Sleep 30000
请注意最后的睡眠是必要的,但由于我不知道实际压缩需要多长时间,所以很麻烦。批处理文件调用:
CScript //B zipCOM.vbs %TEMPZIPDIR% %ARCHIVEFILE%
这是我写的一个函数,可能会对您有所帮助:
http://www.naterice.com/blog/template_permalink.asp?id=64
Function WindowsZip(sFile, sZipFile)
'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 oZipShell = CreateObject("WScript.Shell")
Set oZipFSO = CreateObject("Scripting.FileSystemObject")
If Not oZipFSO.FileExists(sZipFile) Then
NewZip(sZipFile)
End If
Set oZipApp = CreateObject("Shell.Application")
sZipFileCount = oZipApp.NameSpace(sZipFile).items.Count
aFileName = Split(sFile, "\")
sFileName = (aFileName(Ubound(aFileName)))
'listfiles
sDupe = False
For Each sFileNameInZip In oZipApp.NameSpace(sZipFile).items
If LCase(sFileName) = LCase(sFileNameInZip) Then
sDupe = True
Exit For
End If
Next
If Not sDupe Then
oZipApp.NameSpace(sZipFile).Copyhere sFile
'Keep script waiting until Compressing is done
On Error Resume Next
sLoop = 0
Do Until sZipFileCount < oZipApp.NameSpace(sZipFile).Items.Count
Wscript.Sleep(100)
sLoop = sLoop + 1
Loop
On Error GoTo 0
End If
End Function
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
zip 文件夹操作是异步的。您需要等待代码结束。这是一个简单的方法:等到文件上的锁(由压缩操作保持)被释放。
Option Explicit
Dim objArgs, InputFolder, ZipFile
Set objArgs = WScript.Arguments
InputFolder = objArgs.Item(0)
ZipFile = objArgs.Item(1)
Dim fso
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
Dim objShell, source
Set objShell = CreateObject("Shell.Application")
Set Source = objShell.NameSpace(InputFolder).Items
objShell.NameSpace(ZipFile).CopyHere( source )
' Wait for the operation to start
WScript.Sleep 3000
' Loop until the file can be written
Dim objFile
On Error Resume Next
Do While True
Err.Clear
Set objFile = fso.OpenTextFile( ZipFile, 8, False )
If Err.Number = 0 Then
objFile.Close
Exit Do
End If
WScript.Sleep 100
Loop
我有一个用于压缩一些文件的 VBScript。从批处理文件调用时,批处理不会等待脚本完成,您似乎需要在 vbscript 中休眠以便为压缩提供足够的时间,否则您最终会得到一个空的或损坏的 zip 文件。有没有办法让cscript同步执行?
脚本如下:
Set objArgs = WScript.Arguments
InputFolder = objArgs(0)
ZipFile = objArgs(1)
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)
wScript.Sleep 30000
请注意最后的睡眠是必要的,但由于我不知道实际压缩需要多长时间,所以很麻烦。批处理文件调用:
CScript //B zipCOM.vbs %TEMPZIPDIR% %ARCHIVEFILE%
这是我写的一个函数,可能会对您有所帮助:
http://www.naterice.com/blog/template_permalink.asp?id=64
Function WindowsZip(sFile, sZipFile)
'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 oZipShell = CreateObject("WScript.Shell")
Set oZipFSO = CreateObject("Scripting.FileSystemObject")
If Not oZipFSO.FileExists(sZipFile) Then
NewZip(sZipFile)
End If
Set oZipApp = CreateObject("Shell.Application")
sZipFileCount = oZipApp.NameSpace(sZipFile).items.Count
aFileName = Split(sFile, "\")
sFileName = (aFileName(Ubound(aFileName)))
'listfiles
sDupe = False
For Each sFileNameInZip In oZipApp.NameSpace(sZipFile).items
If LCase(sFileName) = LCase(sFileNameInZip) Then
sDupe = True
Exit For
End If
Next
If Not sDupe Then
oZipApp.NameSpace(sZipFile).Copyhere sFile
'Keep script waiting until Compressing is done
On Error Resume Next
sLoop = 0
Do Until sZipFileCount < oZipApp.NameSpace(sZipFile).Items.Count
Wscript.Sleep(100)
sLoop = sLoop + 1
Loop
On Error GoTo 0
End If
End Function
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
zip 文件夹操作是异步的。您需要等待代码结束。这是一个简单的方法:等到文件上的锁(由压缩操作保持)被释放。
Option Explicit
Dim objArgs, InputFolder, ZipFile
Set objArgs = WScript.Arguments
InputFolder = objArgs.Item(0)
ZipFile = objArgs.Item(1)
Dim fso
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
Dim objShell, source
Set objShell = CreateObject("Shell.Application")
Set Source = objShell.NameSpace(InputFolder).Items
objShell.NameSpace(ZipFile).CopyHere( source )
' Wait for the operation to start
WScript.Sleep 3000
' Loop until the file can be written
Dim objFile
On Error Resume Next
Do While True
Err.Clear
Set objFile = fso.OpenTextFile( ZipFile, 8, False )
If Err.Number = 0 Then
objFile.Close
Exit Do
End If
WScript.Sleep 100
Loop