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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
skylinekiller
Posts: 4
Joined: 15 Feb 2023 22:36

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

#1 Post by skylinekiller » 16 Feb 2023 19:03

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" 
)

Compo
Posts: 599
Joined: 21 Mar 2014 08:50

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

#2 Post by Compo » 17 Feb 2023 06:37

What happens when you change %%~dpI to %%~dp0?

skylinekiller
Posts: 4
Joined: 15 Feb 2023 22:36

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

#3 Post by skylinekiller » 17 Feb 2023 08:32

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.

kwsiebert
Posts: 42
Joined: 20 Jan 2016 15:46

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

#4 Post by kwsiebert » 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.

skylinekiller
Posts: 4
Joined: 15 Feb 2023 22:36

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

#5 Post by skylinekiller » 17 Feb 2023 11:57

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" 
)

skylinekiller
Posts: 4
Joined: 15 Feb 2023 22:36

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

#6 Post by skylinekiller » 18 Feb 2023 00:33

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

Post Reply