Some patterns for MsDos and/or Windows NT/2000/XP Shell Scripting: An excellent book is ''Windows NT Shell Scripting'' by Tim Hill. Copyright 1998. ISBN 1-57870-047-7 . Chapter 5 "A Scripting Toolkit" includes many patterns. http://www.acmesoffware.com/acme/Reference_BatchFile.asp ---- '''Silence commands using @, instead of echo off''' The echo command can toggle itself at unexpected times. For instance, if you try to echo an empty string, you may toggle the echo command. Also, many scripts toggle echo, but do not restore the original setting at the end of the script. Some programmers find it easier to prepend commands with @ than to use echo off. For example, @dir When you use multi-line commands in the Windows NT shell scripting language, the @ signs go in strange places. For example: @for /f %%I in ( 'dir ^| find " Directory of "' ) do @( echo %%KT ) ---- '''Test the language version at the top of the script''' Windows NT shell scripts have many features that MsDos scripts lack. These features make it much easier to program in the Windows NT shell scripting language. Unfortunately, older command shells behave strangely when they try to execute the new-fangled scripts. A guard clause can help: @if not "%OS%"=="Windows_NT" goto EXIT ---- '''Function-level scoping''' The Windows NT shell scripting language allows function-level scoping. The setlocal, pushd, popd, and endlocal statements enable function-level scoping. '''Tunnel variables out of functions''' It is good to have local variables. The Windows NT shell scripting language lets you return results by setting variables in the ''calling'' routine's environment ''if and only if'' you set them on the same line as your endlocal statement. For example: @if not "%OS%"=="Windows_NT" goto EXIT @REM Set local scope, initialize return variable. @setlocal & pushd & set RET= @REM Do something @popd & endlocal & set RET=%RET% @goto :EOF :EXIT ---- '''Return to old working directory''' In most Unix shell scripting languages, it is easy to do something like: ; change directories cd somewhere ; do something ; ; return to the previous working directory cd $old_pwd This is harder in MsDos and the Windows NT/2000/XP Shell Scripting language. Fortunately, in the Windows NT/2000/XP Shell Scripting language, you can write a batch file something like: @if not "%OS%"=="Windows_NT" goto EXIT :CLIENT_CODE @REM Set local scope, initialize return variable. @setlocal & pushd & set RET= @REM record the current directory call :GET_CURRENT_DIR set OLD_DIR=%RET% @REM change directories cd /d somewhere @REM do something @REM return to the previous working directory cd /d "%OLD_DIR%" @popd & endlocal & set RET=%RET% @goto :EOF :GET_CURRENT_DIR @REM Gets the path to the current directory @REM (per the dir command). @REM Returns the path via the %RET% variable. @REM This routine assumes that neither @REM the disk volume's name nor its serial number @REM includes the string " Directory of " @REM Set local scope, initialize return variable. @setlocal & pushd & set RET= @for /f "tokens=2,*" %%J in ( 'dir 2^>^&1 ^| find " Directory of "' ) do @( set RET=%%K GOTO :DONE_GET_CURRENT_DIR ) :DONE_GET_CURRENT_DIR @popd & endlocal & set RET=%RET% @goto :EOF :EXIT ---- '''Empty out a file''' Sometimes you need to delete the contents of a file, but not delete the file itself. This code replaces a file with a zero-byte file: @REM Replace these files with 0 byte files. @REM This code assumes that there is no file named ''. @REM Because there is no file named '', @REM the type command generates an error. @REM This error is eaten by the NUL device. type '' 2> NUL 1> "C:\Parent Folder\Child Folder\Filename.txt" type '' 2> NUL 1> "C:\Parent Folder\Child Folder\Another file.txt" ---- http://www.robvanderwoude.com/batchstart.php http://www.robvanderwoude.com/variableexpansion.php http://www.robvanderwoude.com/variableexpansionbug.php ---- See also: UnixShellPattern, CategoryUnixShellPattern