• 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

    • let's be honest here, it was in the line of secret doxing app
    • Dating safety app Tea spills private chats in new leak by Usama Jawad Tea is (was?) an extremely popular "dating safety" app designed for women who typically uploaded pictures of men they have dated, recounting their experience, and highlighting red flags. The app maintains exclusivity to women by requiring all its users to submit a selfie and government identification during the sign-up process. It was the target of a major breach a couple of days ago due to a Firebase bucket being left publicly exposed, leaking the identification data and other sensitive information for thousands of users. Now, the app has been struck with a second cybersecurity incident and it is arguably bigger than the first. 404 Media reports that a second database has leaked and it contains about 1.1 million chat messages discussing some sensitive topics that people likely wouldn't want to make public. These include topics like cheating partners, abortions, and unfaithful boyfriends. The messages span from 2023 to last week, but the impact and scope of the leak is unclear. The person who did discover the database noted that practically any user could access the repository using their own API key. In a statement to Bleeping Computer, Tea has confirmed the second breach too, noting that "some" direct messages (DMs) have been exposed. The company has decommissioned the affected system for now, but claims that other infrastructure remains unaffected. It has emphasized that it will invest efforts in the coming days to improve its cybersecurity posture, but did not share any further details at this time. The service will also be reaching out to its affected customers and offer them free identity protection services as a sort of an apology. These cybersecurity incidents further highlight the need to be vigilant when sharing identifiable information online, especially with apps which are very new to the market and have not yet matured. Security researchers and analysts have cautioned the public that it is very possible to locate social media profiles of Tea users due to all the data that has been leaked.
    • 26200.5722 is the first available 25H2 build from the ge_release_svc_betaflt branch (25H2's previous branch was the "ge_prerelease_im" branch). The 26200.5722 release also removes the "Insider Preview" references in the system area. These significant changes usually indicates that the public release of 25H2 will be ready within 6 weeks to 2 months.
    • Microsoft: Windows Autopatch is the safest way to upgrade enterprise PCs to Windows 11 by Usama Jawad A few hours ago, Microsoft published a guide for IT admins explaining how they can use Intune to upgrade Windows 10 devices to Windows 11, while also migrating from Active Directory (AD) to a cloud-native system like Entra ID. The company has also published a similar guide, but switched the tool to Windows Autopatch, claiming that it is the fastest and safest way for enterprise PCs to update to Windows 11. For those unaware, Windows Autopatch is a way to automate updates while empowering IT admins to ensure that endpoints are healthy and compliant through ring-based, staggered deployments. IT admins also have the ability to reverse updates easily if something does go wrong. In the current scenario of upgrading enterprise PCs to Windows 11 using Autopatch, Microsoft has outlined a four-step process. The first involves assessing Windows 11-readiness across your organization, assigning Entra ID groups to devices, and then mapping these groups to rollout rings in Autopatch. Next, IT admins should segment devices into Windows Autopatch groups, while also defining staggered rollout policies controlled through rollout rings. At a base level, there should be two groups: devices that meet the criteria of Windows 11 and should upgrade to it, and Windows 10 hardware that doesn't meet the criteria and should receive Extended Security Updates (ESUs). Devices should be spread in a logical manner across various rings, with each group having a dedicated update policy. The third step involves defining the speed of staggered update rollouts. This can be managed through the Intune admin center, which gives you control over sequencing, pace, and deferrals. Finally, IT admins should monitor the rollout of the Windows 11 update through the Windows Autopatch feature update reporting module. It contains the update status across devices, trendlines within historical views, and remediation guidance for errors. Microsoft believes that this combination of Windows Autopatch groups and Intune is the best way to upgrade to Windows 11, so IT admins should get started right away as support for Windows 10 is ending on October 14, 2025.
    • TDP of this CPU is 60 watts higher than Ryzen 7600. At s usage rate of four hours per day, at a cost of twelve cents per KWh, the Intel cost $10.51 more per year to use. I don't see a real advantage to Intel here.
  • Recent Achievements

    • Dedicated
      ataho31016 earned a badge
      Dedicated
    • First Post
      Gladiattore earned a badge
      First Post
    • Reacting Well
      Gladiattore earned a badge
      Reacting Well
    • Week One Done
      NeoWeen earned a badge
      Week One Done
    • One Month Later
      BA the Curmudgeon earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      652
    2. 2
      ATLien_0
      261
    3. 3
      Xenon
      165
    4. 4
      neufuse
      142
    5. 5
      +FloatingFatMan
      107
  • Tell a friend

    Love Neowin? Tell a friend!