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"
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").
The below-given answers are both close to what I need. But for some unknown reasons, sometimes the batch file, when I give a parameter, just won't do anything and bring me back to prompt.
I haven't written a batch file for about 15 years, so I completely forgot about the logic behind, etc.
Someone can help, please?
It seems, without any certitude, that the batch file script can't handle folder names with space character. Can one of you confirm and propose a solution for counter this please?
I'm going completely blind here... Thanks for your help! =)
I need the solution to be fully recursive as I don't know the directory structure against which this will be used. Sorry for not having mentioned before.
I have been inspired by all of your answers and other's to come up with the following (which works at 98%):
@echo off setlocal enableextensions enabledelayedexpansion rem rem Display instructions when no parameter is given. rem if "%1" equ "" ( echo Syntaxe : od.bat ^<directory mask>^ echo Exemple : od.bat * goto :Eof ) rem rem Setting the PATH environment variable for this batch file for accessing 7za.exe. rem path=c:\temp;%PATH% rem rem Removing quotes from the given command line parameter path. rem set root=%1 set root=%root:~%1 set root=%root:~0,-1% rem Searching directory structure from root for subfolders and zipfiles, then extracting the zipfiles into a subfolder of the same name as the zipfile. for /F "delims==" %%d in ('dir /ogne /ad /b /s %root%') do ( echo Traitement du dossier : "%%d" for /F "delims==" %%f in ('dir /b "%%d\*.zip"') do ( rem Getting filename without extension. set subfolder=~n%f mkdir "%%d\%subfolder%" rem Extracting zipfile content to the newly created folder. 7za.exe e "%%d\%%f" -o"%%d\%subfolder%" ) ) :Eof endlocal
The only trouble I get is that it wants to create, to stick with my example,
File5 subfolder in order to extract the zipfile into. This is no problem when extracting the actual
File5.zip file. But when it digs deeper in the directory structure to subfolders, since it has gotten the last
File5 filename, it wants to create File5 subfolder into
Folder 1\Folder A, for example, to extract
File1.zip. Here's an example of structure:
║ ║═════ Folder 1 ║ ║ ║ ║══════ Folder A ║ ║ ║ ║═════ File1.zip ║ ║═════ Folder 5 ║ ║═════ File5.zip
So, considering the above-illustrated structure, it wants to create
Folder A, when it should intend to create
Thanks for your support and help! =)
Anyone's help is gratefully appreciated.
I have accepted @Dennis Williamson's answer since his solution fulfills what I have asked on the first place. Otherwise, if one is looking for a fully recursive solution, please follow the link provided in my own answer, which will bring you to the StackOverflow website.