Vbscript 将文件复制到压缩文件夹

Vbscript 将文件复制到压缩文件夹,vbscript,zip,compression,Vbscript,Zip,Compression,我正在尝试在没有任何第三方工具的情况下将现有文件复制到现有的压缩文件夹 我尝试了两种不同的方法,但都没有成功。我使用文本文件作为示例,实际上我需要将Excel文件复制到共享驱动器中的现有压缩文件夹中 第1种方式(FileSystemObject): 第二种方式(Shell): 找到了解决我自己问题的办法。在将我的变量设置为“无”之前,请使用: Wscript.sleep (5000) 所以看起来是这样的: Call fnMoveToZip() Function fnMoveToZip()

我正在尝试在没有任何第三方工具的情况下将现有文件复制到现有的压缩文件夹

我尝试了两种不同的方法,但都没有成功。我使用文本文件作为示例,实际上我需要将Excel文件复制到共享驱动器中的现有压缩文件夹中

第1种方式(
FileSystemObject
):

第二种方式(
Shell
):


找到了解决我自己问题的办法。在将我的变量设置为“无”之前,请使用:

Wscript.sleep (5000) 
所以看起来是这样的:

Call fnMoveToZip()

Function fnMoveToZip()
    Dim objShell
    Dim objFolder

    Set objShell = CreateObject("shell.Application")
    Set objFolder = objShell.NameSpace("C:\Users\User\Desktop\NewCompressed.zip")

    objFolder.CopyHere("C:\Users\User\Desktop\New_Folder\TestFile.txt")

    'New code
    Wscript.sleep (5000) 

    Set objShell = nothing
    Set objFolder = nothing
End Function

正如@JosefZ在您自己答案的注释中提到的:该方法异步运行,即它立即返回,而不等待复制操作完成。您的变通方法只起作用,因为您无意中选择了足够长的等待时间,以便将文件添加到存档中。但是,更好的方法是等待文件实际添加完毕:

Set objShell = CreateObject("shell.Application")
Set objFolder = objShell.NameSpace("C:\Users\User\Desktop\NewCompressed.zip")
cnt = objFolder.Items.Count + 1

objFolder.CopyHere("C:\Users\User\Desktop\New_Folder\TestFile.txt")

While objFolder.Items.Count < cnt
    WScript.Sleep 100
Wend
Set objShell=CreateObject(“shell.Application”)
设置objFolder=objShell.NameSpace(“C:\Users\User\Desktop\NewCompressed.zip”)
cnt=objFolder.Items.Count+1
CopyHere(“C:\Users\User\Desktop\New\u Folder\TestFile.txt”)
而objFolder.Items.Count

FileSystemObject
方法不起作用,因为
FileSystemObject
对归档一无所知。
Copy
方法只是(尝试)将源文件复制到目标文件上,而不是复制到目标文件中。

除了@ansgar wiechers伟大的答案之外,我想强调一个事实,即两者都需要绝对路径名,您可以使用绝对路径名创建。您输入的任何内容都将显示在ZIP的根级别,因此,如果您想要一个文件夹结构,您必须在使用它之前预先准备一个
staging
文件夹结构。我还建议,当您想从头开始使用构建zip文件时,使用
blank.zip
模板zip文件

通过创建任何zip文件并删除其内容来准备
blank.zip

以下示例从当前工作目录中的
staging
文件夹创建
hello.zip
存档。它要求您在当前工作目录中也有模板
blank.zip

选项显式
Dim fso、壳牌、dst
设置fso=CreateObject(“Scripting.FileSystemObject”)
Set shell=CreateObject(“shell.Application”)
调用fso.CopyFile(“blank.zip”、“hello.zip”)
Set dst=shell.Namespace(fso.GetAbsolutePathName(“hello.zip”))
dst.CopyHere(fso.GetAbsolutePathName(“staging”))
而dst.Items.count=0
WScript.Sleep 100
温德

即使我需要相同的功能,但我需要在多个拉链中复制相同的文件

Call fnMoveToZip()

Function fnMoveToZip()
    Dim objShell
    Dim objFolder

    Set objShell = CreateObject("shell.Application")
    Set objFolder = objShell.NameSpace("C:\Users\User\Desktop\NewCompressed.zip")
   'here i need reference which will select the path from column A  
    objFolder.CopyHere("C:\Users\User\Desktop\New_Folder\TestFile.txt")    

    'New code
    Wscript.sleep (5000) 

    Set objShell = nothing
    Set objFolder = nothing

End Function

5000
无法满足较大文件的要求:此方法不返回值。不会向调用程序发出通知以指示复制已完成。删除多余的
Set objShell=nothing
Set objFolder=nothing
谢谢大家!今晚我要试试这个。
Set objShell = CreateObject("shell.Application")
Set objFolder = objShell.NameSpace("C:\Users\User\Desktop\NewCompressed.zip")
cnt = objFolder.Items.Count + 1

objFolder.CopyHere("C:\Users\User\Desktop\New_Folder\TestFile.txt")

While objFolder.Items.Count < cnt
    WScript.Sleep 100
Wend
Call fnMoveToZip()

Function fnMoveToZip()
    Dim objShell
    Dim objFolder

    Set objShell = CreateObject("shell.Application")
    Set objFolder = objShell.NameSpace("C:\Users\User\Desktop\NewCompressed.zip")
   'here i need reference which will select the path from column A  
    objFolder.CopyHere("C:\Users\User\Desktop\New_Folder\TestFile.txt")    

    'New code
    Wscript.sleep (5000) 

    Set objShell = nothing
    Set objFolder = nothing

End Function