-2

Need help with VB script to zip a file in a folder and give the zip file created the name of the original name of the file. Like if there is a trn file named abc.trn then want to create a zip file named abc.zip. Please help if anyone have any idea

Abhishek
  • 5
  • 1
  • 2

1 Answers1

3

Yes and no. According to this answer on SO it is not possible using VBscript alone.

There are VBA methods to zip and unzip using the windows built in compression as well, which should give some insight as to how the system operates. You may be able to build these methods into a scripting language of your choice.

However, you can use an "implementation-dependent behavior of the Windows shell" (again, quoted from the same source)

Dim fso, winShell, MyTarget, MySource, file
Set fso = CreateObject("Scripting.FileSystemObject")
Set winShell = createObject("shell.application")


MyTarget = Wscript.Arguments.Item(0)
MySource = Wscript.Arguments.Item(1)

Wscript.Echo "Adding " & MySource & " to " & MyTarget

'create a new clean zip archive
Set file = fso.CreateTextFile(MyTarget, True)
file.write("PK" & chr(5) & chr(6) & string(18,chr(0)))
file.close

winShell.NameSpace(MyTarget).CopyHere winShell.NameSpace(MySource).Items

do until winShell.namespace(MyTarget).items.count = winShell.namespace(MySource).items.count
    wscript.sleep 1000 
loop

Set winShell = Nothing
Set fso = Nothing
Community
  • 1
  • 1
Dave
  • 8,163
  • 11
  • 67
  • 103