Wednesday, June 17, 2009

Script the World 1; Simple Scripting Reading from a List: article 200907

INTRO AND READING FROM A LIST


A programmer I am not! I do study scripting languages but with my positition and other tools we utilze scripting can be spread far enough apart that I have to pull the books back off the shelf or dig around google to refresh my memory. I try and practice shell scripting, PERL, and VB Script when I need to automate a task and I do keep my scripts around as I have found over the years I continually refer back to them and borrow from one another to accomplish new tasks. Speaking of "referring back" take my advice and take the time to write notes either via comments in the script itself or in a text file in the same directory where you keep the script about the purpose of the script, how it works, why you used it; and WHERE! Over the past 10 years I have become better at this but I also still get bit by digging up scripts years back and have to re-figure why I did what I did.

The stuff I am going to present here is actually a bit embarrassing I mean the stuff I write is SIMPLE! There is also quite a bit of bailing wire and bubble gum involved which leads me to say; test your script in a non-production, non-critical environment before ever using it in a place that could harm your company and possibly your employment status.

As stated this is simple stuff but like me there may be others that need a little idea or hint on how to get things going for themselves.

In this post I am going to show what I do to read from a list and execute a command against each item in that list in a Microsoft Windows environment using a batch file as your executable. In this example I am going to do a ping command against a list of URLs. DON'T BLINK!


STEP 1: Select or create a directory to work from. I like to keep the items below in the same folder, it makes calls that much easier.

STEP 2: Open NOTEPAD.EXE and make a list of urls you want to ping; for example

google.com
slashdot.org
bassproshops.com

For this example save the document as LIST.TXT; and place LIST.TXT in the directory from STEP 1.

STEP 3: Open NOTEPAD.EXE and type in the following:

FOR /F %%A IN (LIST.TXT) DO PING %%A
Save this file as PINGURL.BAT in the directory from step one.
You are done! Execute the script in the method you prefer. For this example lets lets do it from the command prompt.
BTW: Jing is a handy little program. I am not ready to give it a rating but below is a link to the video of me executing the script and showing you where the files are stored.


One last little tip. If you want to spit the scripts output to a text file run the command with ">" and a file name for the output.

c:\pingurl>PINGURL.BAT > OUTPUT.TXT
Remember two big rules:
1. Take the time to write notes
2. Test your script in a non-production, non-critical environment
.

No comments:

Post a Comment