JohnLogan1017
JohnLogan1017

Reputation: 1

How to create bat file that zip all folders using winzip?

I want to create a bat file that compresses all the folders inside a specific directory and then deletes them. For example, Folder structure (before running bat file):

VidoesArch
      January
      February
      March
      ....
  TextualArch
      January
      February
      March
      ....


Folder structure (after running bat file):
  VideosArch
      January.zip
      February.zip
      March.zip
      ....
  TextualArch
      January.zip
      February.zip
      March.zip
      ....

This what I have developed so far:

@echo off
setlocal
set INPBKP=C:\Data\Backup\VideosArch
set OUTBKP=C:\Data\Backup\TextualArch
set WZLOC=C:\PROGRA~2\WINZIP

:: Caution: Use a drive where sufficient disk space is available for this setting.
set WORKDIR=K:\

:: DO NOT CHANGE ANYTHING BEYOND THIS POINT

set COMPCMD="%WZLOC%\WZZIP.EXE" -m -ex -b%WORKDIR% -a 

for /F "USEBACKQ DELIMS==" %%t in (`dir %INPBKP%\*.dat /a/s/b`) do @%COMPCMD% %%t.zip %%t
for /F "USEBACKQ DELIMS==" %%t in (`dir %OUTBKP%\*.dat /a/s/b`) do @%COMPCMD% %%t.zip %%t

endlocal

What this file does is, it zip all the .dat files in the folders. What I want to achieve is it shall zip all the folders.

Update:

I have changed the file content and now it looks like this. Its not working. If I try to run on command prompt it says- 'The system cannot find the file specified'.

@echo off
setlocal
set INPBKP=C:\Data\Backup\VideosArch
set OUTBKP=C:\Data\Backup\TextualArch
set WZLOC=C:\PROGRA~2\WINZIP

:: Caution: Use a drive where sufficient disk space is available for this setting.
set WORKDIR=C:\

:: DO NOT CHANGE ANYTHING BEYOND THIS POINT

set COMPCMD="%WZLOC%\WZZIP64.EXE" -r -p -m -ex -b%WORKDIR%

for %%s in ("%INPBKP%" "%OUTBKP%") do for /F "DELIMS==" %%t in ('dir %%s /ad /b') do %COMPCMD% %%~nt.zip "%%~s\%%t"

endlocal

Upvotes: 0

Views: 3115

Answers (1)

Magoo
Magoo

Reputation: 80193

set COMPCMD="%WZLOC%\WZZIP.EXE" -r -p -m -ex -b%WORKDIR%

for /F "DELIMS==" %%t in ('dir %INPBKP% /ad /b') do %COMPCMD% "%INPBKP%\%%~nt.zip" %INPBKP%\%%t

Adding -r -p to the compress command should gather and retain all the files and directories.

By executing dir /b /ad, %%t will be assigned the directorynames from the target directory. %%~nt selects simply the name part. The usebackq is not required if single quotes are used.

You could possibly also use

for %%s in ("%INPBKP%" "%OUTBKP%") do for /F "DELIMS=" %%t in ('dir /ad /b %%s') do %COMPCMD% "%%~s\%%~nt.zip" "%%~s\%%t"

%%s containing the quoted directoryname and %%~s unquoted (in case of separators in directorynames)

I haven't tested this, so I'd suggest you try it on some disposible test directories first.

[edit - to insert the destination directory for the zip file] [also changed delims== to delims= (cosmetic - change = as delimiter to no delimiter)]

Upvotes: 0

Related Questions