Iterating through folders and files in batch file?

Posted by Will Marcouiller on Stack Overflow See other posts from Stack Overflow or by Will Marcouiller
Published on 2010-06-08T17:19:55Z Indexed on 2010/06/08 17:22 UTC
Read the original article Hit count: 380

Filed under:
|
|
|
|

Here's my situation. A project has as objective to migrate some attachments to another system.

These attachments will be located to a parent folder, let's say "Folder 0" (see this question's diagram for better understanding), and they will be zipped/compressed.

I want my batch script to be called like so:

BatchScript.bat "c:\temp\usd\Folder 0"

I'm using 7za.exe as the command line extraction tool.

What I want my batch script to do is to iterate through the "Folder 0"'s subfolders, and extract all of the containing ZIP files into their respective folder.

It is obligatory that the files extracted are in the same folder as their respective ZIP files. So, files contained in "File 1.zip" are needed in "Folder 1" and so forth.

I have read about the FOR...DO command on Windows XP Professional Product Documentation - Using Batch Files.

Here's my script:

@ECHO OFF

FOR /D %folder IN (%%rootFolderCmdLnParam) DO 
    FOR %zippedFile IN (*.zip) DO 7za.exe e %zippedFile

I guess that I would also need to change the actual directory before calling 7za.exe e %zippedFile for file extraction, but I can't figure out how in this batch file (through I know how in command line, and even if I know it is the same instruction "cd").

Anyone's help is gratefully appreciated.

© Stack Overflow or respective owner

Iterating through folders and files in batch file?

Posted by Will Marcouiller on Server Fault See other posts from Server Fault or by Will Marcouiller
Published on 2010-06-08T17:09:35Z Indexed on 2010/06/08 17:12 UTC
Read the original article Hit count: 380

Here's my situation. A project has as objective to migrate some attachments to another system.

These attachments will be located to a parent folder, let's say "Folder 0" (see this question's diagram for better understanding), and they will be zipped/compressed.

I want my batch script to be called like so:

BatchScript.bat "c:\temp\usd\Folder 0"

I'm using 7za.exe as the command line extraction tool.

What I want my batch script to do is to iterate through the "Folder 0"'s subfolders, and extract all of the containing ZIP files into their respective folder.

It is obligatory that the files extracted are in the same folder as their respective ZIP files. So, files contained in "File 1.zip" are needed in "Folder 1" and so forth.

I have read about the FOR...DO command on Windows XP Professional Product Documentation - Using Batch Files.

Here's my script:

@ECHO OFF

FOR /D %folder IN (%%rootFolderCmdLnParam) DO 
    FOR %zippedFile IN (*.zip) DO 7za.exe e %zippedFile

I guess that I would also need to change the actual directory before calling 7za.exe e %zippedFile for file extraction, but I can't figure out how in this batch file (through I know how in command line, and even if I know it is the same instruction "cd").

Anyone's help is gratefully appreciated.

© Server Fault or respective owner

Related posts about batch

Related posts about batch-file