Developing Matt

My Technical Journal

The Case Against the Batch

leave a comment »

The next time someone asks me to write a batch file I’m going to say no.  I end up having to write one every 5 or 6 months and they always give me problems, whether security or switches or a misunderstanding on a command.  This one creates a backup directory, and then copies files over from a source location to a destination.  It sounds easy, but the reality is that this simple request has moved from ‘just copy files’ to ‘copy files, create a full log to show errors, make a backup of all existing files, and let my assistant be able to run it.’  Soon I expect them to ask me to enable it to only copy certain folders and make it shoot fireworks when it’s done.  The point is: for production don’t use batch.  Use a vb app

  • Catching errors isn’t involves piping out all your cammands in hope that one of them contains an error which they usually don’t. (it seems like there is a way to catch system errors but i can’t find it) 
  • VB app has error handling
  • VB app has debugging
  • Sending emails is easier (i know there are handy dandy tools but there is a comfort level difference here that makes my point for me)
  • Security: anyone with access to the bat file can modify it
  • Extensibility is easier
  • It doesn’t feel like I’m rewriting the wheel. 

There is a bottom line here: program in a technology that you are used to or want to.  Your case can be made simply under the notion that it’s what you know and are motivated to do.  If you don’t know it or aren’t motivated it will take you longer.  Stand up for your self.  With that said, here’s my batch file that runs every day that I wrote because I was asked to and I didn’t talk back.  : )

echo on
REM For testing purposes my production destination is c:\temp
REM this batch file copies existing files to a directory dated today, deletes the existing files, and copies from a source location

REM STEP 1 check for flag before copying
if not exist \\sourcelocation\c$\website\Content\CopyContentYes.txt goto end

Set mm=%DATE:~4,2%
Set dd=%DATE:~7,2%
Set yyyy=%DATE:~10,4%

REM if backup directory exists then GO TO delete files line (else create directory and copy files)
if exist c:\temp\contentBackup\ContentBackup_%yyyy%_%mm%_%dd% GOTO DELETEFILES
mkdir c:\temp\contentBackup\ContentBackup_%yyyy%_%mm%_%dd%

echo STEP 3 COPY LIVE CONTENT FILES TO BACKUP FOLDER >c:\temp\contentBackup\ContentBackup_%yyyy%_%mm%_%dd%\copylog.txt
xcopy c:\temp\content  c:\temp\contentBackup\ContentBackup_%yyyy%_%mm%_%dd% /y /e /c >>c:\temp\contentBackup\ContentBackup_%yyyy%_%mm%_%dd%\copylog.txt

echo STEP 4 DELETE FILES in content files ON PRODUCTION SERVER >>c:\temp\contentBackup\ContentBackup_%yyyy%_%mm%_%dd%\copylog.txt
del c:\temp\content\* /q /s >>c:\temp\contentBackup\ContentBackup_%yyyy%_%mm%_%dd%\copylog.txt

echo step 5 show all the files in the source that are about to be copied
dir \\sourcelocation\c$\website\content /s >>c:\temp\contentBackup\ContentBackup_%yyyy%_%mm%_%dd%\copylog.txt

echo STEP 6 COPY FILES TO NEW DIRECTORY >>c:\temp\contentBackup\ContentBackup_%yyyy%_%mm%_%dd%\copylog.txt
xcopy \\sourcelocation\c$\website\Content c:\temp\content\ /y /e /c >>c:\temp\contentBackup\ContentBackup_%yyyy%_%mm%_%dd%\copylog.txt
echo STEP 7 DELETE COPYCONTENTYES.TXT FILE IN LIVE DIRECTORY >>c:\temp\contentBackup\ContentBackup_%yyyy%_%mm%_%dd%\copylog.txt
del c:\temp\content\CopyContentYes.txt >>c:\temp\contentBackup\ContentBackup_%yyyy%_%mm%_%dd%\copylog.txt


Things I learned while creating this

  •  Del command with any of the /a switches will only delete files with those attributes (not including those files)
  • > will pipe out your results to a log
  • >> will append to an existing log
  • How to create a directory with using a date (this script creates a directory called ContentBackup_2008_04_08)
  • del /q suppresses confirmations and xcopy /q is quite mode (doesn’t show the files being copied)

Written by matt

April 8, 2008 at 8:27 am

Posted in Batch,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: