Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question

4 Answers 4

up vote 1 down vote accepted

You can use PUSHD and POPD:

@echo off
set rootFolderCmdLnParam=%1
FOR /D %%d IN (%rootFolderCmdLnParam%\*) DO pushd %%d & (FOR %%z IN (*.zip) DO 7za.exe e "%%z") & popd 

Call like this:

BatchScript.bat "c:\temp\usd\Folder 0"
share|improve this answer
    
This seems to route to the root drive directory. It succeeded once, and extracted a zip file I had in C:\Open\Projects\[...]\WindowsApplication1.zip instead of staying inside the folder I had specified in parameter: C:\Temp\usd. Any idea? –  Will Marcouiller Jun 8 '10 at 19:17
    
@Will: I can't see any reason it would do that. –  Dennis Williamson Jun 8 '10 at 19:39
    
@Dennis Williamson: Can it be that folder and file names containing a space character are problematic in batch file scripts? –  Will Marcouiller Jun 8 '10 at 19:43
    
@Will: Yes, I'm sorry, there should be quotes around the variable for 7za.exe (cd, pushd and popd don't care). I'll edit my answer. –  Dennis Williamson Jun 8 '10 at 20:55
    
@Dennis: Thanks for the edit. I shall try it as soon as I am in the office. –  Will Marcouiller Jun 9 '10 at 10:36

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.

REM @echo off
setlocal enableextensions enabledelayedexpansion

  if /I "%1" EQU "" (
    echo Syntax: %0 ^<directory-mask^>
    echo Example:  %0 *
    goto :EOF
  )

  for /D %%a in (%1) do call :ProcessFolder "%%a"
  goto :EOF

:ProcessFolder
  rem Changing working directory to '%1'. See 'HELP PUSHD'
  pushd "%1"
  for %%b in (*.zip) do call :ProcessFile "%%b"
  popd
  goto :EOF

:ProcessFile
  rem Extract only filename-without-extension from %1 argument. See 'HELP FOR'
  set SUBFLDR=%~n1

  mkdir "%SUBFLDR%"
  pushd "%SUBFLDR%"

  echo Put processing here, like   7za.exe e "%1"

  popd
  goto :EOF
share|improve this answer
    
Would it be hard to make your solution fully recursive? Otherwise, it does what I need, except for recursiveness. =) Thanks! =) –  Will Marcouiller Jun 9 '10 at 15:15

You could also use FORFILES. EXE with the /S switch. This is also in the Resource Kit.

share|improve this answer
    
I haven't been able to get or to download the resource kit you're telling me about. Is there any place I can download it? I have searched a little over the Internet and in Microsoft's website and still can't find it. I'm sure this could be useful. –  Will Marcouiller Jun 9 '10 at 10:33
    
Really? If you google for "windows resource kit", the first link I see is for the Win2k3 Resource Kit download link. It's got its own Wikipedia link, FFS : en.wikipedia.org/wiki/Resource_Kit –  mfinni Jun 9 '10 at 13:02
    
It is for Windows 2003. I'm currently testing for Windows XP, but will probably be used on a Windows 2000 system. I just yet can't say. –  Will Marcouiller Jun 9 '10 at 13:31
    
Oh my god. From the Wikipedia article that I linked : "has since been released for every Windows version, except for Windows Me, Windows CE and Windows 98 Second Edition." You can find it for every OS you care about. Plus, the binaries for each version will run on any version, they just might not work properly - things that look for AD wouldn't work on NT 4.0, for example. –  mfinni Jun 9 '10 at 13:52
    
I shall take an eye out this when I have more spare time. But as for now, I feel like I'd be safer with a simple plain batch file. –  Will Marcouiller Jun 9 '10 at 17:31

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.