Pipes, not just for plumbers.

For quite some time, I have wanted to develop a batch script that I could use to automate the process of backing up my documents then email them to myself. Using my GMail account's enormous capacity, I can keep several copies of my documents accessible online without having to worry about drive space.

My requirements:

  1. Compression needs to be a standard format. (ZIP, TAR, etc.) I want the option to access these files using any computer.
  2. Need to be able to filter out files or include only specific files (not just the entire folder). I have a few large databases, that I wouldn't want to be sent. No matter what, I don't want this 2gb Access database sent to me.
  3. Automated. I want this to happen on a regular basis, without me having to initiate it. Backups are useless if you can't rely on them.
  4. Sent to me via email. As I stated, I want this to be emailed to me, instead of sitting on the computer I ran it from.

Here is the code that I came up with. I make use of DOS pipes in order to get my data.

dir /b /s /AA "%userprofile%\my documents\" | find "My Shapes" /v | find "My Pictures" /v | find "My Videos" /v | c:\bin\zip232xn\zip c:\My_Stuff.zip -@ >> c:\my.log

The DIR command displays the directory in BARE format (path and filename only), only show files with the Archive bit turned on, and search sub-folders. The FIND statement, searches the filenames given it, and excludes entries that contain the search string (like "My Shapes"). Finally Info-Zip takes the filename and compresses it into a single file, C:\My_Stuff.zip.

When that is complete, I run the DOS Attrib command to turn off the attribute bit on all the files I compressed. Then I email myself the file using BLAT. Note, that I have set it to span the email into multiple 10mb attachments. You'll need to adjust this to match your outgoing SMTP server, and use an email program that can handle multipart UUEncoded attachments.

You'll need:

The full script is below.


Echo off
echo %DATE% %TIME% > c:\my.log

dir /b /s /AA "%userprofile%\my documents\" | find "My Shapes" /v | find "My Pictures" /v | find "My Videos" /v | c:\bin\zip232xn\zip c:\My_Stuff.zip -@ >> c:\my.log
if "%errorlevel%"=="0" attrib -A /s /D "%userprofile%\my documents\*.*" >> c:\my.log

REM Repeat code above for additional folders. For example a server side share.

:send
C:\BIN\blat262\full\blat.exe c:\my.log -to example@gmail.com -server mail -Subject "Stuff from %date% %time%" -f MyFromAddress@example.com -attach c:\my_stuff.zip -multipart 10240

if "%errorlevel%"=="0" del c:\my_stuff.zip
del c:\my.log

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <blockquote> <center> <hr> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img>
  • Lines and paragraphs break automatically.
  • Web page addresses and e-mail addresses turn into links automatically.

More information about formatting options