1

I use a batch file to backup files to 7-Zip archives. It creates an archive for each file separately in the folder where the original file is. Works with sub folders.

FOR /r %%i IN (*.*) DO ( "c:\program files\7-zip\7z.exe" a "%%~i.7z" "%%i" -p"%variable%" -t7z -mx0 -mhe -mmt )

Then I clone the original directory tree (no files copied here) with XXCopy to a new destination.

XXCopy "%DirectorySource%" "%DirectoryDestination%" /T /ED5 /Q3 /YY

Then I move all the 7z files to the cloned directory structure above.

XXCopy "%DirectorySource%\*.7z" "%DirectoryDestination%" /S /ED /RC /YY /Q3

This works perfectly, although I would like to create the 7z archives directly into the cloned directory structure. No files to be created or modified at the Source Location.

Thanks!

  • Where you specify the destination 7z archive in your first line ("%%~i.7z"), why don't you construct the destination with something like ("%DirectoryDestination%%%~ni.7z")? – Dane Aug 18 '15 at 16:31
  • 1
    I guess I really meant ("%DestinationDrive%%%~pni.7z") – Dane Aug 18 '15 at 16:39
  • That worked! Do you know how to: 1. Keep the extension of the file before the 7-zip extension (example: photo.jpg.7z) 2. The code you provided creates folders before the current folder in the destination. (example: If the folder I am backing up is C:\1\2\3\Photos and I want to back it up to *D:\Backup\Photos* what I get is *D:\Backup\Photos\1\2\3\Photos* and then all the files. I would like to just have: *D:\Backup\Photos* and then all the files and sub folders that were backed up.) – user3703618 Aug 18 '15 at 22:14
  • 1
    %%~pnxi.7z is a shorter way to write that bit, as you're combining the p, n, and x which give you the path, filename, and extension respectively. This is already at the limit of my understanding of batch files. I'm pretty sure that there's a way to capture just the part of the path that you care about. – Dane Aug 19 '15 at 4:23
  • 1
    Do you use this batch file from various locations? Or do you always run it for the same pair of directories? If the same pair, there's the %string:~4,3% method of extracting part of a variable (in this case grabbing three characters after skipping the first four characters of %string%). – Dane Aug 19 '15 at 4:34
2

AutoHotKey to the rescue! Limiting yourself to the Windows command line is painful. Since you mentioned that you intend to do this loop in conjunction with AutoHotKey, why not just use AutoHotKey for the whole thing?

Use StrLen and SubStr to extract the variable portion of your file path. The file loop will recurse through all the files you want. And then it's just a matter of using RunWait to pass the paths you've generated to 7-Zip. The ,, Hide specified at the end of the RunWait tells it to hide the command windows spawned.

Here's an example script that includes the ability to choose source and destination folders via the GUI:

InputBox, password, Enter Password for Archives, The generated archives will be protected with the password you enter below. Your input will be masked., hide
; Using FileSelectFolder is just one way of choosing your folders.
FileSelectFolder, sourcepath,,, Source Folder
sourcepath := RegExReplace(sourcepath, "\\$")  ; Removes the trailing backslash, if present.
FileSelectFolder, destinationpath,,, Destination Folder
destinationpath := RegExReplace(destinationpath, "\\$")  ; Removes the trailing backslash, if present.

; This is the meat of the answer:
sourcelen := StrLen(sourcepath) + 1    ; Determine the start of the variable part of the path.
Loop, Files, %sourcepath%\*.*, R       ; Here's the replacement for your batch file loop.
{
    varfilepath := SubStr(A_LoopFileFullPath, sourcelen) ; Grab everything to the right of the source folder.
    RunWait, "c:\program files\7-zip\7z.exe" a "%destinationpath%%varfilepath%.7z" "%A_LoopFileFullPath%" -p"%password%" -t7z -mx0 -mhe -mmt,, Hide
    FileCount := a_index
}
Msgbox Archives Created: %FileCount%`nSource: %sourcepath%`nDestination: %destinationpath%

Note that you need v1.1.21+ of AHK or above for the file loop to operate as written.

  • note this script doesn't handle cancels very well :) – jiggunjer Jan 17 '16 at 9:25
  • Do you mean during the AHK portion or while 7-Zip is compressing? – Dane Jan 17 '16 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.