0

I have hundreds of zip and rar file inside a lot of subdirectories and I need to unzip them in folders named as file zip + date of zip file.

now I use this batch script :

FOR /D /r %%F in (".") DO (
pushd %CD%
cd %%F
FOR %%X in (\*.rar \*.zip) DO (
"C:\\Program Files\\7-zip\\7z.exe" e "%%X" -o"%%\~nX"\_"%%\~tX" -aou
)
popd
)

it functions but only under windows server 2019 and changing the format of the date to es. 01.01.23 otherwise it gives me an error because it can't create the folder ( under win 10 home it's impossible to use).

So I need to change the format of %%~tX into something as 01_01_23 so i can use under win 10 .

I improved the script like this :

FOR /D /r %%F in (".") DO (
 pushd %CD%
 cd %%F
 FOR %%X in (\*.rar \*.zip) DO (
  set "d=%%\~tX"
  set "DD=%d:\~0,2%" & set "MM=%d:\~3,2%" & set "YY=%d:\~6,2%"
  "C:\\Program Files\\7-zip\\7z.exe" e "%%X" -o"%%\~nX"\_"%DD%\_%MM%\_%YY%" -aou
 )
 popd
)

and it's ok

But if I add hour :

FOR /D /r %%F in (".") DO (
 pushd %CD%
 cd %%F
 FOR %%X in (\*.rar \*.zip) DO (
  set "d=%%\~tX"
  set "DD=%d:\~0,2%" & set "MM=%d:\~3,2%" & set "YY=%d:\~6,2%"
  set "HH=%d:\~9,6%"
  "C:\\Program Files\\7-zip\\7z.exe" e "%%X" -o"%%\~nX"\_"%DD%\_%MM%\_%YY%.%HH%" -aou
 )
 popd
)

I create an undeletable directory. Why ?

Excuse me but during staging, Ground characters were added that I hadn't typed.
My latest revision is this:

setlocal EnableDelayedExpansion
FOR /D /r %%F in (".") DO (
 pushd %CD%
 cd %%F
 FOR %%X in (*.rar *.zip) DO (
  set "dt=%%~tX" & set "DD=%dt:~0,2%" & set "MM=%dt:~3,2%" & set "YY=%dt:~6,2%"
  "C:\Program Files\7-zip\7z.exe" e "%%X" -o"%%~nX"."!DD!.!MM!.!YY!" -aou
 )
 popd
)

I launch the batch inside the directory that interests me (with all its subdirectories). And I have also this problem : variable %DD% , %MM%, %DD% remain the same despite having entered : setlocal EnableDelayedExpansion.

Thank you very much for everything. This is my first question. I learned a lot. with this version it's all ok for me.

FOR /D /r %%F in (".") DO (
pushd %CD%
cd %%F
FOR %%X in (*.rar *.zip) DO (
setlocal EnableDelayedExpansion
set "dt=%%~tX"
set "DD=!dt:~0,2!" 
set "MM=!dt:~3,2!" 
set "YY=!dt:~6,2!"
set "HH=!dt:~9,2!"
set "MI=!dt:~12,2!"
"C:\Program Files\7-zip\7z.exe" e "%%X" -o"%%~nX"_"!DD!_!MM!_!YY!_!HH!.!MI!" -aou
)
popd
)

P.S. : while trying, I made a syntax error and created folders like "example_.." that I can't delete even with paid software.And if i copy a file inside automatically windows create a new folder with the same name minus "." es: "pippo_.." became "pippo_" with the file inside, and I can delete this new directory but not the first named "pippo_.." And with many file folders I get : Maximum recursion level of setlocal reached, but this does not affect the final result.

Marco
  • 1
  • 2
  • `FOR %%X in (\*.rar \*.zip) DO (` should examine the **root** directory, not the **current** directory, so it should not work as desired, 2019 or not. Please review. Your second and third scripts should also not work properly due to the [delayed expansion trap](https://stackoverflow.com/a/30284028/2128947) – Magoo Apr 19 '23 at 10:34
  • What is `\~` supposed to do? And why are you using ```\\``` as path separators? – Compo Apr 19 '23 at 12:45
  • I'm also not understanding the use of `pushd %CD%`, (which should use doublequotes anyhow), or the matching `popd`. – Compo Apr 19 '23 at 12:55
  • To format code, select the code & press the `{}` formatting button. `PUSHD` saves the current directory & switches to the nominated directory. No quotes are required (but it looks neater). `POPD` returns to the original directory. Hence, `PUSHD "%%F"` should switch to the directory you require - no need for the `cd %%F`. As for the problem with `%MM%` etc,, again you have fallen victim to the [delayed expansion trap](https://stackoverflow.com/a/30284028/2128947) which has been previously answered. You *have not* used `delayedexpansion` in the `set` line, but you *have* in the `7z.exe` line. – Magoo Apr 19 '23 at 14:56
  • @Magoo, the `FOR` loop is recursive and as there's no way of determining the content which would be assigned to the absolute fully qualified path, `%%F`. Whilst in general terms both `CD` and `PushD` can both handle spaces without doublequotes, neither can handle other valid file or directory name characters, like the ampersand. For that reason it isn't just for aesthetics, it is possibly essential and therefore should be always used as best practice. – Compo Apr 19 '23 at 15:15
  • @Compo : fair enough on the quotes, hence in the current instance, `PUSHD "%%F"` because `%%F` may contain poison characters. – Magoo Apr 19 '23 at 15:28
  • I also note that `~6,2` would always return `19` or more likely `20` for you as `YY`. Perhaps you meant to use `~8,2` instead. – Compo Apr 19 '23 at 15:49

0 Answers0