0

The following code works great at extracting any zip-type files in current directory and subdirectories below. It will create a directory with the name of the zip file and extract the contents into that directory. This is great, but if you have test.zip and test.rar in the same directory, this is a problem if you don't want the contents of the rar and zip outputting to the same "test" directory. I would like it to extract to a directory with the zip name including extension. In other words, extract test.zip into a folder called "test.zip" and test.zip to a folder called "test.rar" -- to avoid file name collisions.

for /F "DELIMS=" %%I IN ('dir /b /s *.zip *.rar *.7z') DO ( "C:\Program Files\7-Zip\7z.exe" x -aos -o"%%~dpnI" "%%I" )

I thought this could simply be done by changing -o"%%~dpnI" to -o"%%~dpnxI", but that didn't seem to work. Please help!

6
  • Do you mean "test.zip" to a folder called "test.zip" and "test.rar" to a folder called "test.rar"? Commented Sep 23, 2020 at 9:58
  • Should the original files be deleted after extraction? Commented Sep 23, 2020 at 10:15
  • Ricardo - Yes (folders). No (delete original files). Per someone's comment, and I tested, it is NOT possible to have a folder called "test.zip" and a file called "test.zip" in the same directory. It is a filename collision. Therefore, I am fine with having the extracted folder have a unique appended tag like "test.zip(ext)" -- (ext) meaning extracted. How could I do that? Commented Sep 24, 2020 at 16:22
  • Ricardo, sorry missed your message below. Looks like I can do it like this (thanks!): %%~dpnI%%~xI(ext) Commented Sep 24, 2020 at 16:25
  • Instead of the above, how can I do the following but not have it include the period in the extension? %%~dpnI%%~xI Commented Sep 24, 2020 at 18:27

1 Answer 1

0

I found out that there can't be a folder and file with the same name and extension in the same directory even though one is a file and one is folder. My solution would be to extract test.zip to test_.zip folder (using a underscore in front of the extension so they don't have the same name).

@echo off
:: Put name of folder of extraction here:
set Folder=%userprofile%\desktop\Test

:: Put location of 7z.exe here:
set Seven=C:\Program Files\7-Zip\7z.exe

pushd "%Folder%"
for /F "DELIMS=" %%I IN ('dir /b /a-d /s *.zip *.rar *.7z') DO ("%Seven%" x -aos -o"%%~dpnI_%%~xI" "%%I")
popd

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .