Reputation: 243
i have a folder structure in this pattern. I've just shown two sub directories and 2 files in each but generally I have n number of subdirectories at a single level and single level of files (but can be n number of files)under them.
Directory master
subDirectory x:
file1
file2
Directory y:
file 3
file 4
I need to create a windows script, a batch file to run from the master directory and give me two zip files x.zip and y.zip containing their respective files.
I know my script has to use for and zip commands but I am going bonkers trying to get it to work as I can't understand from the syntax of these commands and googling doesnt seem to help.
I found a command like this for %f in ("*.*") do zip "%~nf.zip" "%f"
but it seems to be working only if the files are directly there without subfolders.
Upvotes: 24
Views: 276965
Reputation: 65
Late to the party, but I wanted to add on to another proposed answer that recommended tar
, but the output zip archive could not be read because the format was wrong. In order to resolve this, one must include the argument --archive zip
. Here is an example:
tar -cf path\to\target.zip --format zip path\to\source
Also, if you are using tar
from the command line then you can use argument -a
instead of --format
, and tar
will automatically use the correct format based on the output file's extension ("zip" in this case). Unfortunately, -a
is not available when using tar
in a batch script.
See other format options here from FreeBSD.
Upvotes: 2
Reputation: 111
Starting windows 10 build 17063, you can use TAR. MSDN link
For example, to create a zip file for a directory named folder XYZ that is in inside folder Reports, you can enter the following in the command prompt.
tar -cf XYZ.zip \Reports\XYZ
Upvotes: 6
Reputation: 21
Simple script Its convert all dump folder and subfolder files in desktop as backup.zip
write this below line in backup.bat and execute
"C:\Program Files\7-Zip\7z.exe" a -tzip "C:\Users\shris\Desktop\backup.zip" -r "C:\Users\shris\Desktop\dump\*" -mx5
Upvotes: 0
Reputation: 27026
I like PodTech.io's answer to achieve this without additional tools. For me, it did not run out of the box, so I had to slightly change it. I am not sure if the command wScript.Sleep 12000
(12 sec delay) in the original script is required or not, so I kept it.
Here's the modified script Zip.cmd
based on his answer, which works fine on my end:
@echo off
if "%1"=="" goto end
setlocal
set TEMPDIR=%TEMP%\ZIP
set FILETOZIP=%1
set OUTPUTZIP=%2.zip
if "%2"=="" set OUTPUTZIP=%1.zip
:: preparing VBS script
echo Set objArgs = WScript.Arguments > _zipIt.vbs
echo InputFolder = objArgs(0) >> _zipIt.vbs
echo ZipFile = objArgs(1) >> _zipIt.vbs
echo Set fso = WScript.CreateObject("Scripting.FileSystemObject") >> _zipIt.vbs
echo Set objZipFile = fso.CreateTextFile(ZipFile, True) >> _zipIt.vbs
echo objZipFile.Write "PK" ^& Chr(5) ^& Chr(6) ^& String(18, vbNullChar) >> _zipIt.vbs
echo objZipFile.Close >> _zipIt.vbs
echo Set objShell = WScript.CreateObject("Shell.Application") >> _zipIt.vbs
echo Set source = objShell.NameSpace(InputFolder).Items >> _zipIt.vbs
echo Set objZip = objShell.NameSpace(fso.GetAbsolutePathName(ZipFile)) >> _zipIt.vbs
echo if not (objZip is nothing) then >> _zipIt.vbs
echo objZip.CopyHere(source) >> _zipIt.vbs
echo wScript.Sleep 12000 >> _zipIt.vbs
echo end if >> _zipIt.vbs
@ECHO Zipping, please wait...
mkdir %TEMPDIR%
xcopy /y /s %FILETOZIP% %TEMPDIR%
WScript _zipIt.vbs %TEMPDIR% %OUTPUTZIP%
del _zipIt.vbs
rmdir /s /q %TEMPDIR%
@ECHO ZIP Completed.
:end
Usage:
One parameter (no wildcards allowed here):
Zip FileToZip.txt
will create FileToZip.txt.zip
in the same folder containing the zipped file FileToZip.txt
.
Two parameters (optionally with wildcards for the first parameter), e.g.
Zip *.cmd Scripts
creates Scripts.zip
in the same folder containing all matching *.cmd
files.
Note: If you want to debug the VBS script, check out this hint, it describes how to activate the debugger to go through it step by step.
Upvotes: 1
Reputation: 39
This is the correct syntax for archiving individual; folders in a batch as individual zipped files...
for /d %%X in (*) do "c:\Program Files\7-Zip\7z.exe" a -mx "%%X.zip" "%%X\*"
Upvotes: 3
Reputation: 8771
This is link by Tomas has a well written script to zip contents of a folder.
To make it work just copy the script into a batch file and execute it by specifying the folder to be zipped(source).
No need to mention destination directory as it is defaulted in the script to Desktop ("%USERPROFILE%\Desktop")
Copying the script here, just incase the web-link is down:
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET sourceDirPath=%1
IF [%2] EQU [] (
SET destinationDirPath="%USERPROFILE%\Desktop"
) ELSE (
SET destinationDirPath="%2"
)
IF [%3] EQU [] (
SET destinationFileName="%~n1%.zip"
) ELSE (
SET destinationFileName="%3"
)
SET tempFilePath=%TEMP%\FilesToZip.txt
TYPE NUL > %tempFilePath%
FOR /F "DELIMS=*" %%i IN ('DIR /B /S /A-D "%sourceDirPath%"') DO (
SET filePath=%%i
SET dirPath=%%~dpi
SET dirPath=!dirPath:~0,-1!
SET dirPath=!dirPath:%sourceDirPath%=!
SET dirPath=!dirPath:%sourceDirPath%=!
ECHO .SET DestinationDir=!dirPath! >> %tempFilePath%
ECHO "!filePath!" >> %tempFilePath%
)
MAKECAB /D MaxDiskSize=0 /D CompressionType=MSZIP /D Cabinet=ON /D Compress=ON /D UniqueFiles=OFF /D DiskDirectoryTemplate=%destinationDirPath% /D CabinetNameTemplate=%destinationFileName% /F %tempFilePath% > NUL 2>&1
DEL setup.inf > NUL 2>&1
DEL setup.rpt > NUL 2>&1
DEL %tempFilePath% > NUL 2>&1
Upvotes: -1
Reputation: 5264
No external dependency on 7zip or ZIP - create a vbs script and execute:
@ECHO Zipping
mkdir %TEMPDIR%
xcopy /y /s %FILETOZIP% %TEMPDIR%
echo Set objArgs = WScript.Arguments > _zipIt.vbs
echo InputFolder = objArgs(0) >> _zipIt.vbs
echo ZipFile = objArgs(1) >> _zipIt.vbs
echo CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" ^& Chr(5) ^& Chr(6) ^& String(18, vbNullChar) >> _zipIt.vbs
echo Set objShell = CreateObject("Shell.Application") >> _zipIt.vbs
echo Set source = objShell.NameSpace(InputFolder).Items >> _zipIt.vbs
echo objShell.NameSpace(ZipFile).CopyHere(source) >> _zipIt.vbs
@ECHO *******************************************
@ECHO Zipping, please wait..
echo wScript.Sleep 12000 >> _zipIt.vbs
CScript _zipIt.vbs %TEMPDIR% %OUTPUTZIP%
del _zipIt.vbs
rmdir /s /q %TEMPDIR%
@ECHO *******************************************
@ECHO ZIP Completed
Upvotes: 7
Reputation: 361
I know its too late but if you still wanna try
for /d %%X in (*) do (for /d %%a in (%%X) do ( "C:\Program Files\7-Zip\7z.exe" a -tzip "%%X.zip" ".\%%a\" ))
here * is the current folder. for more options try this link
Upvotes: 11
Reputation: 2027
You're near :)
First, the batch (%%variable) and Windows CMD (%variable) uses different variable naming. Second, i dont figure out how do you use zip from CMD. This is from Linux users i think. Use built-in zip manipulation is not like easy on Win, and even harder with batch scripting.
But you're lucky anyway. I got (extracted to target folder) zip.exe and cygwin1.dll from the cygwin package (3mb filesize both together) and start play with it right now.
Of course, i use CMD for better/faster testing instead batch. Only remember modify the %varname to %%varname before blame me :P
for /d %d in (*) do zip -r %d %d
Explanation:
for /d ...
that matches any folder inside. Only folder ignoring files. (use for /f to filesmatch)
for /d %d in ...
the %d tells cmd wich name do you wanna assign to your variable. I put d to match widh d (directory meaning).
for /d %d in (*) ...
Very important. That suposses that I CD
to desired folder, or run from. (*)
this will mean all on THIS dir, because we use /d
the files are not processed so no need to set a pattern, even if you can get only some folders if you need. You can use absolute paths. Not sure about issues with relatives from batch.
for /d %d in (*) do zip -r ...
Do ZIP is obvious. (exec zip itself and see the help display to use your custom rules). r- is for recursive, so anyting will be added.
for /d %d in (*) do zip -r %d %d
The first %d is the zip name. You can try with myzip.zip, but if will fail because if you have 2 or more folders the second cannot gave the name of the first and will not try to overwrite without more params. So, we pass %d to both, wich is the current for iteration folder name zipped into a file with the folder name. Is not neccesary to append ".zip" to name.
Is pretty short than i expected when start to play with.
Upvotes: 5
Reputation: 80203
for /d %%a in (*) do (ECHO zip -r -p "%%~na.zip" ".\%%a\*")
should work from within a batch.
Note that I've included an ECHO
to simply SHOW the command that is proposed. You'd need to remove the ECHO
keywor to EXECUTE the commands.
Upvotes: 22