# Batch to Zip Files within Folder Structure



## jymoo (May 22, 2011)

I have a concern on creating ZIP files using WinRAR using a batch program. I have a main folder and multiple subfolders within that folder.

Main 
- Module 1
- Chapter 1.doc (inside module 1)
- Chapter 2.doc (inside module 1)
- Module 2
- Chapter 1.doc (inside module 2)
- Chapter 2.doc (inside module 2)
- Module 3
- Sub-module (inside module 3)
- Chapter 1.doc (inside sub-module 1)
- Chapter 2.doc (inside sub module 1)

The structure is something like this. I need to Zip the doc files within each module as 'Folder_Name.zip' and place it under Main folder. In case of a submodule, it should be 'SubModule_Name.zip', placed under 'Module/Sub-module' folder. After creating the zip archives, i need to delete the doc files.
So, in the above case, it should be-

Main
- Module 1.zip
- Module 2.zip
- Module 3
- Sub-module.zip (inside Module 3)

I have created a batch file as follows:


```
@echo off
del /a /f /s /q :H *.tmp
echo Temp files deleted
pause
CD\
CD d:\testcopy\sub
C:\progra~1\WinRAR\RAR a -ep d:\testcopy\sub.zip
CD d:\testcopy\sub2
C:\progra~1\WinRAR\RAR a -ep d:\testcopy\sub2.zip
echo Files zipped
pause
del /a /f /s /q :H *.doc
echo Word files deleted
pause
```
But in this, the structure should be definite always and I need to write about 100 lines to define the folder structure. I need a solution, which works within any 'Main' folder, irrespective of its structure. Please help. If you need further info, please ask.


----------



## kilonox (Dec 3, 2010)

This should be under programming...


----------



## Laxer (Dec 28, 2010)

I requested the move.

In my opinion DOS!=Design 

It doesn't even have a gui


----------



## jymoo (May 22, 2011)

No way eh? :4-dontkno

I dont need a GUI. It's as simple as that, just do the job of zipping with foldername.zip. 

It should be fine to recreate a directory structure using xcopy command and then zip and copy the zip files to their corresponding folders in the counterpart. But how to give it a variable name, based on the parent folder is my rubberball. 

:sigh::sigh: pls techiesss!!


----------



## Ninjaboi (Dec 27, 2010)

Hello jymoo, welcome to TSF!

You should try taking a look at these links:

How to get folder path from file path with CMD - Stack Overflow
Win/Batch: How to get the folder name from path - computer help forum

That should answer your 'rubberball' question as to get the parent folder name into a variable. Sorry I'm not much more help than that, I don't use batch very often.


----------



## AlbertMC2 (Jul 15, 2010)

Hi

You can try this - it is not perfect but it might help:


```
FOR /D /r %%G in (*) DO if exist %%G\*.doc RAR a -ep %%G.zip %%G\*.doc
FOR /D /r %%H in (*) DO if exist %%H\*.zip move %%H\*.zip c:\temp
```
The first line goes through all the folders and subfolders looking for *.doc
If a .doc is found it zips the whole folder/subfolder and gives it the name of the folder/subfolder
Unfortunately it creates the zip file under the containing folder (parent folder).
The second line then goes through all the folders again and moves all the zip files from the corresponding folder to c:\temp
The batch file has to be saved under the upper most parent folder which in your case would be MAIN.
I have also put the RAR.exe in my path variable so I don't have to call "C:\progra~1\WinRAR\RAR" every time but that should work as well.


----------



## jymoo (May 22, 2011)

Wow Wow! Thank you very much AlbertMC2. I'm sure I have a way out of the maze now. You have given me the exact hint i needed. I've been a lil busy these days. Will let you all know the results soon. Thank youuuuuuuuuu! 

Thanks to Ninjaboi too for the hint. i will check it soon.


----------

