# Solved: Loop through dirrectories - Batch File



## jat421 (Jun 14, 2008)

Hi,

I have a batch file that goes into a folder and zips everything inside it. But I have few directories that I want to zip the files under. So I was thinking making a FOR loop to do that. The code doesn't work. What am I doing worng.

I have 10 folders under c:/temp


```
cd c:/temp/
set ZipProg="C:\WZZIP.exe"
for /f "delims=" %%i in ('dir /ad /s /b') do (cd "%%i"
for /f %%a in ('dir /a /on /b "*.*"') do (%ZipProg% "%%~na.zip" "%%a"))
del *.tif
```
Thanks for all the help!!


----------



## TheOutcaste (Aug 8, 2007)

You're using forward slashes instead of backslashes in the first CD command.
Try *cd c:\temp*
The last backslash isn't needed.


----------



## TheOutcaste (Aug 8, 2007)

Note that if there are individual files in the root of each folder (%%i), a separate zip file will be created for each file containing just the one file.
This will also fail if any of the files/folders have spaces in the name.
To work with spaces, you need to add "Delims=" to the 2nd For loop

```
for /f [B][COLOR=Red]"Delims="[/COLOR][/B] %%a in ('dir /a /on /b "*.*"') do (%ZipProg% "%%~na.zip" "%%a"))
```
With the following folder structure, with each folder containing two files:
testfile.txt
test file2.txt


```
C:.
&#9500;&#9472;&#9472;&#9472;Test1
&#9474;   &#9500;&#9472;&#9472;&#9472;A_Profile
&#9474;   &#9474;   &#9500;&#9472;&#9472;&#9472;Profile 1
&#9474;   &#9474;   &#9492;&#9472;&#9472;&#9472;Profile1
&#9474;   &#9492;&#9472;&#9472;&#9472;B_Profile
&#9474;       &#9500;&#9472;&#9472;&#9472;Profile 1
&#9474;       &#9492;&#9472;&#9472;&#9472;Profile1
&#9492;&#9472;&#9472;&#9472;Test2
    &#9500;&#9472;&#9472;&#9472;A Profile
    &#9474;   &#9500;&#9472;&#9472;&#9472;Profile 1
    &#9474;   &#9492;&#9472;&#9472;&#9472;Profile1
    &#9492;&#9472;&#9472;&#9472;B Profile
        &#9500;&#9472;&#9472;&#9472;Profile 1
        &#9492;&#9472;&#9472;&#9472;Profile1
```
You'll get the following:
In Test1:


*A_Profile.zip* which contains the two files, but not the subfolders
*B_Profile.zip* which contains the two files, but not the subfolders
*testfile.zip* which contains testfile.txt
In Test1\A_Profile


*Profile1.zip* which contains the two files
*testfile.zip* which contains testfile.txt
In Test1\A_Profile\Profile1


*testfile.zip* which contains testfile.txt
In Test1\A_Profile\Profile 1


*testfile.zip* which contains testfile.txt
and so on.
Not sure if that's what you really want.


----------



## jat421 (Jun 14, 2008)

Thanks for helping me. You are right all the files under each folder will be zipped separately. 

What I want to do is that the script should cd into each folder under c:/temp and zip the files in each folder. The script I have works for the zipping part if I take off the first FOR loop that is intended to go inside each folder. So I need help with the first FOR loop that will go inside each folder and the second FOR loop will zip the files separately in each folder. 

Hope this make sense. Thanks!!!


----------



## TheOutcaste (Aug 8, 2007)

This should do it. I added the */A-D* to the second loop to exclude directories so it will list just the files.
Works in my Test folders for files/folders with our without spaces, and only zips files (1 file per zip), not folders

```
PushD C:\Temp
Set ZipProg="C:\WZZIP.exe"
For /F "Delims=" %%i In ('Dir /AD /S /B') Do (CD "%%i"
For /F "Delims=" %%a In ('Dir /A-D /ON /B "*.*"') Do (%ZipProg% "%%~na.zip" "%%a"))
PopD
```


----------



## jat421 (Jun 14, 2008)

That worked great!!!!!!!!... Thanks a lot!!!..Batch files are awesome!!!...This is gonna make my life easier at work  thanks to you!


----------



## TheOutcaste (Aug 8, 2007)

You're Welcome! Glad to help.

If your issue has been resolved you (and ONLY you) can mark this thread Solved by using the Mark Solved button at the Top Left of this thread (above the first post) 










Jerry


----------



## jat421 (Jun 14, 2008)

Hi,

Sorry to open this old thread. I am having only one issue with the batch file that I just noticed today. For Example I have

Folder1
Folder2
Folder3
Folder4

And inside each folder I have a folder called "Working" folder. So basically the files are in Folder1, Folder2, Folder3 and Folder4 and they get zipped and put inside each Working Folder. Now the script is also looking under the Working folder that are already zipped. Is there anyway to have the script not look at the Working Folder under each of the folders. Thanks!!


----------



## Squashman (Apr 4, 2003)

winzip cmd line has an exclude switch. Not sure if it is just for files though. I do something similar at work but I have never looked at excluding folders. only files.


----------



## Squashman (Apr 4, 2003)

From the help file


> x<filename> Exclude the specified file(s) from the zipping process. Wildcards are allowed.
> wzzip -x*.txt d:\temp\test.zip *.*
> Creates test.zip and includes all files in the current folder except .txt files.
> 
> ...


----------



## TheOutcaste (Aug 8, 2007)

TheOutcaste said:


> ```
> PushD C:\Temp
> Set ZipProg="C:\WZZIP.exe"
> For /F "Delims=" %%i In ('Dir /AD /B') Do (CD "%%i"
> ...


Just remove the */S* from the first For loop as I did above. Then it will only process the first level of folders:
C:\Temp\Folder1
C:\Temp\Folder2
C:\Temp\Folder3
C:\Temp\Folder4
but not
C:\Temp\Folder1\Working
C:\Temp\Folder2\Working
C:\Temp\Folder3\Working
C:\Temp\Folder4\Working


----------



## jat421 (Jun 14, 2008)

k great, I will give that a try. Let you know how it goes. Thanks!!


----------



## jat421 (Jun 14, 2008)

Hi TheOutcaste,

I spoke too soon. It only loops through one folder under c:\temp if I remove /S. Any idea. Really apprreciate your help!. Thanks!


----------



## Squashman (Apr 4, 2003)

Add one line of code.

```
PushD C:\Temp
Set ZipProg="C:\Program Files\WinZip10\WZZIP.exe"
For /F "Delims=" %%i In ('Dir /AD /B') Do (
	CD "%%i"
	For /F "Delims=" %%a In ('Dir /A-D /ON /B "*.*"') Do (%ZipProg% "%%~na.zip" "%%a")
	[color=red]CD ..[/color]
)
PopD
```


----------



## TheOutcaste (Aug 8, 2007)

Thanks for catching my error Squashman, I did leave out the *cd ..* part. My bad for copying from the previous post instead of from the actual file.


----------



## Squashman (Apr 4, 2003)

TheOutcaste said:


> Thanks for catching my error Squashman, I did leave out the *cd ..* part. My bad for copying from the previous post instead of from the actual file.


You are still the Batch Man in my book! :up:


----------



## jat421 (Jun 14, 2008)

Yea that makes sence, cd out of the dir once the zip is done . Thanks you guys, that worked awsome!!!!.


----------

