• 0

7zip in a batch file


Question

Hi,

I have a batch file that runs each day and downloads about 20 .zip files from a network share to a server which then extracts them.. before continuing with other bits and bobs

the copying is carried out by ROBOCOPY, and the extraction done by 7zip

this was all working fine til yesterday when i realised my process was falling over every time. Turns out one of the .zip files was corrupt and when it tried to extract the batch file just bombs out

7zip has a file integrity check option but i cant for life of me figure out how to get the result of this check into something like an IF THEN ELSE execute the extract part

options i have considered:

1. only copy correct files with robocopy (cant work out how to check integrity, or even if its possible)

2. do a crc on the files - not really useful as i dont know what the results should be

3. use the 7zip t flag in some manner - 7z.exe t filename.zip and generate an ERRORLEVEL in the batch... bit lost on this one??????

5. batch file try-catch 'hack' - ok i was desperate at this point... :)

can anyone figure out a clever method of making this work? i will be eternally grateful because its starting to really **** me off... :p

perhaps i could run a check, output the successful results to a file and then only extract the filenames that are contained in that file??

thanks all

a frustrated chris :|

Link to comment
https://www.neowin.net/forum/topic/622510-7zip-in-a-batch-file/
Share on other sites

5 answers to this question

Recommended Posts

  • 0

7z t -r filename.zip

might work. If you don't specify a recurse parameter, and your archive has directories inside it, it will not generate an error if the files and directories at the top level of the archive are OK.

Other than that, I have no ideas. :p

I wonder... Could it be the extraction process that is messing things up? You might consider that option, in which case you could consider using something like PicoZip to extract the files and compare the results.

  • 0
  BGM said:
3. use the 7zip t flag in some manner - 7z.exe t filename.zip and generate an ERRORLEVEL in the batch... bit lost on this one??????

Try something like:

@echo off
REM *
REM Test zip
REM *
7za.exe t filename.zip
REM *
REM 0 = no error, else error.
REM *
IF %ERRORLEVEL% == 0 ( GOTO EXTRACT ) ELSE ( GOTO ERROR )

:EXTRACT
echo.
echo Extracting zip...
echo.
GOTO END

:ERROR
echo.
echo Error: %ERRORLEVEL%
echo.

:END

  Exit Codes from 7-Zip said:
7-Zip returns the following exit codes:

Code Meaning

0 No error

1 Warning (Non fatal error(s)). For example, one or more files were locked by some other application, so they were not compressed.

2 Fatal error

7 Command line error

8 Not enough memory for operation

255 User stopped the process

  • 0
  rpgfan said:
I wonder... Could it be the extraction process that is messing things up? You might consider that option, in which case you could consider using something like PicoZip to extract the files and compare the results.

its possible i suppose, i did manage to atleast open the file with winrar...

it still gave an error mind you, i might investigate that tomorrow...

@Wilhelmus: thanks for that! ill give it a shot tomorrow, i am assuming that thats completely untested and off the top of your head???

also, i spent a good 2 hours today searching for error codes, or infact any sort of error output for 7zip so double thanks for the heads up :)

  • 0

Ok, so with the great help from Wilhelmus I finally achieved my goal! :)

Here’s the code in case anyone is interested... It accepts a path name of where the files are located as an argument at the command line and runs through all matching files checking the integrity before attempting an unzip if they are valid..

Most importantly doesn’t bomb out if they are invalid! Woo

@echo off

REM Map source location to a drive letter
NET USE z: /delete
NET USE z: \\server\folder\sub /p:yes


REM Copy all zip files in mapped folder to temp directory 
ROBOCOPY Z:\  %1 /MIN:5


REM Change to 7zip directory
cd "c:\Program Files\7-Zip"
c:


REM Process each file in the temp directory..
for /f %%a in ('dir /b %1\b_*.zip') do call :PROCESS %1 %%a
GOTO :EOF


:PROCESS
REM Test the integrity of the file
7z t %1\%2


REM 0 = no error, else error.
IF %ERRORLEVEL% == 0 ( GOTO EXTRACT ) ELSE ( GOTO ERROR )


REM Extract the valid files into the temp directory
:EXTRACT
7z e %1\%2 -o%1 -y
GOTO END


