Archive directory(s) with 7-Zip, and upload via FTP

Example scripts and tips (replaces Old Scripts & Tips archive)

Moderators: Dorian (MJT support), JRL, Phil Pendlebury

Post Reply
catman
Newbie
Posts: 10
Joined: Wed Oct 07, 2015 5:34 pm

Archive directory(s) with 7-Zip, and upload via FTP

Post by catman » Sat Oct 17, 2015 12:25 am

7-Zip is an excellent free open-source compression program: www.7-zip.org

This macro compresses the specified directory/subdirectories into a .7z file and uploads the resulting uniquely-named and password-protected archive file to the specified website.

The uploaded archive is then size-checked against the PC original.

Code: Select all

Let>IGNORESPACES=1
Let>fromDrive=m:
Let>fromVolume=MSF
Let>7zDrive=z:

// ask for an optional comment to be included in the 7z file name
Input>comment,Optional %fromVolume% archive comment,
StringReplace>comment,SPACE,{""},comment

// get the current date/time for inclusion in the 7z file name
GetDate>date
GetTime>time

// remove dashes, colons, and spaces from date/time (colons are not allowed in a file name)
StringReplace>date,-,{""},date
StringReplace>time,:,{""},time
StringReplace>time,SPACE,{""},time

// generate the full 7z file name and path
Let>7z_file=%fromVolume%-%date%-%time%-%comment%.7z
Let>7zPath=%7zDrive%\%7z_file%

// run 7-Zip, -r recursing subdirectories, -x excluding any recycle bin and System Volume Information files
Let>fromPath=%fromDrive%\*
Let>password=xxxxxxxx
Let>z7exe="c:\program files\7-Zip\7z.exe"
Let>RP_WAIT=1
Run>%z7exe% a -p%password% -r -x!$RECYCLE.BIN -x!"System Volume Information" %7zPath% %fromPath%
If>RP_RESULT<>0
    MessageModal>7-Zip execution error: "%RP_RESULT%"
    Exit
Endif

// test the generated archive
Run>%z7exe% t -p%password% %7zPath%
If>RP_RESULT<>0
    MessageModal>The archive failed testing
    Exit
Endif

// upload the archive
Let>FTP_PASSIVE=1
Let>server=ftp.website.com
Let>username=uuuuuuuu
Let>password=yyyyyyyy
FTPPutFile>server,username,password,21,7zPath,/,,I
If>FTP_RESULT<>Success
    MessageModal>FTP error: FTP_RESULT <> "Success": "%FTP_RESULT%"
    Exit
Endif
If>FTP_NUMFILES<>1
    MessageModal>FYI: %FTP_NUMFILES% files were transferred instead of just one
    Exit
Endif

// get ftp directory information for the file just uploaded
Let>dir_list=%TEMP_DIR%\dir_list.txt
FTPGetDirList>server,username,password,21,dir_list,/,7z_file,D

// compare the ftp file size to the pc file size
// dir_list should contain a one-line directory listing of our uploaded .7z file
ReadLn>dir_list,1,line
Let>ftp_size={ltrim(copy(%line%,26,10))}
FileSize>7zPath,pc_size
If>ftp_size<>pc_size
    MessageModal>File size mismatch.%CRLF%FTP file size:%ftp_size% PC file size:%pc_size%
    MessageModal>FTP directory entry:%CRLF%"%line%"
    Exit
Endif

MessageModal>%fromDrive% (%fromVolume%) successfully 7-Zip'd and FTP'd
Exit

User avatar
CyberCitizen
Automation Wizard
Posts: 721
Joined: Sun Jun 20, 2004 7:06 am
Location: Adelaide, South Australia

Re: Archive directory(s) with 7-Zip, and upload via FTP

Post by CyberCitizen » Wed May 18, 2016 3:44 am

Nice Script.
FIREFIGHTER

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts