4

Please see updated answer below

Windows Batch files.

I absolutely love 7-Zip and works great, but like WinZip's ability to move and timestamp files. However, WinZip's (command-line interface) has limitations for the size of files and the quantity of files within an archive, for example.

Googling we do see that there is a person, who did take 7-zip's code and has re-compiled the code to use an -m (move) switch. However, I don't feel comfortable about using a third-party outside of the Developer of 7-zip in our Production environment.

Also, I have seen - http://alternativeto.net/software/7-zip/

I have coded: IF NOT ERRORLEVEL 1 (after the Zip process) to delete the file, but this cannot be safe if there are multiple of the same file name structure.

Here are some sample bits of code: 7zip:

D:\scripts\7za -tzip -y a D\zips\20140116_someclient_%computername%@%location%.zip D:\logs\apache_log.2014-01-16* >nul >>%logfile%
IF NOT ERRORLEVEL 1 ECHO Y|DEL D:\logs\apache_log.2014-01-16*

Where apache_log.2014-01-16 could be:

apache_log.2014-01-16.1
apache_log.2014-01-16.2
apache_log.2014-01-16.3

etc.

WinZip

c:\progra~1\WinZip\Wzzip.exe -T -ex -m D\zips\20140116_someclient_%computername%@%location%.zip D:\logs\some_file.2014-01-16* >nul >>%logfile%

I guess from a software requirements standpoint, I would like to have some of the abilities of WinZip in 7Zip.. I like 7Zip as it has the ability to work with large Archives. I like PeaZip and I used PowerArchiver many moons ago. Problem I have is that I am working with Apache Tomcat for Windows, and we have to archive off hourly logs that can be over 200-300 MB an hour and need to keep the servers clear of old logs.

Hope this makes more sense.

UPDATE:

Do believe that I have answered my own question..

FOR /F %%F IN ('dir /B "D:\serverlogs\server.log.2014-01-19*"') DO (
D:\work\scripts\7za.exe –tzip –y a "D:\ziplogs\201401109_COMPANY_SERVERNAME@DATACENTER.ZIP" >> D:\WORK\SCRIPTS\LOGS\LOG_20140120.LOG && DEL "D:\serverlogs\%%F"
)

Which this does work well now.

Leptonator
  • 3,379
  • 2
  • 38
  • 51
  • Can you elaborate on "if there are multiple of the same file name structure"? Some examples would be useful here. – Mark Jan 17 '14 at 18:47
  • To best suggest alternatives, please list out all the features that you require in the file archive/compression software. – David Ruhmann Jan 17 '14 at 19:13
  • Hopefully with the code and the requirements it provides more color to the issue. – Leptonator Jan 17 '14 at 20:24
  • Did provide an answer to my question. Added a component to delete the file in the FOR loop - this is now working properly. – Leptonator Jan 20 '14 at 08:24

2 Answers2

7

Latest alpha versions of 7-Zip support new switch "-sdel" to delete files after including to archive.

Igor Pavlov
  • 209
  • 1
  • 6
  • It is great to get an answer here from the person who wrote 7-Zip. Many Thanks. – Leptonator Jan 19 '14 at 01:40
  • OK.. I trawled around on your site, and I see that 9.30 has the -sdel key and then it was removed in 9.32? – Leptonator Jan 19 '14 at 05:33
  • -sdel switch is supported in all new versions: 9.30, 9.31, 9.32. – Igor Pavlov Jan 20 '14 at 16:25
  • At the risk of appearing silly, can you provide a sample use of the -sdel command? Any time I tried to use it, it failed. – Leptonator Jan 21 '14 at 05:21
  • 1
    7z -sdel a a.zip a.txt – Igor Pavlov Jan 23 '14 at 20:25
  • Thank you for the guidance on this. Will definitely look into the use of this switch, going forward. – Leptonator Jan 26 '14 at 05:56
  • 1
    can be the Alpha version used without fear of data loss? When will be stable version with -sdel included? – Dee Jun 10 '14 at 08:29
  • @Dee at the time you asked it was already included in the stable version – Edgar Griñant Jan 05 '15 at 10:59
  • Well, this is still a problem then for Debian / Ubuntu users! Even Ubuntu 15.04 is stuck on the ancient p7zip version 9.20, where no '--move' or '-sdel' param seems available. This is very important to inline shell commands and scripting. Cannot seem to find a PPA repo for a newer/alpha version – Marcos Jul 24 '15 at 14:42
0

On 7z Discussion there you can found a crazy batch way of substitute of missing delete after archive switch by Andy here: http://sourceforge.net/p/sevenzip/discussion/45797/thread/5bf7022b/

@ECHO off
echo ################################################
echo Zipping up Backups with 7-Zip
cd e:\gh\backups
7z.exe a %DATE:~-4%-%DATE:~4,2%-%DATE:~7,2%_DB1-Prod.7z
    IF  "%errorlevel%"=="0" (goto testzips) ELSE (goto zipfail)
