Page 1 of 1

Looping and Zipping of files

Posted: 29 Mar 2011 23:11
by AyakaShota
Hi All,

I am new to Dos Batch. My problem now is that I need to loop through a directory with many subfolders. I need to zip those subfolders (using 7zip) and place the zipped subfolders into another directory.

Before zipping
My main root is C:\Users\ayaka\Desktop\ZipTask\TestingDirectory
Sample Folders:
C:\Users\ayaka\Desktop\ZipTask\TestingDirectory\April2001\temp123\temp123.html
C:\Users\ayaka\Desktop\ZipTask\TestingDirectory\April2001\temp234\temp234.html
C:\Users\ayaka\Desktop\ZipTask\TestingDirectory\May2001\temp345\temp345.html
C:\Users\ayaka\Desktop\ZipTask\TestingDirectory\May2001\temp456\temp456.html

After zipping
C:\Users\ayaka\Desktop\ZipTask\TestDir

The folders should be zip according to the month and year. So, there should be 2 zipped folders, mainly April2001 and May2001 in C:\Users\ayaka\Desktop\ZipTask\TestDir

I had done abit of coding, but it doesn't really work.

Here's the code:

Code: Select all

@ECHO off 


FOR /R C:\Users\ayaka\Desktop\ZipTask\TestingDirectory\ %%G IN (*) DO (


for /f "tokens=1-7 delims=\ " %%i in ("%%G") do (
Set folder=%%o
Set path=%%i\%%j\%%k\%%l\%%m\%%n\%%o

cd C:\Users\ayaka\Desktop\ZipTask\7za920

7za.exe a -t7z %path%\%folder%.7z C:\Users\ayaka\Desktop\ZipTask\TestDir


)

)
@ECHO on


Can anyone help me? Thanks.

Re: Looping and Zipping of files

Posted: 30 Mar 2011 03:44
by AyakaShota
I managed to create the batch file to do it. But I have no idea why the program stopped after the first zipped folder is made.

Here's the new code:

Code: Select all

@ECHO off
FOR /R C:\Users\ayaka\Desktop\ZipTask\TestingDirectory\ %%G IN (*) DO (
FOR /f "tokens=1-7 delims=\ " %%i in ('dir /b /s "%%G"') do (

IF NOT EXIST C:\Users\ayaka\Desktop\ZipTask\TestDir\%%o.7z (

echo %%i\%%j\%%k\%%l\%%m\%%n\%%o

cd C:\Users\ayaka\Desktop\ZipTask\7za920

7za.exe a -t7z C:\Users\ayaka\Desktop\ZipTask\TestDir\%%o.7z C:\Users\ayaka\Desktop\ZipTask\TestingDirectory\%%o
)
)
)
@ECHO on


Output:
C:\Users\ayaka\Desktop\ZipTask\TestDir\April2004.7z

The output should also include this file, but the program did not continue:
C:\Users\ayaka\Desktop\ZipTask\TestDir\May2004.7z

Re: Looping and Zipping of files

Posted: 30 Mar 2011 19:39
by AyakaShota
Finally, I made it. But it will take a very long time if there are alot of folders in each folder and many files in each folder.

Here's the code:

Code: Select all

@echo off

echo.going to execute loop
call:loop
echo.returned from loop
echo.&pause&goto:eof

:loop
FOR /R C:\Users\ayaka\Desktop\ZipTask\TestDir\ %%G IN (*) DO (
echo.going to execute cont
call:cont %%G
echo.returned from cont
)
echo.going back to loop
echo.&pause&goto:eof

:cont
FOR /f "tokens=1-7 delims=\ " %%i in ('dir /b /s "%~1"') do (
echo %%i\%%j\%%k\%%l\%%m\%%n\%%o >> path.txt
IF NOT EXIST C:\Users\ayaka\Desktop\ZipTask\testingFolder\%%o.7z (
cd C:\Users\ayaka\Desktop\ZipTask\7za920
7za.exe a -t7z C:\Users\ayaka\Desktop\ZipTask\testingFolder\%%o.7z C:\Users\ayaka\Desktop\ZipTask\TestingDirectory\%%o
)
)
goto:eof


Anybody has any idea on how to improve this? So that it can be done efficiently?

Re: Looping and Zipping of files

Posted: 24 Apr 2011 20:56
by scienceguru1.bat
not sure (not familiar w/ zipping), but i would quess something along the lines of:

Code: Select all

@echo off
cd "(heading folder location)"
copy /s "*.*" "zip.zip"