Wednesday, May 6, 2009

Batch Scripts Perl scripts and Bears Oh My!!!

I don't know but maybe I'm the only one who has these problems but I don't know how many times I'll run a batch script and find that it's managed to start run and close before I even saw the screen come up, now of course you can get around this by putting a pause in your script, but honestly I don't always want to pause I just want to see that it successfully completed and not wait for me to hit enter.

One way to work around this is by following this steps:

Purpose: The point of this is to make the window stay open when you run a batch file.
  1. Windows Key + R
  2. type regedit in the window
  3. Back up Registry
    1. File
    2. Export>
    3. Save somewhere you can find it in case you need to revert
  4. Click the Plus sign next to HKEY_CLASSES_ROOT
  5. scroll to batfile
  6. Click the little plus next to it
  7. Click the little plus next to shell
  8. Click the little plus next to open
  9. Click the folder command
  10. For the REG_SZ on the right window double click the (Default)
  11. Make sure the string looks like this:
    • "cmd.exe" /k "%1" %*


Now to explain, what I have set it to do is to basically keep the window open after running so you are returned to a command prompt.

Another place I find this to be very helpful is for example if you're using perl scripts right?

How many times do you run a perl script (to test) just by double clicking on it, and it opens calls an error and closes before you get a chance to even look at it?

Almost all the same steps as you'll see:
  1. Windows Key + R
  2. type regedit in the window
  3. Back up Registry
    1. File
    2. Export>
    3. Save somewhere you can find it in case you need to revert
  4. Click the Plus sign next to HKEY_CLASSES_ROOT
  5. scroll to Perl
  6. Click the little plus next to it
  7. Click the little plus next to shell
  8. Click the little plus next to open
  9. Click the folder command
  10. For the REG_SZ on the right window double click the (Default)
  11. Make sure the string looks like this:
    • "cmd.exe" /k "perl %1" %*
Of course yours may have looked a little different BEFORE but in order to enable the window to stay open when you run the script you basically use the same thing what we're saying is open a command prompt (and stay open), /k says "run the following command" which happens to be the perl %1 and as a reminder the %1 is a parameter passing in what was called to it.

The beauty of this is it can be applied to almost any script/exe etc. additionally you could add other arguments, for example if you want your perl scripts to always run in "debug" mode to give you the most information when you run a script (in case it fails) that would be a -w right in front of the %1:
  • "cmd.exe" /k "perl -w %1" %*
I hope you enjoyed and let me know how it works for you!!!