Home > Back-end >  Windows: command line how to unzip file and rename it's content with zipped file name in curren
Windows: command line how to unzip file and rename it's content with zipped file name in curren

Time:04-07

I would appreciate if I got help on this, I have the following script that unzip and rename the content with zipped file name, however it works in the current folder only, I need it work in subfolders as well!

@Echo off
for /F %%I IN ('dir /b  *.zip *.rar') DO (
     "C:\Program Files\7-Zip\7z.exe" x -o"%%~dpI" "%%I" -aoa
    for /F "delims=" %%f in ('dir  /b *.html') do (
        ren "%%f" "%%~nI.html"
    ) 
)

it should search for any .zip file and unzip it and rename it's content with the zip file, example:

abc.zip ---> contains xyz.html

the result should be ----> abc.html

CodePudding user response:

I have found a work-around, it's not the best but solve the issue, you need to create a script that target the desired directory and run other script that works in the current directory only.

script target desired dircotry:

cd /D D:\Target\
Call C:\Scripts\UnzipRenameScript.cmd

and here the script that do the work (UnzipRenameScript):

@Echo off
for /F %%I IN ('dir /b  *.zip *.rar') DO (
    del "%%~nI.html"
     "C:\Program Files\7-Zip\7z.exe" x -o"%%~dpI" "%%I" -aoa
    for /F "delims=" %%f in ('dir  /b *.html') do (
        ren "%%f" "%%~nI.html"
    del "%%~nI.zip"
    ) 
)
  • Related