:testzips
echo ################################################
echo Testing Integrity of Zipfile
FOR %%i IN (*.7z) DO 7z.exe t %%i
    IF "%errorlevel%"=="0" (goto extract) ELSE (goto testzipfail)
:extract
echo ################################################
Echo Extracting files to temp folder
FOR %%i IN (*.7z) DO 7z.exe e %%i -oe:\gh\temp
IF  "%errorlevel%"=="0" (goto movezip) ELSE (goto errorextract)
:movezip
echo ################################################
echo Moving Zipped File to Archive Folder
move e:\gh\Backups\*.7z e:\gh\Archives
IF  "%errorlevel%"=="0" (goto testfiles) ELSE (goto archivemovefail)
:testfiles
echo ################################################
fc /b e:\gh\backups\*.4BS e:\gh\temp\*.4BS 
IF ERRORLEVEL 0 (goto testfile4BL) else (goto comparealert)
:testfile4BL
fc /b e:\gh\backups\*.4BL e:\gh\temp\*.4BL
IF ERRORLEVEL 0 (goto testfile4BK) else (goto comparealert)
:testfile4BK
fc /b e:\gh\backups\*.4BK e:\gh\temp\*.4BK
IF ERRORLEVEL 0 (goto testfile4BR) else (goto comparealert)
:testfile4BR
fc /b e:\gh\backups\*.4BR e:\gh\temp\*.4BR
IF ERRORLEVEL 0 (goto copytonas) else (goto comparealert)
:: IF ERRORLEVEL 1 (goto copytonas) ELSE (goto comparealert)
:copytonas
echo ################################################
echo Copying archive to Fileserver
robocopy e:\gh\Archives \\fileserver\backups /NP /MAXAGE:1
REM IF "%errorlevel%"=="1" (goto cleanup) ELSE (goto nascopyalert)
    if errorlevel 16 echo ***FATAL ERROR*** & goto nascopyalert
    if errorlevel 15 echo OKCOPY + FAIL + MISMATCHES + XTRA & goto nascopyalert
    if errorlevel 14 echo FAIL + MISMATCHES + XTRA & goto nascopyalert
    if errorlevel 13 echo OKCOPY + FAIL + MISMATCHES & goto nascopyalert
    if errorlevel 12 echo FAIL + MISMATCHES& goto nascopyalert
    if errorlevel 11 echo OKCOPY + FAIL + XTRA & goto nascopyalert
    if errorlevel 10 echo FAIL + XTRA & goto nascopyalert
    if errorlevel 9 echo OKCOPY + FAIL & goto nascopyalert
    if errorlevel 8 echo FAIL & goto nascopyalert
    if errorlevel 7 echo OKCOPY + MISMATCHES + XTRA & goto nascopyalert
    if errorlevel 6 echo MISMATCHES + XTRA & goto nascopyalert
    if errorlevel 5 echo OKCOPY + MISMATCHES & goto cleanup
    if errorlevel 4 echo MISMATCHES & goto nascopyalert
    if errorlevel 3 echo OKCOPY + XTRA & goto cleanup
    if errorlevel 2 echo XTRA & goto nascopyalert
    if errorlevel 1 echo OKCOPY & goto cleanup
    if errorlevel 0 echo No Change & goto nascopyalert
:cleanup
Echo Congrats - All files were backed up properly.  Cleaning up
REM cd e:\gh\temp
REM del /Q *.*
cd e:\gh\temp
    del /Q *.*
cd e:\gh\backups
    del /Q *.*
IF "%errorlevel%"=="0" (goto EOF) ELSE (goto cleanupalert)
:zipfail
echo FAILURE 
echo Backup files did not zip properly - Check Backups!!
:testzipfail
echo FAILURE 
echo Backup files did not pass integrity chec - Check Backups!!
:archivemovefail
echo FAILURE 
echo Backup files did not move to archive directory - Check Backups!!
:errorextract   
echo FAILURE 
echo Backup files did not extract to test directory properly - Check Backups!!
:comparealert
Echo FAILURE
echo Backup files did not pass comparison check in FC - Check Backups!!
:nascopyalert
echo FAILURE 
echo Backup files did not copy to the NAS - Check Backups!!
:cleanupalert
echo FAILURE 
echo Backup files did not clean up - Check Backups!!
:cleancomplete
:EOF
Echo Proccess Complete - Check Logs and emails.

But this could actually work after some tuning. Better would be if such options could be in 7zip.

Dee
  • 282
  • 1
  • 9
  • I think you need to work on your ERRORLEVEL stuff.. The behavior as presented causes me to question the reliability of this script.. Start here - http://stackoverflow.com/questions/4367930/errorlevel-inside-if?rq=1 – Leptonator Sep 15 '14 at 17:49