Page 1 of 1

extract .7z subdirectories into the same folder of bat file

Posted: 16 Feb 2023 19:03
by skylinekiller
I want to extract all the .7z files' subdirectories (skipping the parent directory) into the same directory as my bat file. If files/folders exist, it overwrites without prompting
Example of now:

Code: Select all

    │   extract.bat
    │
    ├───Steven Folder
    │       Steven.7z   (parent folder is Steven and has subdirectories of Pictures and Resume)
    │
    ├───John Jacobs Folder
    │       John Jacobs.7z   (parent folder is John Jacobs and has subdirectories of Pictures and reports)
    │
    └───Jay Cloud Folder
             Jay Cloud .7z
Example of what I want:

Code: Select all

extract.bat
Pictures 'folder'
Resumes 'folder'
Reports 'folder'
 
The script I am using below only extracts the .7z file parent directory into the same directory where the .7z file is.

Code: Select all

for /F "delims=" %%I IN ('dir /b /s/a-d *.7z') DO (

    "C:\Program Files\7-zip\7z.exe" x -y -o"%%~dpI" "%%I" 
)

Re: extract .7z subdirectories into the same folder of bat file

Posted: 17 Feb 2023 06:37
by Compo
What happens when you change %%~dpI to %%~dp0?

Re: extract .7z subdirectories into the same folder of bat file

Posted: 17 Feb 2023 08:32
by skylinekiller
Thank you for taking a look at it. I I make the change you suggested, It creates a folder in the same directory as .bat file called "%~dp0" and all the .7z files are extracted into that folder BUT it still extracts the parent folders of all the .7z files. I am trying to get it to OMIT the parent folder, as shown in my initial post.

Re: extract .7z subdirectories into the same folder of bat file

Posted: 17 Feb 2023 11:32
by kwsiebert
That almost sounds like you entered it on the command line, rather than running the batch file. It's going to behave differently in each case. Alternately, did you accidentally type a third %?

This is not the place for help with the 7zip application, but check what the command line switches being used do. Also, be aware that ZIP archives might have been created with a folder structure inside them in the first place, which could be getting extracted.

Re: extract .7z subdirectories into the same folder of bat file

Posted: 17 Feb 2023 11:57
by skylinekiller
Below is the code with the change you suggested. I guess where it extracts isn't as crucial as omitting the first directory when extracting and overwriting everything as it extracts. That's the main challenge getting it to extract the .7z from the second directory.

Code: Select all

for /F "delims=" %%I IN ('dir /b /s/a-d *.7z') DO (

    "C:\Program Files\7-zip\7z.exe" x -y -o"%%~dp0" "%%I" 
)

Re: extract .7z subdirectories into the same folder of bat file

Posted: 18 Feb 2023 00:33
by skylinekiller
kwsiebert wrote:
17 Feb 2023 11:32
That almost sounds like you entered it on the command line, rather than running the batch file. It's going to behave differently in each case. Alternately, did you accidentally type a third %?

This is not the place for help with the 7zip application, but check what the command line switches being used do. Also, be aware that ZIP archives might have been created with a folder structure inside them in the first place, which could be getting extracted.
That is correct "folder structure inside them in the first place, which could be getting extracted" I want it to extract everything after the first folder in the zip