0

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.

Will Marcouiller
  • 256
  • 2
  • 5
  • 16

4 Answers4

2

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"
Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151
  • 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 08 '10 at 19:17
  • @Will: I can't see any reason it would do that. – Dennis Williamson Jun 08 '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 08 '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 08 '10 at 20:55
  • @Dennis: Thanks for the edit. I shall try it as soon as I am in the office. – Will Marcouiller Jun 09 '10 at 10:36
  • @Dennis: This now works, but for a limited amount of files, though. When I have a subfolder in `%%d`, I want it to dig into this subfolder, what it actually doesn't. When I only have a folder containing a zipfile, it works. But not when I have a subfolder. Any way around this? – Will Marcouiller Jun 09 '10 at 13:11
  • @Will: How many levels deep? Just two or completely recursive? Are there zip files in each %%d *and* in each level of subdirectories below them? Your original question implies one directory with one level of subdirectories, each of which contain zip files and the zip files should be unzipped into the same directory in which they exist. That's what my script does. Your answers to my followup questions will help me modify it. Thanks. – Dennis Williamson Jun 09 '10 at 13:53
  • Idealy, I would say completely recursive, since I don't know the directory structure against which it will be used. Sorry for not having mentionned this more clearly. – Will Marcouiller Jun 09 '10 at 14:47
  • Very old question/answer at this point, but thank you for this answer. I don't do much batch scripting (I avoid Windows wherever possible), but needed to write a script which does a thing in each directory. Your syntax was the only complete example for looping over sub directories given a variable. I tried sooooo many different ways of doing this from soooo many different resources. Thank you. – blthayer Apr 02 '20 at 16:40
1

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
DeckerDK
  • 13
  • 1
  • 4
0

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

mfinni
  • 36,144
  • 4
  • 53
  • 86
  • 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 09 '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 : http://en.wikipedia.org/wiki/Resource_Kit – mfinni Jun 09 '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 09 '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 09 '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 09 '10 at 17:31
0

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.

Will Marcouiller
  • 256
  • 2
  • 5
  • 16