1

I have a root directory with many different sub-directories that have .zip files in them (example below). I need a batch/.cmd/etc to unzip the zip files in their sub directories and rename the extracted file (a .tif) the same as the zip file. I have researched around and found the following code, however this will only work when placed in the sub-directories. Not the parent folder. How to I make this code work through all the sub-directories as explained?

Example File Structure

C:\This\Is\Random\Parent\Sub\Sub\1234.zip {ABCD.tif}

What I'm Looking for C:\This\Is\Random\Parent\Sub\Sub\1234.tif

set work=%temp%\%random%%random%%random%%random%
mkdir "%work%" || goto :eof

for %%A in ('dir /b /s *.zip *.rar') do (
"%ProgramFiles%\7-Zip\7z.exe" e -o"%work%" "%%~A"
for %%F in ("%work%\*") do move "%%~F" "%%~nA%%~xF"
)

rmdir "%work%"
1

I was able to make the following changes to achieve what I was looking for

set work=%temp%\%random%%random%%random%%random%
mkdir "%work%" || goto :eof

for /F %%I IN ('dir /b /s *.zip *.rar') do (
"%ProgramFiles%\7-Zip\7z.exe" x -o"%work%" "%%~I"
for %%F in ("%work%\*") do move "%%~F" "%%~nI%%~xF"
)

rmdir "%work%"

Only issue now is ".zip" is included in the name of the extracted .tif. Help resolving that would be appreciated

  • I posted an earlier version of the code. The "A" is replaced with "I" in the second loop as "%%-nI%%~xF" this makes the tif unzip to the parent dir with the correct extension of just .tif . "%%I%%xF" extracts them to the correct subfolders but brings the extension .zip.tif. – gdink1020 Oct 21 '17 at 15:13
  • That saves them correctly as just the .tif extension, but does not preserve the file structure and unzips them all to the parent directory – gdink1020 Oct 21 '17 at 15:29
  • In substituting "%%~FnI.tif" for "%%~nI%%~xf" in the original code the result is the .tif is extracted in the correct subfolder with the .zip.tif file extension. – gdink1020 Oct 21 '17 at 16:41
  • I ran the code exactly how you have it in the last link link. It ran and did not move the extracted file into the correct folder. there was and error. (attached) link – gdink1020 Oct 22 '17 at 14:28
  • I ran the last version you suggested; again it's giving the The system cannot find the file specified. error. In clarification of the standards, The data originates on a website, I wrote a python scrape that downloads the .zip files (Over 500,000 .zip files). The python script creates directories based on the Public Land Survey, first by Township then Section where there could be multiple unique .zip's. ex.) C:\\data\Township\Section\ABC.zip. Within the .zip there is 1 tif file named 1234.tif the file needs to be extracted in the correct section folder with the name ABC.tif – gdink1020 Oct 23 '17 at 3:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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