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").
EDIT #1
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?
EDIT #2
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! =)
EDIT #3
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.
EDIT #4
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 File5
in Folder A
, when it should intend to create File1
instead.
Thanks for your support and help! =)
Anyone's help is gratefully appreciated.
EDIT #5
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.
You can use
PUSHD
andPOPD
:Call like this:
Here is a Windows .CMD script which might help you a bit.
It is not a complete solution, but I hope you at least get some ideas from it.
You could also use FORFILES. EXE with the /S switch. This is also in the Resource Kit.
The answer provided by @aphoria following the link below allows the script to be fully recursive, to create a subfolder of the same name of the zipfile, etc.
Here is the link to the answer to the same question on StackOverflow
Thanks to everyone, you all have helped me a lot.