0

I have folders like this.

  • Main folder
    • sub1
    • sub6
    • sub8

I want to compress these folders to sub1.zip, sub6.zip and sub8.zip using VBScript.

I was using this VBScript and batch file:

'Get command-line arguments.
Set objArgs = WScript.Arguments
InputFolder = objArgs(0)
ZipFile = objArgs(1)

'Create empty ZIP file.
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)

'Required!
wScript.Sleep 2000

For converting each folders using batch file like this:

set /p "xxx=enter your subfolder name"
cscript.exe zip.vbs "%xxx%" "%xxx%.zip"

How to compress each subfolder into a ZIP archive file without deleting the folder?

I am not a programmer. I don't know how to use for loop to input a text file. I don't have any external software like WinRAR or 7-Zip.

0

2 Answers 2

0

Is it necessary to call VBScript from batch file ? If not, you could try this code. This code gets the list of all folders in current folder and zip them. So if you run it in main folder you get sub1.zip, sub6.zip, sub8.zip and original folders remains intact. However subfolders must contain only files or the created zip file will be empty.

Set fso = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Shell.Application")
Set currentFolder = fso.GetFolder(".")

For each folder in currentFolder.SubFolders 
'Create empty ZIP file.
zipName = folder.Name + ".zip"
fso.CreateTextFile(zipName, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
Set zipFile = fso.GetFile(zipName)
Set source = objShell.NameSpace(folder.Path).Items

objShell.NameSpace(zipFile.Path).CopyHere(source)

Next

'Required!
wScript.Sleep 2000
0

if you in your batch file do it like this:

set /p "xxx=enter your subfolder name"
copy %xxx% %xxx%1
cscript.exe zip.vbs "%xxx%" "%xxx%.zip"

that should do the trick

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.