:ERROR
echo Error: %ERRORLEVEL%


:END

Edited by BGM
  • 0

Hi,

I am trying to do similar thing as you did last year. I have a bach file and need to run 7z t command to test zip files and write result in a log file.

I did something. it creates log file and test files in cmd line. I see that testing the files running on screen, but not write it into log file. I am not fimiliar with a batch file and 7zip. Please help meeee

  BGM said:
Ok, so with the great help from Wilhelmus I finally achieved my goal! :)

Here?s the code in case anyone is interested... It accepts a path name of where the files are located as an argument at the command line and runs through all matching files checking the integrity before attempting an unzip if they are valid..

Most importantly doesn?t bomb out if they are invalid! Woo

@echo off

REM Map source location to a drive letter
NET USE z: /delete
NET USE z: \\server\folder\sub /p:yes


REM Copy all zip files in mapped folder to temp directory 
ROBOCOPY Z:\  %1 /MIN:5


REM Change to 7zip directory
cd "c:\Program Files\7-Zip"
c:


REM Process each file in the temp directory..
for /f %%a in ('dir /b %1\b_*.zip') do call :PROCESS %1 %%a
GOTO :EOF


:PROCESS
REM Test the integrity of the file
7z t %1\%2


REM 0 = no error, else error.
IF %ERRORLEVEL% == 0 ( GOTO EXTRACT ) ELSE ( GOTO ERROR )


REM Extract the valid files into the temp directory
:EXTRACT
7z e %1\%2 -o%1 -y
GOTO END


:ERROR
echo Error: %ERRORLEVEL%


:END

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • NAPS2 (Not Another PDF Scanner 2) 8.2.0 by Razvan Serea NAPS2 is a document scanning application with a focus on simplicity and ease of use. Scan your documents from WIA- and TWAIN-compatible scanners, organize the pages as you like, and save them as PDF, TIFF, JPEG, PNG, and other file formats. NAPS2 creates fully text searchable PDF files that can be imported and indexed within your document management system. NAPS2 is currently available in 40 different languages. NAPS2 key features: Scan documents using WIA- and TWAIN-compatible scanners Scan as many pages as you like from glass or ADF, including duplex support Rotate, flip, remove, and rearrange scanned pages Save as PDF, TIFF, JPEG, PNG, or other file formats Directly email PDFs Search through text included in your PDFs by using optical character recognition (OCR), in any of over 100 languages. Configure brightness, contrast, resolution, and other scan options Save your configurations as profiles to be easily reused later Optional command-line interface (CLI) for automation and scripting MSI installer and application-level configuration available for group policy (GPO) deployment Portable/standalone archives available Translations: English, Català, Čeština, Dansk, Deutsch, Español, Français, Hrvatski, Italiano, Magyar, Nederlands, Polski, Portugues, Russian, Ukrainian, Hebrew NAPS2 8.2.0 changelog: NAPS2 is now available on the Microsoft Store. It costs a small fee to support the developer and provide automatic updates. NAPS2 will continue to be freely available at www.naps2.com Added "Edit with" under the "Image" menu for using an external image editor Added "Share even when NAPS2 is closed" option for Scanner Sharing This will show a system tray icon and restart on login Imported file names are now used as the default file name when saving The "Apply to all selected" checkbox now stays checked Escl: Increased maximum time searching for devices from 5s to 60s Escl: Scanner IPs are now cached for faster and more reliable scanning Windows: Added an arm64 installer Windows: Replaced the "No friendly name" device name from some drivers with "Unknown Scanner" Mac: Fixed an issue where saved files didn't always have the right extension Mac: Disabled the "Apple Mail" email provider when not the default email reader Mac: Updated icons for Split/Combine Linux: Fixed issues with the Save dialog Download: NAPS2 (64-bit) | 43.5 MB (Open Source) Download: Portable NAPS2 8.2.0 | 61.9 MB Link: NAPS2 Home Page | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • I still remember a prediction that in the future, the BIOS will have all the necessary drivers... for any OS. Still not there
    • No mandatory.... Once again the one-way approach (only incentives or only detractors, or as they say 'the carrot or the stick') has proven ineffective.
    • ScreenToGif 2.41.3 by Razvan Serea Screen, webcam and sketchboard recorder with an integrated editor. ScreenToGif is an open source program that lets you to record a selected area of your screen, live feed from your webcam or live drawings from a sketchboard. Afterward, you can edit and save the animation as a gif, apng, video, psd or png image. Also there's the Webcam Recorder and the Board Recorder. Features: Record your screen and save directly to a gif looped animation or a video. Pause and continue to record or start over by discard the recording. Move the window around to record what you want, it stays on top. You can add Text, Subtitles, Title Frames, Borders, Watermarks, Drawings, etc. Crop and Resize. Remove frames that you don't want. Select a folder to save the file automatically or select one before encoding. Add the system cursor to your recording. Very small sized and portable executable. What's new in ScreenToGif 2.41.3: Reduced app size and migrated to .NET 9. It's now easier to see the brush cursor when painting light or dark colors on background of same lightness (Editor and Board recorder). Add possibility of running the app having only newer versions of .NET by @nil4 in #1381 Updated Turkish translation by @luther09 in #1389 Added தமிழ் (Tamil) translation by @TamilNeram in #1387 Updated Greek Translation by @theoasima in #1346 Fixed Importing frames using FFmpeg v7 was getting stuck. Exporting partially using an expression was not following the frame ordering. Color picker was not selecting the correct hue. Download: ScreenToGif 64-bit | Portable 64-bit ~7.0 MB (Open Source) Download: ScreenToGif 32-bit | Portable 32-bit Download: ScreenToGif ARM64 | Portable ARM64 View: ScreenToGif Home Page | User Guide | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Explzh 9.77 by Razvan Serea Explzh is a free Windows archive manager for creating, extracting and managing archives. The program supports many different types of archives, including zip, 7z, rar, tar, ace, lzh, arj, cab, iso, img, msi, sfx and more. Apart from archive creation and extraction, you will also be able to verify compressed data for errors, initiate repair routines, split data into multiple items, and more. It additionally allows you to password protect your data and attach digital signatures to files. Key features of Explzh: Explorer-like GUI and operability. LHA, ZIP (ZIPX), JAR, CAB, RAR, TAR, TAR.XXX, 7z, ARJ, WIM, CHM, PE, HFS, NSIS Format Installer, ISO, InstallShield, MSI, and several other formats... Support for more archive formats by introducing the integrated archiver DLL. Self-extracting archive creation function that can create high-performance automatic installers. Digital signature addition function to created self-extracting archive. Office 2007 or later document compression / image optimization re-archiving function. Supports compression and decompression of Unicode file names. Supports compression and expansion exceeding 4GB. AES encryption function. You can create a robust secure ZIP encryption archive. Thumbnail function of image file. In-library file search function. . Equipped with archive file conversion function. File split function. The split file has a self-consolidation function, and can concatenate files larger than 4GB. (No need for batch file or connection software) UU (XX) Encode, Base64 decode function. FTP upload function Supports Windows 11 shell integration extended context menu. Explzh 9.77 changelog: Added a "Reset" button to the [Special Color Settings] in [Options]-[Screen Color]-[Special Colors]. *After resetting, the colors will be set to the ones that AI judges to be most optimal for the current screen mode. Updated to Visual Studio 2022 v.17.14.4. Download: Explzh 64-bit | Explzh 32-bit | ~6.0 MB (Freeware) Download: Explzh ARM64 | 5.9 MB View: Explzh Home Page | Screenshot | Themes Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • Week One Done
      Al_ earned a badge
      Week One Done
    • Week One Done
      MadMung0 earned a badge
      Week One Done
    • Reacting Well
      BlakeBringer earned a badge
      Reacting Well
    • Reacting Well
      Lazy_Placeholder earned a badge
      Reacting Well
    • Dedicated
      Epaminombas earned a badge
      Dedicated
  • Popular Contributors

    1. 1
      +primortal
      477
    2. 2
      +FloatingFatMan
      273
    3. 3
      ATLien_0
      243
    4. 4
      snowy owl
      211
    5. 5
      Edouard
      182
  • Tell a friend

    Love Neowin? Tell a friend!