Tag Archive


3D 3dprinting android ant BigData bitcoin Browsers C/C++ cryptocurrency CSS dd ddrescue dogecoin DOS editors find Games Git hadoop html html5 Java Linux litecoin node perl Postgres Programming Python scripting Shell SQL Swing TOTK Utilities utilization vi Video Web Web Design Wifi Windows Wordpress XML Zelda

DOS: Capture return values from commands

When I am writing build automation scripts, there are times that I have to insure that the previous command completed
with no errors. I have found that ERRORLEVEL gets filled in with the return value from a command.

dir %1

if (%ERRORLEVEL%) == (0) echo Found File: %1
if (%ERRORLEVEL%) == (1) echo Missing File: %1

So if you save the previous in a file and run the command with one (1) argument, it will display the output of the dir command
but also print either “Found file: ” or “Missing File: “.

Dos: Adding commands to the PATH

When you install a new piece of software that does not add itself to the Windows PATH variable, you may find your self
needing it to be run from a command prompt. You could type the full command path each time you run the executable, but
it may be easier to add the directory to the PATH variable so that it is always available.

To add it for the current command prompt:

set PATH=%PATH%;"C:Program FilesSometool"

This will add the variable to the current command prompt environment but will go away once you close the window and will not
be available to other prompts unless you type it in again.

To add it to the Windows Environment, you can use the My Computer->Properties menu to select the “Advanced System Settings”
in Windows 7 to then select the “Environment Variables…”. Now the choice you have is to add it to the System variables or
the User variables for.

If you choose the System varables you will be adding it to the system as a whole and any other user who logs in will have
the updated path. If you add it to the User variables, then it is only available to your user.

Whichever you choose, select the Path entry and click the “Edit…” button. This will pop up a dialog that will let
you add to the “Variable Value” entry. Now you will need to decide whether you want your new path at the beginning, end,
or somewhere in the middle. You might add it to the beginning if it is being used to override an existing command. You
may add to the end if there is no conflict with other installs. And finally, you may need to choose the middle if there
are some commands you need to override but others that need to be left alone.

Entries are separated by the semi-colon “;” character in DOS.

DOS: Check for empty environment variable

I was writing a batch file the other day and needed to change the argumets to a command based on if the user had specified his password on the command line.

SET PASS=somepassword
: or
:SET PASS=
if x%PASS%= X (
  SET PASSARG=--password somepassword
) else
(
  SET PASSARG=
)

commandtorun %PASSARG%

Now if the user comments out his password, the script will make the PASSARG environment variable be empty or if they set a password the passarg variable will be filled in.

Retrieve drive letter on Windows

The other day I had to write a script to change to a working directory and back to the original directory. The problem was they were on different drives on Windows. So I had to find a way to capture the drive letter
and switch back to that drive. When using the following command,

cd F:somedir

you are not moved to the “F:” drive without an explicit “F:” command.

The cd Command on windows will show the current directory location. Using some string manipulation on that and capturing it to a variable allows us to go back to that drive.

REM Capture the current drive letter
set ORIGDRIVE=%cd:~0,2%

F:
REM some other scripting things here

REM Return to the original drive letter
ORIGDRIVE

The first line captures the current drive letter by specifying the first character with 0 and the ,2 states to pick up the next 2 characters which would hold the “C:” or whatever drive you started on.