为什么这个 VBScript 运行 在 Excel VBA 中很好,但作为一个独立的 vbscript?
Why does this VBScript run fine in Excel VBA but as a stand alone vbscript?
我写了这个 VBscript 来创建一个 zip 文件,然后将一个文件夹复制到其中。当我 运行 脚本作为 excel 中的 Sub 时,它会创建文件并将文件夹完美地复制到其中,但是当我 运行 它作为 .vbs 文件时,它会创建 zip 文件,但什么也没有否则会发生。我尝试在创建 zip 文件后但在复制文件之前添加 wscript.sleep 10000,但仍然没有任何反应。我还尝试使用 FileExists 检查复制之前是否存在 zip 文件,它 returns 为真但仍然不会复制。这是代码。
Dim dtmValu
dtmValue = Now()
Dim DestPath
DestPath = "C:\Users\FirstUser\Desktop\Test\" & Month(dtmValue) & "_" & Day(dtmValue) & "_" & Year(dtmValue) & ".zip"
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(DestPath, 8, vbTrue)
BlankZip = "PK" & Chr(5) & Chr(6)
For x = 0 To 17
BlankZip = BlankZip & Chr(0)
Next
ts.Write BlankZip
Set objFolder = Nothing
Set objShell = Nothing
Set fso = Nothing
Set ts = Nothing
Set objShell = CreateObject("shell.Application")
Set oFso = CreateObject("Scripting.FileSystemObject")
Set objFolder = objShell.Namespace(DestPath)
Dim sFolder
sFolder = "C:\Users\FirstUser\Desktop\TestSource\"
objFolder.CopyHere (oFso.GetAbsolutePathName(sFolder))
此行:objFolder.CopyHere (oFso.GetAbsolutePathName(sFolder))
执行异步,并且可能您的 objFolder
在完成操作之前被销毁。
尝试添加一些逻辑以在退出脚本之前检查复制是否完成。您提到您增加了等待时间,但很难说这是否足够长,而且许多因素可能会使时间有所不同。但是这种方法应该更稳定一些。在复制前统计项目数量,然后比较之后的计数,直到复制完成才结束:
'// your pre-copy code here:
'//...
Set objShell = CreateObject("shell.Application")
Set oFso = CreateObject("Scripting.FileSystemObject")
Set objFolder = objShell.Namespace(DestPath)
cnt = objFolder.Items.Count + 1 '// I added this
Dim sFolder
objFolder.CopyHere oFso.GetAbsolutePathName(sFolder)
'// and this
While objFolder.Items.Count < cnt
WScript.Sleep 100
Wend
我写了这个 VBscript 来创建一个 zip 文件,然后将一个文件夹复制到其中。当我 运行 脚本作为 excel 中的 Sub 时,它会创建文件并将文件夹完美地复制到其中,但是当我 运行 它作为 .vbs 文件时,它会创建 zip 文件,但什么也没有否则会发生。我尝试在创建 zip 文件后但在复制文件之前添加 wscript.sleep 10000,但仍然没有任何反应。我还尝试使用 FileExists 检查复制之前是否存在 zip 文件,它 returns 为真但仍然不会复制。这是代码。
Dim dtmValu
dtmValue = Now()
Dim DestPath
DestPath = "C:\Users\FirstUser\Desktop\Test\" & Month(dtmValue) & "_" & Day(dtmValue) & "_" & Year(dtmValue) & ".zip"
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(DestPath, 8, vbTrue)
BlankZip = "PK" & Chr(5) & Chr(6)
For x = 0 To 17
BlankZip = BlankZip & Chr(0)
Next
ts.Write BlankZip
Set objFolder = Nothing
Set objShell = Nothing
Set fso = Nothing
Set ts = Nothing
Set objShell = CreateObject("shell.Application")
Set oFso = CreateObject("Scripting.FileSystemObject")
Set objFolder = objShell.Namespace(DestPath)
Dim sFolder
sFolder = "C:\Users\FirstUser\Desktop\TestSource\"
objFolder.CopyHere (oFso.GetAbsolutePathName(sFolder))
此行:objFolder.CopyHere (oFso.GetAbsolutePathName(sFolder))
执行异步,并且可能您的 objFolder
在完成操作之前被销毁。
尝试添加一些逻辑以在退出脚本之前检查复制是否完成。您提到您增加了等待时间,但很难说这是否足够长,而且许多因素可能会使时间有所不同。但是这种方法应该更稳定一些。在复制前统计项目数量,然后比较之后的计数,直到复制完成才结束:
'// your pre-copy code here:
'//...
Set objShell = CreateObject("shell.Application")
Set oFso = CreateObject("Scripting.FileSystemObject")
Set objFolder = objShell.Namespace(DestPath)
cnt = objFolder.Items.Count + 1 '// I added this
Dim sFolder
objFolder.CopyHere oFso.GetAbsolutePathName(sFolder)
'// and this
While objFolder.Items.Count < cnt
WScript.Sleep 100
Wend