-2

I want to back up important files to my USB drive every day. The USB drive is always plugged into the computer, so I don't need to worry about drive letters. I know how to create a batch file to simply copy and paste them into the drive, but I was wondering if there is a way to create a batch file that makes a zip file of all the folders I want (using winzip or winrar), and then has them sent to the drive. That way I can archive them instead of just copying and replacing them.

Thank you.

Brad
  • 1
  • 1
  • 1
  • 1
    What have you tried, and how has what you've tried failed? Ideally, you should provide a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) of what you've tried, and include specific information on how it failed, with error messages and/or erroneous output. SO is not a code-writing service; the best questions are those which provide useful information so that those who answer can guide you to devising your own correct answer. See [How to Ask a Good Question](http://stackoverflow.com/help/how-to-ask). – Jeff Zeitlin May 17 '17 at 18:47

2 Answers2

1

See:How can you zip or unzip from the command prompt using ONLY Windows' built-in capabilities?

Powershell can do this:

Add-Type -A System.IO.Compression.FileSystem
[IO.Compression.ZipFile]::CreateFromDirectory('C:\foo\', 'D:\foo.zip')

you can incorporate it into a batch file by calling powershell.exe like so:

powershell.exe -nologo -noprofile -command "& { Add-Type -A 'System.IO.Compression.FileSystem'; [IO.Compression.ZipFile]::CreateFromDirectory('C:\foo\', 'D:\foo.zip'); }"

I would recommend running it from a scheduled task so you don't have to start it manually or worry about it running all the time, and plan on disconnecting/replacing/archiving the drive itself occasionally - locally attached storage is not a backup

Community
  • 1
  • 1
Cpt.Whale
  • 4,784
  • 1
  • 10
  • 16
0

Start WinRAR, click in menu Help on first menu item Help topics, expand on help tab Contents the list item Command line mode and

  • read the help page Command line syntax,
  • expand the list item Commands and read the help page Alphabetic commands list and
  • expand the list item Switches and read the help page Alphabetic switches list.

Then you know how I created this single command line below and what all those switches mean.

"%ProgramFiles%\WinRAR\WinRAR.exe" a -ac -ao -afzip -agYYYY-MM-DD_NN -cfg- -ed -ep1 -ibck -inul -m5 -r -y -- "D:\Backup Folder\DataBackup_.zip" "C:\Path To Folder With Files To Backup\"

See also answer on Simply compress 1 folder in batch with WinRAR command line?

WinRAR adds to ZIP archive only files with archive attribute set because of switch -ao and clears the archive attribute after compressing the file into the archive because of -ac. The archive attribute is automatically set by Windows when a file is modified, renamed or moved. So this command line avoids compressing the same unmodified files again and again into ZIP archives.

The ZIP file is named automatically with current date in name and an automatic incremented number in case of this command line is executed multiple times on one day.

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143