Wednesday, March 12, 2008

Take your scripts and commandline apps with you

Interesting thing about environment variables. They can be temporarily altered on a program by program basis. Dos prompts are a fine example of this. And with a program like Console you can wrap up a nifty tweak to the %PATH% variable.
Assuming you have your batches and console apps in one directory(\Data\bin\) you can run a script that would look like this...

:: Pulls drive letter from CD root variable
:: %CD:~0,1%
:: Does a string operation then assigns
@IF %CD:~0,1% EQU D SET USBroot=D:
@IF %CD:~0,1% EQU E SET USBroot=E:
@IF %CD:~0,1% EQU F SET USBroot=F:
@IF %CD:~0,1% EQU G SET USBroot=G:
@IF %CD:~0,1% EQU H SET USBroot=H:

This batch script if ran from console on a usb drive will pull the drive letter from the %CD% environment variable, and through a series of IF statements will set the drive letter based on %USBroot%

Modify the code to suit your USB drives folder hierarchy. And start Console with the following commandline switches:
Console.exe -c "/k \Data\Bin\Drive.bat&CD \"

Which will run the batch script and then bring you to the root directory of the USB drive. If you have Pstart to launch console for you, you'll have to do things this way as well. I tried but apparently regular expressions are not performed by Pstart for setting environment variables. And the %pdrive% variable only works in the general and advanced tabs. But mind not, this is a great fix to a problem that wasn't really that complicated anyway.
Just a not I didn't use Console2(beta), if you do it should be just as easy for you to set it as well.