4
votes

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\[email protected]" >> D:\WORK\SCRIPTS\LOGS\LOG_20140120.LOG && DEL "D:\serverlogs\%%F"
)

Which this does work well now.

2
Can you elaborate on "if there are multiple of the same file name structure"? Some examples would be useful here.Mark
To best suggest alternatives, please list out all the features that you require in the file archive/compression software.David Ruhmann
Hopefully with the code and the requirements it provides more color to the issue.Leptonator
Did provide an answer to my question. Added a component to delete the file in the FOR loop - this is now working properly.Leptonator

2 Answers

7
votes

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

0
votes

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.