简体   繁体   中英

Extract 7-zip files into their containing directory from a batch file?

I have a directory, c:\7zip\test , containing many subdirectories, some containing 7-zip files.

I want to find and extract the 7-zip files into their holding directories from a batch file and then delete those 7-zip files.

@If "%1"=="" (Set pathf=c:\7zip\test\) else (Set pathf=%1) 
@If "%2"=="" (Set lzma2_test=*.7z) else (Set lzma2_test=%2)

for /r "%pathf%" %%f in ("%lzma2_test%") do 7z x -y -mx1 -m0=lzma2 "%%f" -oFolderName
del "%pathf%" + subfolder name (if needed) + 7-zip file name

Instead of FolderName , I should send a directory name, but I do not know how. And the same for del .

Open a command prompt , run for /? and read the output help from top of first page to bottom of last page, especially the section about referencing the loop variable with a modifier like %~dpI with I being the loop variable to reference the drive and path.

@echo off
if "%~1" == "" (set "pathf=c:\7zip\test") else (set "pathf=%~1")
if "%~2" == "" (set "lzma2_test=*.7z") else (set "lzma2_test=%~2")

for /R "%pathf%" %%I in ("%lzma2_test%") do (
    7z.exe x -y -o"%%~dpI" "%%I"
    if not errorlevel 1 del "%%I"
)

The compression switches -mx1 -m0=lzma2 are useless on extracting an archive. The archive contains in header the algorithm used on compression and so extraction works always as long as used 7z.exe supports the archive type and used algorithm on creating the archive.

The archive file is only deleted if the extraction was successful, ie 7x.exe exited not with a value greater or equal 1 which means equal 0 which is the exit code for a successful extraction.

It would be more safe to use the following code which works also if an archive file contains inside another archive file. This code is recommended also for usage on a FAT16, FAT32 or exFAT drive.

@echo off
if "%~1" == "" (set "pathf=c:\7zip\test") else (set "pathf=%~1")
if "%~2" == "" (set "lzma2_test=*.7z") else (set "lzma2_test=%~2")

for /F "eol=| delims=" %%I in ('dir "%pathf%\%lzma2_test%" /A-D /B /S 2^>nul') do (
    7z.exe x -y -o"%%~dpI" "%%I"
    if not errorlevel 1 del "%%I"
)

This code makes sure not extracting an archive file inside an archive file by chance which as it could happen by the first code which iterates of the list of archive files with the list changing on each iteration on extracting an archive file containing an archive file and because of deletion of the successfully extracted archives.

It would be a good idea to test the used batch file first by running it from within a command prompt window with echo left to 7z.exe and if not errorlevel 1 to see what would be executed without really executing it.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /?
  • del /?
  • dir /?
  • echo /?
  • for /?
  • if /?
  • set /?

Read the Microsoft article about Using command redirection operators for an explanation of 2>nul . The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background with %ComSpec% /c and the command line between ' appended as additional arguments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM