• 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

    • I'm old I guess, first thing I think of is just regular input/output.
    • Now, kids, Dan O'Dowd is what we call a professional hater.
    • Billionaire slams 'Tesla Cultists' for praising Robotaxi, says it's 5+ years behind Waymo by David Uzondu Image via Depositphotos.com The Tesla Robotaxi program has kicked off in Austin, Texas, and reactions are pouring in from all corners of the internet. A select group of investors and influencers have been invited to try the service, which operates within a limited area of South Austin for a price of $4.20. While the vehicles are operating without anyone in the driver's seat, the program has specific rules for this pilot phase, including a human "safety monitor" who rides along in the passenger seat just in case things go sideways. Of course, the launch did not go unnoticed by Tesla's most vocal and well-funded critic, Dan O'Dowd. O'Dowd is the billionaire founder of a group called The Dawn Project, which has dedicated itself to highlighting what it calls critical safety failures in Tesla's Full Self-Driving software. He refers to himself as an expert in creating "unhackable" software for military and aerospace clients, and ran for U.S. Senate back in 2022 on a single-issue platform: to "make computers safe for humanity" by banning Tesla's FSD. In 2023, He was banned from advertising on X after He made promoted posts that show Tesla FSD among other things, failing to stop at Stop signs. Last year, his group, The Dawn Project, paid for a Super Bowl ad, where a Tesla equipped with FSD did not act on a child-sized mannequin in the road. That commercial ends with a message, urging parents to "boycott Tesla to keep your kids safe." Today, O'Dowd took to X to slam the launch of the Robotaxi service, saying the "Tesla Cultists are celebrating victory" over a system he believes is years behind the competition (especially Waymo). He pointed out that with only fourteen cars operating for half the day, the system was already making significant errors, a rate he claims is consistent with community-tracked FSD data. The videos shared by the creators (Rob Maurer and Ed Niedermeyer), O'Dowd mentioned in his post, appear questionable, depending on your perspective. In Maurer's video, a trip that was otherwise smooth had a few unnerving seconds of the vehicle slightly swerving into the wrong lane, correcting itself, swerving again, correcting itself, and then finally settling. The other video from Ed Niedermeyer shows something entirely different. Niedermeyer captured a Tesla Robotaxi approaching an "extensive crime scene" with multiple police vehicles parked on the side of the road. On his personal Bluesky account (Ed stopped posting on X late last year, in protest of Musk), He claims the Tesla braked hard twice for no clear reason. In his commentary, Niedermeyer argued the car "shouldn't react to any of these police vehicles," and that it was concerning how it reacted to some but not others, before stopping in the "middle of the road instead of defaulting to a minimal risk condition."
    • Arch is now also using Wayland as the default session for Plasma 6.4, with X11 session becoming optional (so upgrading to Plasma 6.4 on X11 Arch might need manual intervention). It's been well over a decade in making, but I guess the time for Wayland to be the default is finally upon us.
  • Recent Achievements

    • Week One Done
      ravenmanNE earned a badge
      Week One Done
    • Conversation Starter
      Brett76 earned a badge
      Conversation Starter
    • One Month Later
      Miguel Batista earned a badge
      One Month Later
    • Dedicated
      moojay67 earned a badge
      Dedicated
    • One Month Later
      Jim Dugan earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      664
    2. 2
      Michael Scrip
      229
    3. 3
      ATLien_0
      216
    4. 4
      Steven P.
      148
    5. 5
      Xenon
      141
  • Tell a friend

    Love Neowin? Tell a friend!