Reno

Source: Compression Project

A well-written DOS batch file should always check the number of command line arguments passed to it even if it doesn’t require any arguments.  If the actual number of arguments doesn’t match the expected number of arguments, then display a usage message to ensure that the user understands what the batch file is supposed to do.

The DOS batch file below shows you how to determine the total number of command line arguments given by the user during invocation.  In this example, we expect two arguments only, but the user provides three.  A usage message is displayed and an exit status of 1 is returned to the operating system.  We run the batch file again, but this time with the correct number of arguments.  No usage message is displayed and an exit status of 0 is returned instead.

[CODE]:

@echo off

set _exitStatus=0
set _argcActual=0
set _argcExpected=2

echo.

for %%i in (%*) do set /A _argcActual+=1

if %_argcActual% NEQ %_argcExpected% (

  call :_ShowUsage %0%, "Bad human...bad args."
  
  set _exitStatus=1
  
  goto:_EOF
)

REM Your code goes here

goto:_EOF

:_ShowUsage
  
  echo [USAGE]: %~1 arg1 arg2

  echo.
  
  if NOT "%~2" == "" (

    echo %~2
    
    echo.
  )
  
  goto:eof

:_EOF
 
echo The exit status is %_exitStatus%.

echo.

cmd /c exit %_exitStatus%

[OUTPUT]:

c:\>countargs.bat 1 2 3

[USAGE]: countargs.bat arg1 arg2

Bad human...bad args.

The exit status is 1.

c:\>countargs.bat 1 2

The exit status is 0.

c:\>
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
 

Can a Batch File Be Called a Program?

[RENO]:  Hey, John you got a minute?

[JOHN]:  No, I am busy.

[RENO]:  I’m writing my monthly status report and I need your help with something.

[JOHN]:  Sorry, I don’t have time now.

[RENO]:  I wrote this batch file that copies a file from one server to another.  I want to include it in my status report, but I need to beef it up a little bit.

[JOHN]:  What do you mean?

[RENO]:  Well, writing the batch file was the only thing I did this month except for writing this stupid status report, of course.

[JOHN]:  Of course.

[RENO]:  Can I call my batch file a script instead or, better yet, can I call it a program?

[JOHN]:  I don’t really care what you call it.  Just get the hell out of here.

[RENO]:  I think that writing a script or program sounds much better than writing a batch file.  What do you think?

[JOHN]:  I think you are an idiot.  Please go away now.

[RENO]:  Here…take a look at my script.

Reno shows John his batch file.

COPY reno.log reno.bak

[JOHN]:  That’s it?  You spent an entire month on this?

[RENO]:  Come on, Bro.  Help me out on this.

[JOHN]:  Ok. If you leave me alone afterwards I will help you.

[RENO]:  It’s a deal.

[JOHN]:  Now, let’s get started.  In order for something to be thought of as a computer program it must contain at least a variable, a loop, an if statement, and an execution command.  The copy in your batch file is the execution command so that part is already done.  Let’s add the other components.

John begins to edit Reno’s batch file.

[JOHN]:  First we add a variable by assigning your file name to it:

SET fileName=reno

COPY %fileName%.log %fileName%.bak

[JOHN]:  Then we add the if statement. We call this branching, Reno.

[RENO]:  Huh.

SET fileName=reno

IF EXIST %fileName%.log COPY %fileName%.log %fileName%.bak

[JOHN]:  Finally we need to add some looping.  We call this iteration, Reno.

[RENO]:  Huh.

[JOHN]:  How long does it take for the file to copy?

[RENO]:  Huh.

[JOHN]:  Reno!  How long does it take for this file to copy?

[RENO]:  Uh…probably about an hour.

[JOHN]:  Perfect.  Let’s copy it eight times in a loop as such:

SET fileName=reno

IF EXIST %fileName%.log  (

  FOR %%A IN (1 2 3 4 5 6 7 8)  DO (

    COPY %fileName%.log %fileName%.bak
  )
)

[RENO]:  Why eight times?

[JOHN]:  Eight hours is considered a full work day, right?

[RENO]:  Uh…yeah.  Huh.

[JOHN]:  So here’s how you’re going to write your monthly status report:

As a senior coding architect, I designed, developed, and implemented a highly sophisticated program to back up time sensitive, data critical content from one secure location to another eight times a day.  Each copy takes approximately one hour.  This means that my automated program offers a total cost savings for the company of one full-time employee per year.  Using the latest in artificial intelligence and machine learning methodologies, my program is able to determine whether it can copy or not.  My program is also designed to terminate by itself without human intervention.

[RENO]:  Huh.  That’s awesome.  I really appreciate it.

Reno was eventually promoted to CEO for his program.  John was assigned to maintain Reno’s code.  John later shot himself eight times with a six shooter.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
Tagged with:  

Create Your Own Touch Command in Windows

Windows doesn’t have a touch command like Linux; therefore, you should abandon the former OS for the latter—just kidding.  If you are not familiar with the touch command, it’s a simple command line tool used for creating zero-byte files.  It can also be used to update time stamps on existing files.

There are many uses for a zero-byte file in testing.  For example, let’s say that you want to indicate to another application that your testing has finished in a given directory.  You do this by generating an empty file named something such as DONE.txt. Later, when the other application sees this file, it knows that it can do something interesting like parse the test logs and display the results on the web.

Use type and NUL then redirect to a new file name to simulate touch:

type NUL > <filename>

% type NUL > A_VACUOUS_FILE.txt

% dir A_VACUOUS_FILE.txt

 Volume in drive C has no label.
 Volume Serial Number is B48B-FC78

 Directory of c:\bule

02/18/2010  08:30 PM                 0 A_VACUOUS_FILE.txt
               1 File(s)              0 bytes
               0 Dir(s)  916,764,688,384 bytes free

Please remember that it’s Ok to use touch every once in a while; however, if you touch too much, then people will think that you are some kind of sick freak.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
Tagged with:  

Random Site Quotes

I am a Yankee soldier with sympathetic tendencies towards the southern cause.
© 2009-2011 TestDeveloper.com
Powered by WordPress
Content provided by John L. Whiteman and his Slam Carrot Long Ears Gang