RSS

the .bat framework

02 Aug

As I look back, I can’t believe how far I made it in IT before I saved my first .bat file.  Through multiple programming classes and over two years of enterprise level programming before I saved that simple text file as a .bat file.  The mindset when writing scripts is usually very sequential and single threaded in nature. In that regard, I hope dodging the .bat for as long as I did has help me become a better programmer. My first was about 1 year ago, and since then I have created hundreds of .bats, mostly for file transfers.

Batch scripting is automation in its simplest form. Say every night before you go home you save the most recent copy of all of your spreadsheets to a backup drive.  Right-click copy/past done.  Why on earth would you want to automate that?  Because anything that can go wrong will.  The night you forgot to copy will be the same day you were slammed with all kinds of work, the same day you got tons of work done on those spreadsheets and the same day that your hard drive fails.  The next morning you realize, that for some reason 2 months ago you decided to drag and drop to your spreadsheets instead of copy/pasting and windows decided you just wanted shortcuts and not the actual file.  So now that you are scared lets make a .bat file to copy all of your spreadsheets.

create a new text file and put this into it

copy /y "c:workspreadsheets*.xls*" "d:thumbdrive"

now save the file as scripto.bat.  Then you might choose to setup a scheduled task at 6pm to run scripto.bat.  And there you go, piece of cake. Painfully simple automation right at my fingertips for so long and I never took advantage.

pause

Scroll any direction to continue.

up and you are off and running with your own .bat ideas or down because you are interested where I will take you.

So now that you have your spreadsheets in two locations you are protected against disk/computer failure. Lets cover some of the basics then kick it up a notch.

c:
cd "test"
copy a.txt b.txt
if %1 GTR 3 goto donotdel
del b.txt
del a.txt
:donotdel
exit 0

The %1 is a parameter.  If you open up the command prompt, navigate to the directory of your batch file the type >batchfile.txt 20 you will pass in “20” and then the “goto donotdel” will get processed and you’ll skip the delete steps.

So back to our theoretical example. You think you backup strategy is awesome, heck maybe it even worked once.  So you decide to push it on all of your co-workers and they also like the idea.  Your boss calls to give you a raise and you drop the phone on the keyboard which happened to have one of those spreadsheets open.  Now the IT department instead of having expenses totaling $92,020 have $920,220 in expenses.  You save, close and your backup runs at 6pm overwriting your last good copy.  Or maybe it is worse and you deleted an entire column and have no way of knowing what the numbers were.

So lets have that .bat achieve the spreadsheets.  Think of it as the poor mans source control.

for %%A in (*.xls*) do copy /y "%%A" "D:test%%A"

If you use this command what you are doing is looping through filenames matching *.xls*. Source control in a basic form allows you to rollback you changes by retrieving any version of a file.  You won’t achieve that functionality but you will be able to rollback to any given day if you incorporate a timestamp in the file.  So with this example you set a system variable to the date and create a directory to place the files into.

D:
cd test
set YYYYMMDD=%date:~-4,4%%date:~-10,2%%date:~-7,2%
if not exist %YYYYMMDD% mkdir %YYYYMMDD%
for %%A in (*.xls*) do copy /y "%%A" "D:test%YYYYMMDD%%%A"

Scripting and automation is easy, but they create new problems.  If you learn to rely on automation without periodically making sure it is working you will suffer the consequences.  For example, if you installed a dvd burner into you computer then every day after that you come in and plug in your flash disk to pick up your spreadsheets you will be disappointed they won’t make it do you newly mapped E: drive.  If you had successfully restored files a few times you could have slightly lost the importance of saving your spreadsheets frequently.

Once you weight in the costs to the benefits I believe you will make the right choice and continue to automate.  Choosing to automate with batch files means your batch files will multiply rapidly.  When they multiply you will also find that you are creating most of the lines over and over again with small changes.  Its the copy/paste school of scripting.  Its difficult to avoid but you should try.  What frameworks in programming are all about is avoiding having to re-invent the wheel each time you need to travel. With a framework we can hop in the car and go.

When I analyze the example above to determine how I can avoid copy and pasting this code I think what the next batch file will look like.  Then compare the two and see what changed.  This way you can determine what the parameters should be.  The next step to build you framework is to replace the 4 possible parameters( Drive, Current Directory, extension, destination )

%1
cd %2
set YYYYMMDD=%date:~-4,4%%date:~-10,2%%date:~-7,2%
if not exist %YYYYMMDD% mkdir %YYYYMMDD%
for %%A in (*%3*) do copy /y "%%A" "%4%YYYYMMDD%%%A"

Save the file above and use this to pass in all the parameters.

CALL dailybkp.bat D: test .xls D:test

I know its a very loose usage of the term “framework” but dailybkp.bat can be used as a foundation for development.

Advertisements
 
Leave a comment

Posted by on August 2, 2010 in Uncategorized

 

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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: