John's Computer Journal: batch
Showing posts with label batch. Show all posts
Showing posts with label batch. Show all posts

Saturday, May 19, 2007

Batch function library loader

I've put together a few functions that I find useful when doing batch scripting. I've attached the library of functions bellow. Included in the library are functions for multi-line out, file-prepending, and unsetting environment variables.

Syntax

call function :[function_name] [function_parameters]

[function_name]       - name of the function.
[function_parameters] - arguments to pass to the function.

Read the function documentation within the function.bat file for further information on variables specific to each function. Examples of each function included with the attached file are provided below.

Examples

unset
call function :unset path temp tmp username

The above example executes the function unset with the parameters: path, temp, tmp, and username. After executing the unset function each of the environment variables path, temp, tmp, and username will no longer exist.

prepend
call function :prepend history 1st I did this
call function :prepend history 2nd I did this

The above example executes the function prepend twice with two parameters: history and a string. After executing the prepend function the file named history will have the line: "2nd I did this" (without the quotes) as the first line of the file and the line: "1st I did this" (without the quotes) as the second line of the file.

out
goto output

Hello %username%,
Do you know the ^%TIME^%?

:output

call function :out %0 output  

The above example builds a multi-line output and executes the function out with two parameters: the current filename and the target output. Notice the % symbols are escaped for TIME to prevent out from expanding the %TIME% variable. After executing the out function the following will appear on the display:


Hello John,
Do you know the %TIME%? 

Note: the results of the out function can also be redirected to a file by appending the > operator.

Adding Functions

New functions can be added to the function library by adding a label and goto :EOF pair where the label is the name of the new function. For example the following code will add the hello function which will simply display Hello %1 where %1 is the first argument passed to hello.

:hello
    echo Hello %1
goto :EOF

After adding the above code to the function.bat file and typing:

call function :hello World!
call function :hello %username%!

will display:

Hello World!
Hello John!
A good collection of functions ready to be copied and pasted directly into the function file can be found at Ritchie Lawrence's Batch Function Library.

Download

function.bat

Monday, April 23, 2007

Simple Batch script Program Installer

The following batch script will install a program into the Program Files directory on the computer. The batch script will create and register an uninstaller, and create a shortcut in the Start Menu. To uninstall the program simply click the program's entry in the "Add / Remove Programs" control panel.

Usage

Pass the script the name of a directory containing an executable. The script assumes the directory and executable have similar names. If the names don't match, rename either the directory or the executable.

Requirements

The batch script will run on any Windows operating system. I have tested the batch script on Vista. Windows XP and 2000 should also work. To run the script on Windows 9x modify the startMenu variable to point to Windows 9x's start menu location.

Why?

Some programs don't have installers. Sometimes I'll put a program someplace and forget I have it because it doesn't show up in the start menu or in the "Add / Remove Programs" control panel.

Download

quickInstall.bat

Friday, April 20, 2007

Nested Environment Variables

The following workaround example shows how to nest environment variables inside of each other in a batch file. The nested variables can be used for specifying sub-string indices or replacements dynamically during runtime.

For example to remove the current directory from the path environment variable:

set path=%PATH:%CD%=%

does not work. However, the following code:

for /f "delims=" %%i in ('echo %%PATH:%CD%^=%%') do set path=%%i

will work fine.

References

Change the System Path from CMD

The batch file provided below will change the system path from the command line. Pass the batch script a directory and it will add it to the system path. If the directory is already in the path it will remove it.

System Requirements

The script runs on any MS Windows OS that has uses the registry to store the system path. Windows NT, 2000, XP, and Vista all store the system path in the registry. For windows Vista the script will need to be run as an Administrator.

For information on how to run a batch file as an Administrator in Vista check out: http://www.vistaclues.com/run-a-batch-file-as-an-administrator/

Why?

Having important executables in the path can make command line work easier. Some applications that have been ported from the Linux environment are easier to work with if the system paths are setup correctly. Developers and administrators may find this script helpful.

Limitations

After updating the path the system does not refresh the current path variable to reflect the change. The system must be manually refreshed.

References

Download

UpdatePath