DOS Batch file programming


DOS writes temporary files to the %temp% directory every time a | (pipe) is used, and if the temp variable is not set or set to an invalid directory, DOS will attempt to write to the current directory. This will lead to acute problems if the current drive is full, a CD-ROM, floppy, or otherwise removable or write protected, resulting in batch slowdown or failure.The temp variable is required wherever you see a pipe or %temp%. Lines that are strictly optional are preceeded by, ':: %opt%'; remove this to activate code. Unless otherwise indicated, all solutions tested in English language MS-DOS 6.22 (without spaces in directory/file names). Some solutions are not language independent and will require adaptation to work in another language. Vital: The %temp% directory must not have an extension; c:\temp.dir.


  • Check if a batch parameter contains wildcards.
  • Get the date or time without having to press [Enter].
  • Find a file using a partial file name.
  • Omit specific files from DIRouput.
  • Redirect a command's ouput to a file & place date and time into a file.
  • Get the date and time into variables.
  • {Revised 30 April 2005}
  • Redirect a batch file's ouput to a file.
  • Add a directory to the path.
  • Remove a specific line from an ASCII (plain text) file.
  • Get user input.
  • Get user input with verification and option to quit.
  • Get user input with doskey.
  • Get the complete file name of a running batch file.
  • Determine if a path is used when a batch file is run.
  • Insure an absolute path is used to run a batch file.
  • Reboot the computer from a batch file.
  • Check how many hard disks are present.
  • See which is the last valid drive letter.
  • Get drive letter assigned tothe ramdrive.
  • Save and restore the path.
  • Convert a string to all uppercase letters.
  • Check if ansi.sys is installed.
  • Check if there exists any sub-directories.
  • Check if there an extension on a file name in a batch parameter.
  • Find out if Windows is running, from a batch file.
  • Place a banner at the top of the screen.
  • Create a simple menu without using ansi.sys.
  • Create a simpler menu without using ansi.sys.
  • Check if a file is in the path.
  • Place the current drive and directory into variables.
  • Get the full path to a running batch file, even if none is given.
  • Save and restore a file's attributes.
  • Get the absolute path to a running batch file.
  • Parse a string.
  • Save and restore the prompt.
  • Make a simple counter.
  • Run a program after a 10 second on-screen count down.
  • Not run a program after an on-screen count down (without ansi.sys).
  • Count up to a specified number.
  • Blank the screen.
  • Format a drive without user input.
  • Determine that a drive is not a network, CD-ROM or RAM drive.
  • Check if a drive is valid.
  • Check if a drive is valid and writeable.
  • Check if there is a disk in the a: drive; with user prompt and time out.
  • Get drive type: hard, floppy, network/CD-ROM, RAM, or unknown.
  • List all local hard drives including megabytes free and megabytes total.
  • Check if the %temp% directory is full.
  • Get the length of a simple(single word) string.
  • Check if the root directory is current.
  • Make a batch file keep a limited count of how many times it's run.
  • Get the basename of a file.
  • Get the CD-ROM letter.
  • Get bytes required by current environment.
  • Getserial number of drive.
  • Get volume label of drive.
  • Place command output or line of text into a variable with a helping file.
  • Get filename of an existing file into a variable.
  • Get directory of an existing file into a variable.
  • Get fullpath of an existing file into a variable.
  • Get drive letter of a full path into a variable.
  • Get file extension into a variable.
  • Get file date into a variable.
  • Get the size of a file into a variable (1).
  • Get the size of a file into a variable (2).
  • Get file time into a variable.
  • Edit the path at the DOS prompt.
  • Isolate the first (non-system/hidden) file in a directory.
  • Display boot drive letter or place into a variable.
  • Place first character of string into a variable.
  • Display current drive letter or place into a variable.
  • Remove 1st character of string and place remainder into variable.
  • Make a batch file self-delete.
  • Make a batch file self-delete with doskey.
  • Parse system date with / (slash) as separator.
  • Parse system dates without / (slash) as separator.
  • Get space free on drive into variable.
  • Get up to nine words of first line of input file into variables.
  • Compare two numbers and determine which is larger/smaller.
  • Get newest file in current or specified directory into a variable.
  • Display all logical hard drives.
  • Get number of files in current or specified directory into a variable.
  • Get number of lines in a file containing a specific character/word into a variable.
  • Get computer name into a variable (1).
  • Get computer name into a variable (2).
  • Get computer name into a variable (3).
  • Get computer name into a variable (4).
  • Check for network connection through network adapter.
  • Get IP address.
  • Get user name.
  • Get physical address or MAC.
  • Get physical address or MAC(2).
  • Print special characters to the console (screen) or to a file
  • Get a pseudo random number (1-99) into a variable
  • Remove (zero byte) empty directories.

  • Check if a batch parameter contains wildcards.
    FOR %%f in (%1) do IF %%f==%1 GOTO nowilds
     
  • Get the date or time withouthaving to press [Enter].
    FIND.EXE /V "): " ECHO. |DATE |FIND.EXE "Current date"
    ECHO. |TIME |FIND.EXE "Current time"
    ::
    Or, the following which should be language independent.
    ECHO. |DATE |FIND.EXE /V "): "
    ECHO. |TIME |FIND.EXE /V ": "
     
  • Find a file using a partial file name.
    This is also useful for finding 'other' strings in theDIR output.

     
    DIR/A-D/-P *.* %2 %3 %4 |FIND.EXE /I %1 |MORE.COM
     
  • Omit specific files from DIR output.
    :: Omit .bat and sys files, without /B switch.
    DIR/A-D/S/-P |FIND.EXE /V " BAT " |FIND.EXE /V " SYS "
     
    :: Omit .bat and sys files, with /B switch.
    DIR/A-D/B/S/-P |FIND.EXE /V ".BAT" |FIND.EXE /V ".SYS"

     
  • Redirect a command's ouput to a file & place date and time into a file.
    Redirection works with many, but not all DOS commands.
     
    :: Creates a new file.
    ECHO. |DATE |FIND.EXE "Current date" >results.txt
     
    :: Creates a new file, or appends to an existing file.
    ECHO. |TIME |FIND.EXE "Current time" >>results.txt

     
  • Get the date andtime into variables.
    :: datetime.bat
    :: also works with mo/da/year format
    :: revised 30 April 2005
    @echo off
    echo @prompt set time=$t$_set date=$d$_>%temp%.\$dattim1.bat
    %comspec%/e:4096/c%temp%.\$dattim1.bat>%temp%.\$dattim2.bat
    call %temp%.\$dattim2.bat
    for %%v in (%time%) do set time=%%v
    echo set day=%%1>%temp%.\$dattim3.bat
    echo set date=%%2>>%temp%.\$dattim3.bat
    call %temp%.\$dattim3.bat %date%
    del%temp%.\$dattim?.bat
    ::

     
  • Redirect a batch file's ouput to a file.
    COMMAND/CTEST.BAT >results.txt
     
  • Add a directory to the path.
    PATH drv:\DirName;%path%
    :: Or
    PATH %path%;drv:\DirName

     
  • Remove aspecific line from an ASCII (plain text) file.
    FIND.EXE /V "String" <anyfile> %temp%.\anyfile.tmp
    COPY/Y %temp%.\anyfile.tmp anyfile
    DEL %temp%.\anyfile.tmp

     
  • Get user input.
    Here is the standard method widely accredited to batch file pioneer, Tom Lavedas,
    which has been adapted to work no matter which directory is current. In otherwords,
    it will not slow down or fail if current is a floppy, write protected, full or otherwise
    unavailable. This DOS batch solution is broken in NT.

     
    :: input.bat
    :: adaptation of a routine by Tom Lavedas
    @ECHO off
    IF "%temp%"=="" ECHO. the temp variable must be set
    IF "%temp%"=="" GOTO end
    IF not exist %temp%.\nul ECHO. temp variable is invalid
    IF not exist %temp%.\nul GOTO end
    IF exist enter.bat ECHO. enter.bat in current directory.
    IFexist enter.bat GOTO end
    SET input=
    ECHO Type input and press [Enter].
    FC.EXE CON nul /LB1 /N |DATE |FIND.EXE "1:"> %temp%.\setinput.bat
    ECHO :loop> %temp%.\enter.bat
    ECHO IF not "%%input%%"=="" SET input=%%input%% %%5>>%temp%.\enter.bat
    ECHO IF "%%input%%"=="" SET input=%%5>> %temp%.\enter.bat
    ECHO SHIFT>> %temp%.\enter.bat
    ECHO IF not "%%5"=="" GOTO loop >> %temp%.\enter.bat
    PATH> %temp%.\respath.bat
    PATH%temp%.\;%path%
    FOR %%c in (call del) do %%c %temp%.\setinput.bat
    FOR %%c in (call del) do %%c %temp%.\respath.bat
    DEL %temp%.\enter.bat
    IF not "%input%"=="" ECHO. Input was: "%input%"
    :end
    ::

     
  • Get user input with verification and option to quit.
    This is another version of the previous batch. Instead of utilizing the %temp%
    directory from the current, it changes to the %temp% directory (andback again).
    Verification and option to quit added.

     
    :: input2.bat
    :: get user input with verification and option to quit
    @ECHO off
    ECHO @PROMPT $N:$_CD $P> %temp%.\setback1.bat
    %COMSPEC%/E:2048/C%temp%.\setback1.bat> %temp%.\goback.bat
    DEL %temp%.\setback1.bat
    FOR %%c in (%temp%.\ CD) do %%c %temp%.
     
    :again
    SET input=
    ECHO. Type input and press [Enter].
    FC.EXE CON nul /LB1 /N |DATE |FIND.EXE "1:"> setinput.bat
    ECHO :loop > enter.bat
    ECHO IF not "%%input%%"=="" SET input=%%input%% %%5>>enter.bat
    ECHO IF "%%input%%"=="" SET input=%%5>> enter.bat
    ECHO SHIFT >> enter.bat
    ECHO IF not "%%5"=="" GOTO loop >> enter.bat
    FOR %%c in (call del) do %%c setinput.bat
    DEL enter.bat
    IF not "%input%"=="" CHOICE.COM /N/CYQN " Is this correct? Yes, No or Quit "
    IF not "%input%"=="" IF errorlevel 3 GOTO again
    IF not "%input%"=="" IF errorlevel 2 SET input=
     
    :end
    FOR %%c in (call del) do %%c %temp%.\goback.bat
    IF not "%input%"=="" ECHO. Input was %input%
    ::

     
  • Get user input with doskey.
    A batch by Tom Lavedas slightly modified to write to %temp% instead of
    current, eliminated use of tildes (~), shortened variable name. Changed
    'echo' to 'echo.' and 'now:' to 'now.' which improves readability of
    initial output line. And switched order offirst two lines after @echo off

     
    :: InputLin - A routine to get a line of text from user.
    :: Tom Lavedas <lavedas@pressroom.com>
    :: http://www.pressroom.com/~tglbatch/
    @echo off
    doskey [4]=echo $*$G %temp%.\tmp.txt$T exit >nul
    echo. Enter your data now.
    fc con nul /lb1 /n | find /n " 1: " | %comspec% /k > nul
    doskey 1:=echo set Input=$*$G %temp%.\tmp.bat$T exit
    %comspec% /k < %temp%.\tmp.txt > nul
    for %%v in ([4]1:) do doskey %%v=
    for %%v in (%temp%.\tmp.bat del) do call %%v %temp%.\tmp.??t
    for %%v in (%for example%) do echo Input Line: "%input%"

     
  • Get the complete file name of a running batch file.
    This will return the complete batch file name, whether an extension is
    used or not. However, if a path is used, it will also be included in
    the %src% variable.

     
    ECHO %0? |FIND.EXE /I".bat?" >nul
    IF not errorlevel=1 SET src=%0
    IF     errorlevel=1 SET src=%0.bat

     
  • Determine if a path is used when a batch file is run.
    In addition to getting the complete file name, this code checks to see
    if the file exists as passed to the batfile. If it doesn't exist, then
    a path to the batch was not entered, or the batch file is not in the
    current directory.

     
    ECHO %0? |FIND.EXE /I ".bat?" >nul
    IF not errorlevel=1 SET src=%0
    IF     errorlevel=1 SET src=%0.bat
    IF not exist %src% FOR %%c in (ECHO GOTO:end) do %%c. %src% not in current directory; path required
    :: or
    IF not exist %src% FOR %%c in (ECHO PAUSE GOTO:end) do %%c. %src% not in current directory; path required

     
  • Insure an absolute path is used to run abatch file.
    This code does this by insuring that the complete path is entered from
    the start.

     
    ECHO %0 |FIND.EXE ":\" >nul
    IF errorlevel=1 FOR %%c in (ECHO GOTO:end) do %%c  complete path required
    ECHO %0? |FIND.EXE /I ".bat?" >nul
    IF not errorlevel=1 SET src=%0
    IF     errorlevel=1 SET src=%0.bat

     
  • Reboot the computer from a batch file.
    These work in MS-DOS 6.22/Windows 3.11. Use at your own risk.
     
    :: warmboot.bat
    @ECHO E 0040:0072 34 12 >%temp%.\reboot.dat
    @ECHO G=FFFF:0000 >>%temp%.\reboot.dat
    @DEBUG.EXE <%temp%.\reboot.dat >nul
     
    :: coldboot.bat
    @ECHO G=FFFF:0000 | DEBUG.EXE >nul

     
  • Check how many hard disks are present.
    This will show how many hard drives are installed andprovide a summary of each.

    UNFORMAT.COM /L /PARTN |FIND.EXE "Drive #"
     
    Typical output:
    Drive # 80h has 3695 cylinders, 255 heads, 63 sectors (from BIOS).
    Drive # 81h has 1811 cylinders, 128 heads, 63 sectors (from BIOS).

     
    If you only want the number of hard drives, use the /C switch on FIND.EXE.
     
  • See which is the last valid drive letter.
    MEM.EXE /D |FIND.EXE "LASTDRIVE"
     
  • Get drive letter assigned to the ramdrive.
    Looks for "ramdrive" in a label by default, but accepts a different string to
    search for as an argument. Message displays to screen if label is found; remove %
    signs around %>nul% to disablethis feature. Stops searching if string is
    found in a label. Does not find labels with more than one adjacent space, but this
    feature could be added.

     
    :: Begin f-ramvol.bat
    @echo off
    IF "%1"=="ReCuRs" FOR %%C in (SHIFT GOTO:loop) do %%C
    IF not "%1"=="" SET vol=%1
    IF "%1"=="" SET vol=ramdrive
    %0 ReCuRs d e f g h i j k l m n o p q r s t u v w x y z
     
    :loop
    IF "%1"=="" FOR %%C in (SET GOTO:end) do %%C vol=
    %COMSPEC%/F/CVOL %1: |FIND/I"%vol%" %>nul%
    IF errorlevel=1 FOR %%C in (SHIFT GOTO:loop) do %%C
    SET drv_%1=%vol%
    SET vol=
    :end
    ::

     
  • Save and restore the path.
     
    :: save
    PATH>%temp%.\respath.bat
    :: restore
    CALL %temp%.\respath

     
  • Convert a string to all uppercase letters.
    Here is a way to do it, sincePATH always converts to uppercase.

     
    PATH> %temp%.\respath.bat
    PATH string
    SET variable=%path%
    CALL %temp%.\respath.bat
    DEL %temp%.\respath.bat

     
  • Check if ansi.sys is installed.
    :: isansi.bat
    @ECHO off
    MEM.exe /M:ANSI |FIND.EXE "=ANSI"> nul
    IF not errorlevel=1 ECHO. ansi.sys installed
    IF     errorlevel=1ECHO. ansi.sys not installed

     
  • Check if there exists any sub-directories.
    DIR/AD/W/-P |FIND.EXE " 2 file(s) " >nul
    IF not errorlevel=1 ECHO  no sub-directories exist
    IF     errorlevel=1 ECHO  sub-directories exist
    ::
    Or
    TREE.COM DirName |FIND.EXE "No sub-directories" >nul
    IF not errorlevel=1 ECHO no sub-directories exist
    IF     errorlevel=1 ECHO  sub-directories exist

     
  • Check if there an extension on a file name in a batch parameter.
    You can use this, if the file name is passed as an existing file.

     
    IF not exist %1 FOR %%c in (ECHO GOTO:end) do %%c  file is invalid
    IF     exist %1.* SET ext=none

     
  • Find out if Windows is running, from a batch file.
    SET |FIND.EXE "windir=" >nul
    IF not errorlevel=1 ECHO  Windows is running
    IF     errorlevel=1 ECHO  Windows is not running
    :: Or
    MEM.EXE /M:WIN |FIND.EXE "WIN is not"
    IF errorlevel=1 ECHO  Windows is running
    :: Or
    MEM.EXE /C |FIND.EXE "WIN" >nul
    IF not errorlevel=1 ECHO  Windows is running
    IF    errorlevel=1 ECHO  Windows is not running
    :: Or
    MEM.EXE /D |FIND.EXE "WIN" >nul
    IF not errorlevel=1 ECHO  Windows is running
    IF     errorlevel=1 ECHO  Windows is not running

     
  • Place a banner at the top of the screen.
    This example incorporates a technique used in the previous answer.

     
    :: winbannr.bat
    :: Ansi.sysrequired; there are ten spaces between $d and Enter.
    @ECHO off
    SET |FIND.EXE "windir=" |IF not errorlevel=1 GOTO ok
    ECHO. Windows not running
    GOTO end
     
    :ok
    @ECHO on
    @PROMPT $e[0m
    CLS
    @ECHO off
    @ECHO on
    @PROMPT $_$e[s$e[H$e[1;36;44m $d          Enter $e[37mEXIT $e[36m to return to Windows$e[K$e[1;37;46m$e[u$p$g
    CLS
    @ECHO off
    ECHO.
     
    :end
    ::
     
  • Create a simple menu without using ansi.sys.  {last modified Apr. 28, 2002}
    :: menu.bat
    @ECHO off
    :loop
    FOR %%c IN (CLS ECHO ECHO ECHO) do %%c.
    ECHO.                          1. Current directory listing
    ECHO.                             in size order
    ECHO.      % 26 spaces %       2. Edit Config.sys
    ECHO.                          3. MEM
    ECHO.                          4. MS Diagnostics (MSD)
    ECHO.                          5. Exit
    ECHO.
    :: There are 20 spaces between (") and (Your).
    CHOICE/C12345/N"                    Your choice: "
    IF errorlevel=5 GOTO end
    IF errorlevel=4 FOR %%c in (MSD.EXE GOTO:loop) do %%c/I
    IF errorlevel=3 FOR %%c in(MEM.EXE PAUSE GOTO:loop) do %%c
    IF errorlevel=2 FOR %%c in (edit goto:loop) do %%c c:\config.sys
    %COMSPEC%/CDIR/A/O-S/P
    FOR %%c in (pause goto:loop) do %%c
    :end
    ::

     
  • Create a simpler menu without using ansi.sys.
    See a professional looking version (with borders) of this menu (new window)

     {last modified Apr. 28, 2002}  
    ::menu2.bat
    @ECHO off  
    :loop
    CLS
    ECHO.
    ECHO.
    ECHO.
    ECHO.                          1. Current directory listing
    ECHO.                             in size order
    ECHO.      % 26 spaces %       2. Edit Config.sys
    ECHO.                          3. MEM
    ECHO.                          4. MS Diagnostics (MSD)
    ECHO.                          5. Exit
    ECHO.
    :: There are 20 spaces between (") and (Your).
    CHOICE/C12345/N"                    Your choice: "
    IF errorlevel=5 ECHO.
    IF errorlevel=5 GOTO end
    IF errorlevel=4 GOTO MS Diagnostics (MSD)
    IF errorlevel=3 GOTO MEM
    IF errorlevel=2 GOTO DIR listing
    EDIT c:\config.sys
    GOTO loop
     
    :MS Diagnostics (MSD)
    MSD.exe /i
    GOTO loop
     
    :MEM
    MEM.exe /a/c
    ECHO.
    PAUSE
    GOTO loop
     
    :DIRlisting
    %COMSPEC%/cDIR/A/O-S/P
    PAUSE
    GOTO loop
     
    :end
    ::

     
  • Check if a file is in the path.
    :: ckpath.bat
    @ECHO off
    IF "%rec%"=="01" GOTO recurs
    IF "%1"=="" FOR %%c in (ECHO. GOTO:end) do %%c filename required
    IF exist %1 FOR %%c in (ECHO. GOTO:end) do %%c %1 in current directory
    SET rec=01
    SET fil=%1
    %0 %path%
     
    :recurs
    SET rec=
    IF"%1"=="" ECHO. %fil% not in current directory or path
    IF "%1"=="" GOTO end2
    IF exist %1.\%fil% GOTO found
    SHIFT
    GOTO recurs
     
    :found
    IF exist %1%fil% ECHO. %1%fil%
    IF exist %1\%fil% ECHO. %1\%fil%
     
    :end2
    SET fil=
     
    :end
    ::

     
  • Place the current drive and directory into variables.
    Whether run in a sub-directory or the root directory, there will be a
    backslash included at the end of the %dir1% variable. This
    insures that a filename can always be added to or included with the
    variable. If you use WfW with 32BFA enabled, the 'IF exist DIR\nul' test is
    broken so use the "find" test as shown below, instead.

     
    :: drvdir.bat
    @ECHO off
    ECHO @PROMPT SET drv1=$N:$_SET dir1=$P> %temp%.\drvdir01.bat
    %COMSPEC%/E:2048/C%temp%.\DRVDIR01.BAT > %temp%.\drvdir02.bat
    CALL%temp%.\drvdir02.bat
    FOR %%v in (%temp%.\drvdir0?.bat) do DEL %%v
    IF exist %dir1%\nul SET dir1=%dir1%\
    ::: If you use WfW with 32-BFA, substitute the above line
    ::: with the two lines below.
    ::ECHO %dir1%\ |FIND "\\" >nul
    ::IF errorlevel=1 SET dir1=%dir1%\
    ECHO. current drive is %drv1%
    ECHO. current directory is %dir1%

     
  • Get the full path to a running batch file, even if none isgiven.
    :: findself.bat
    :: Find.exe required. Choice.com needed to find drive letter of
    :: target file. Will find itself (default), or qualified filename
    :: passed as parameter one so it can be called by another batch thusly;
    :: CALL findself %0.
    ::
    @ECHO off
    IF "%1"=="ReCuRs" GOTO ckpath
    IF "%temp%"=="" ECHO. temp variable not set
    IF "%temp%"=="" GOTO end
    IF "%comspec%"=="" ECHO. comspec variable not set
    IF "%comspec%"=="" GOTO end
    %comspec%/c
    FIND.exe "x" <nul>nul
    IF not errorlevel 1 ECHO. FIND.exe missing
    IF not errorlevel 1 GOTO end
     
    SET ext=
    SET tar=
    IF "%1"=="" GOTO nowilds
    FOR %%f in (%1) do IF %%f==%1 GOTO nowilds
    ECHO. wildcards not supported
    GOTO end
     
    :nowilds
    SET bsl=0
    ECHO. >%temp%findsel$.tmp
    IF exist %temp%\findsel$.tmp SET bsl=
    IF exist %temp%findsel$.tmp SET bsl=\
    IF "%bsl%"=="0" FOR %%c in (echo. goto:end) %%c %temp% directoryerror
    ECHO %0:> %temp%%bsl%findsel$.tmp
    FIND.EXE /I ".bat:" <%temp%%bsl%findsel$.tmp>nul
    IF errorlevel=1 SET ext=.BAT
    IF not "%1"=="" SET tar=%1
    IF "%tar%"=="" SET tar=%0%ext%
    IF exist %tar% GOTO found
    ECHO %tar% >%emp%%bsl%findsel$.tmp
    FIND ":" <%temp%%bsl%findsel$.tmp>nul
    IF not errorlevel 1 ECHO. bad filespec
    IF not errorlevel 1 FOR %%c in (SET GOTO:end) do %%c tar=
    %0%ext% ReCuRs %path%
     
    :ckpath
    SHIFT
    IF "%1"==""ECHO. not found
    IF "%1"=="" FOR %%c in (SET GOTO:end) do %%c tar=
    IF not exist %1.\%tar% GOTO ckpath
    SET tar=%1.\%tar%
     
    :found
    :: ASCII Assembler by Laura Fairhead
    ECHO XPYP[*'CC-\1P\QX,=P,APZ5O!PQ2O~5aaI~}Ksx>%temp%%bsl%echos.com
    %temp%%bsl%echos.com @SET tar=>%temp%%bsl%tar0$.bat
    DEL %temp%%bsl%findsel$.tmp
    DEL %temp%%bsl%echos.com
    SET dircm$=%dircmd%
    SET dircmd=
    DIR/A-D/B/L/S %tar%.>> %temp%%bsl%tar0$.bat
    SETdircmd=%dircm$%
    SET dircm$=
    FIND "SET tar="<%temp%%bsl%tar0$.bat>%temp%%bsl%tar1$.bat
    CALL %temp%%bsl%tar1$.bat
    DEL %temp%%bsl%tar?$.bat
    ECHO. Source is "%tar%"
    ::
    :: bonus: get drive letter
    ::
    ::CTTY nul % this line optional %
    CHOICE.COM />nul
    ::CTTY con % this line optional %
    IF not errorlevel 255 ECHO. -- choice.com missing, drive letter not obtained
    IF not errorlevel 255 GOTO end
    SET drv=
    ECHO %tar%>%temp%%bsl%setdrv.tmp
    CHOICE/Cabcdefghijklmnopqrstuvwxyz/N<%temp%%bsl%setdrv.tmp/S set drv=%%drv%%>%temp%%bsl%setdrv.bat
    DEL %temp%%bsl%setdrv.tmp
    FOR %%c in (CALL DEL) do %%c %temp%%bsl%setdrv.bat
    ECHO. Drive is %drv%
    :end
    SET bsl=
    ::

     
  • Save and restore a file's attributes.  {last modified Dec. 30, 2002}
    :: Begin s&r_atrb.bat
    @ECHO off
    IF "%1"=="" for %%c in (echo. goto:end) do %%cno filename given
    IF not exist %1 for %%c in (echo. goto:end) do %%c file not found
    DIR/AR-D/O/B/-P %1 >%temp%.\atrb_r.dat
    DIR/AA-D/O/B/-P %1 >%temp%.\atrb_a.dat
    DIR/AS-D/O/B/-P %1 >%temp%.\atrb_s.dat
    DIR/AH-D/O/B/-P %1 >%temp%.\atrb_h.dat
    FOR %%v in (R A S H) DO SET %%v=
    ATTRIB.EXE %1 -R -A -S -H % removes attributes%
    :: Or
    ATTRIB.EXE, %1   % broken in MS-DOS 7.x %
    :: Processing code goes here.
    FIND.EXE "." <%temp%.\atrb_r.dat |IFnot errorlevel=1 SET R=+R
    FIND.EXE "." <%temp%.\atrb_a.dat |IF not errorlevel=1 SET A=+A
    FIND.EXE "." <%temp%.\atrb_s.dat |IF not errorlevel=1 SET S=+S
    FIND.EXE "." <%temp%.\atrb_h.dat |IF not errorlevel=1 SET H=+H
    DEL %temp%.\atrb_?.dat
    FOR %%v in (%R% %A% %S% %H%) do IF not "%%v"=="" GOTO resatrb
    ECHO. %1 had no attributes set
    GOTO end
     
    :resatrb
    ATTRIB.EXE %1 %R% %A% %S% %H%
    FOR %%v in (R A S H) DO SET %%v=
     
    :end
    ::

     
  • Get the absolute path to a running batch file.
    Note, since a batch file with the S attribute won't run,
    there's no need to check for it.

     
    :: pathfor0.bat
    @ECHO off
    IF "%rec%"=="01" GOTO recurs
    SET src=
    ECHO %0? |FIND.EXE /I ".bat?" >nul
    IF not errorlevel=1 SET src=%0
    IF     errorlevel=1 SET src=%0.BAT
    IF not exist %src% GOTO ckpath
    ECHO%0 |FIND.EXE ":\" >nul
    IF not errorlevel=1 GOTO process
    IF exist a.bat ECHO. a.bat found in current directory; terminated
    IF exist a.bat GOTO end
    PATH>%temp%.\respath.bat
    PATH %temp%;%path%
    DIR/AR-D/O/B/-P %src% >%temp%.\atrb_r.dat
    DIR/AA-D/O/B/-P %src% >%temp%.\atrb_a.dat
    DIR/AH-D/O/B/-P %src% >%temp%.\atrb_h.dat
    ATTRIB.EXE %src% +a -h -r
    ATTRIB.EXE %src% >%temp%.\atrbsrc.bat
    ECHO SET src=%%1>%temp%.\a.bat
    FOR %%c in (call del) do%%c %temp%.\atrbsrc.bat
    DEL %temp%.\a.bat
    FOR %%c in (call del) do %%c %temp%.\respath.bat
    FOR %%v in (r a h) DO SET %%v=
    FIND.EXE "." <%temp%.\atrb_r.dat |IF not errorlevel=1 SET R=+R
    FIND.EXE "." <%temp%.\atrb_a.dat |IF not errorlevel=1 SET A=+A
    FIND.EXE "." <%temp%.\atrb_h.dat |IF not errorlevel=1 SET H=+H
    FOR %%v in (%R% %A% %H%) do IF not "%%v"=="" GOTO resatrb
    ATTRIB.EXE %src% -A
    GOTO process
     
    :resatrb
    ATTRIB.EXE %src% %R% %A% %H%
    GOTO process
     
    :ckpath
    ECHO %src% |FIND ":" >nul
    IF not errorlevel 1 ECHO. bad filespec
    IF not errorlevel 1 SET src=
    IF not errorlevel 1 GOTO end
    SET rec=01
    %src% %path%
     
    :recurs
    SET rec=
    IF exist %1.\%src% GOTO process
    SHIFT
    GOTO recurs
     
    :process
    FOR %%v in (r a h) DO SET %%v=
    IF exist %1%src% SET src=%1%src%
    IF exist %1\%src% SET src=%1\%src%
    ECHO. source is "%src%"
    :: Processing code goes here.
    :end
    ::

     
  • Parse a string.
    One way to do this is with choice.com. Here is parse.bat, a relatively easy to
    understand, full functioning demo which can easily be put into practical use.
     
    :: Begin parse.bat
    @ECHO off
    IF "%rec%"=="01" GOTO parse
    IF "%1"=="" FOR %%c in (ECHO GOTO:end) do %%c Nothing to parse
    SET rec=01
    ECHO ; | CHOICE.COM /C;%1; %0;>%temp%.\parse1.bat
    %temp%.\PARSE1.BAT
     
    :parse
    IF "%1"=="[" SHIFT
    IF "%1"=="]?" GOTO end
    ECHO %1
    SHIFT
    GOTO parse
    :end
    SET rec=
    IF exist %temp%.\parse1.bat DEL %temp%.\parse1.bat
    ::

    To see how this batch works, use: COMMAND/Y/E:24000/CPARSE string.
    This will allow you follow the process one step at a time. You may specify up
    to 32000 bytes on the /E: switch.
     
  • Save and restore the prompt.
     
    :: prmptbak.bat
    @ECHO off
    :: save prompt
    ECHO @ECHO on> %temp%.\resprmpt.bat
    SET |FIND "PROMPT=">> %temp%.\resprmpt.bat
    ECHO CLS>> %temp%.\resprmpt.bat
    ECHO @ECHO off>> %temp%.\resprmpt.bat
    ECHO ECHO.>> %temp%.\resprmpt.bat
    :: restore prompt
    CALL %temp%.\resprmpt.bat
    ::

     
  • Make a simple counter.
    There are many reasons to do a process a specific number of times. If only a
    small number is needed, this simple counter, called a 'bang' counter may be
    used. Ansi.sys required for this demo. Esc  represents the
    escape character, created in edit by 'Ctrl+P Esc'
    .  
    :: bangctr.bat
    @ECHO off
    IF not "%1"=="" GOTO process
    FOR %%v in (*.*) DO CALL %0 %%v
    SET ctr=
    GOTOend
     
    :process
    SET ctr=%ctr%*
    IF "%ctr%"=="**" ECHO Esc[1AEsc[18C%1
    IF "%ctr%"=="***" ECHO Esc[1AEsc[34C%1
    IF "%ctr%"=="****" ECHO Esc[1AEsc[50C%1
    IF "%ctr%"=="*****" ECHO Esc[1AEsc[66C%1
    IF "%ctr%"=="*****" SET ctr=
    :end
    ::

     
  • Run a program after a 10 second on-screen count down.
    This solution uses the bang counter fromthe previous answer. Use as many spaces
    as neccessary to center the count on your screen. Ansi.sys required; Esc
    represents the escape character, created in edit by 'Ctrl+P Esc'.

     
    :: cntdn10a.bat
    @ECHO off
    ECHO.
    IF not "%1"=="" ECHO. "%1" will run in 10 seconds
    ECHO. A aborts, R runs program, C continues count, other key pauses
    SET cnt=
     
    :loop
    SET cnt=%cnt%*
    CHOICE.COM /N /CCRA /TC,1 >nul
    IFerrorlevel=3 GOTO end
    IF errorlevel=2 GOTO run
    IF "%cnt%"=="*" ECHO.       % 26 spaces %        10
    IF "%cnt%"=="**" ECHO.                          Esc[1AEsc[K 9
    IF "%cnt%"=="***"ECHO.                          Esc[1AEsc[K 8
    IF "%cnt%"=="****" ECHO.                          Esc[1AEsc[K 7
    IF "%cnt%"=="*****"ECHO.                          Esc[1AEsc[K 6
    IF "%cnt%"=="******" ECHO.                          Esc[1AEsc[K 5
    IF "%cnt%"=="*******"ECHO.                          Esc[1AEsc[K 4
    IF "%cnt%"=="********" ECHO.                          Esc[1AEsc[K 3
    IF "%cnt%"=="*********"ECHO.                          Esc[1AEsc[K 2
    IF "%cnt%"=="**********" ECHO.                          Esc[1AEsc[K 1
    IF not "%cnt%"=="************" GOTO loop
     
    :run
    ECHOEsc[1AEsc[KEsc[1A
    CALL %1 %2 %3 %4 %5 %6 %7 %8 %9
    :end
    SET cnt=
    ::

     
  • Not run a program after an on-screen count down (without ansi.sys).
    Use as many spaces as neccessary to center the count on your screen. Use additional
    choice modules as desired, to increase seconds. Uses no environmental variables.

     
    :: cntdn5.bat
    CLS
    FOR %%v in (1 2 3 4 5 6) DOECHO.
    IF not "%1"=="" ECHO. "%1" will *not* run in 5 seconds
    ECHO. A aborts, R runs program, C continues count, other key pauses
    CHOICE.COM /CCRA/N /TC,1 >nul
    IF errorlevel=3 GOTO end
    IF errorlevel=2 GOTO run
    CLS
    FOR %%v in (1 2 3 4 5 6) DO ECHO.
    IF not "%1"=="" ECHO. "%1" will *not* run in 5 seconds
    ECHO. A aborts, R runs program, C continues count, other key pauses
    ECHO.  % 26 spaces%            5
    CHOICE.COM /CCRA/N /TC,1 >nul
    IF errorlevel=3 GOTO end
    IF errorlevel=2 GOTO run
    CLS
    FOR %%v in (1 2 3 4 5 6) DO ECHO.
    IF not "%1"=="" ECHO. "%1" will *not* run in 5 seconds
    ECHO. A aborts, R runs program, C continues count, other key pauses
    ECHO.  % 26 spaces%            4
    CHOICE.COM /CCRA/N /TC,1 >nul
    IF errorlevel=3 GOTO end
    IF errorlevel=2 GOTO run
    CLS
    FOR %%v in (1 2 3 4 5 6) DO ECHO.
    IF not "%1"=="" ECHO. "%1" will *not* run in 5 seconds
    ECHO. A aborts, R runs program, C continues count, other key pauses
    ECHO.  % 26 spaces%            3
    CHOICE.COM /CCRA/N /TC,1 >nul
    IF errorlevel=3 GOTO end
    IF errorlevel=2 GOTO run
    CLS
    FOR %%v in (1 2 34 5 6) DO ECHO.
    IF not "%1"=="" ECHO. "%1" will *not* run in 5 seconds
    ECHO. A aborts, R runs program, C continues count, other key pauses
    ECHO.  % 26 spaces%            2
    CHOICE.COM /CCRA/N /TC,1 >nul
    IF errorlevel=3 GOTO end
    IF not errorlevel=2 GOTO end
    CLS
    FOR %%v in (1 2 3 4 5 6) DO ECHO.
    IF not "%1"=="" ECHO. "%1" will *not* run in 5 seconds
    ECHO. A aborts, R runs program, Ccontinues count, other key pauses
    ECHO.  % 26 spaces%            1
    CHOICE.COM /CCRA/N /TC,1 >nul
    IF errorlevel=3 GOTO end
    CLS
    ECHO.
    IF not errorlevel=2 GOTO end
     
    :run
    CALL %1 %2 %3 %4 %5 %6 %7 %8 %9
    :end
    ::

     
  • Count up to a specified number.
    :: count_to.bat
    :: parm1 is thenumber to count to
    ECHO. >%temp%.\count.tmp
     
    :loop
    ECHO count>>%temp%.\count.tmp
    FIND.EXE/C "count" <%temp%.\count.tmp |FIND.EXE "%1"> nul
    IF errorlevel=1 GOTO loop
    ECHO. count=%1
    DEL %temp%.\count.tmp
    ::

     
  • Blank the screen.
    Blank.bat will blank the screen until a key is pressed. Ansi.sys required; Esc
    represents the escape character,created in edit by 'Ctrl+P Esc'.
     
    :: blank.bat
    @ECHO off
    ECHO Esc[0;30;40m
    CLS
    PAUSE > nul
    ECHO on
    @ECHO.
    CLS
    @ECHO off
    ECHO.
    ::

     
  • Format a drive without user input.
    If you have a other-than-English DOS, substitute the appropriate response.

     
    :: qformat.bat
    :: parm1 is drv: parms 2-4 possibleswitches
    @ECHO off
    ECHO y >%temp%.\qformat.tmp
    ECHO. >>%temp%.\qformat.tmp
    ECHO n >>%temp%.\qformat.tmp
    FORMAT.COM %1 %2 %3 %4 <%temp%.\qformat.tmp
    DEL %temp%.\qformat.tmp
    ::

     
  • Determine that a drive is not a network, CD-ROM or RAM drive.
    If not a logical drive, it could be a RAM, network or CD-ROM drive, or a removable media
    drive with no media. Very slow ona LS-120 drive with 120mb media present. Ansi.sys
    optional; Esc represents the escape character, created in edit by
    'Ctrl+P Esc'.
     
    :: logicdrv.bat
    :: parm1 is drive letter without colon
    @ECHO off
    CTTY nul
    %COMSPEC%/F/CCHKDSK.EXE %1: |FIND.EXE "Serial" >nul
    CTTY con
    :: %opt%ECHO Esc[2A
    IF not errorlevel=1 GOTO logical
    ECHO. Drive %1 is not a logical drive.
    ECHO. It could be a network, CD-ROM or RAMdrive.
    GOTO end
     
    :logical
    ECHO. Drive %1 is not a network, CD-ROM or RAM drive.
    :end
    ::

     
  • Check if a drive is valid.
    Ansi.sys optional; Esc represents the escape character, created in edit
    by 'Ctrl+P Esc'.

     
    :: isdrive.bat
    :: parm1 is drive letter without colon
    @ECHO off
    CTTY nul
    %COMSPEC%/F/CDIR/ADH/W/-P %1: |FIND.EXE ":\">nul
    CTTY con
    :: %opt%ECHO Esc[2A
    IF not errorlevel=1 ECHO. drive %1 valid
    IF     errorlevel=1 ECHO. drive %1 invalid
    ::

     
  • Check if a drive is valid and writeable.
    Ansi.sys optional; Esc represents the escape character, created in edit
    by 'Ctrl+P Esc'.
     
    :: Begin ckwrite.bat
    :: parm1 is drive letter withoutcolon
    @ECHO off
    CTTY nul
    %COMSPEC%/F/CDIR/ADH/W/-P %1: |FIND.EXE ":\" >nul
    IF errorlevel=1 GOTO no_drive
    ECHO writeable >%temp%.\temptmp1.tmp
    %COMSPEC%/F/CCOPY /Y %temp%.\temptmp1.tmp %1:\temptmp1.tmp
    DEL %temp%.\temptmp1.tmp
    CTTY con
    :: %opt%ECHO Esc[2A
    IF exist %1:\temptmp1.tmp GOTO write_ok
    ECHO. drive %1 not writeable
    GOTO end
     
    :write_ok
    DEL %1:\temptmp1.tmp
    ECHO. drive %1 writeable
    GOTO end
     :no_drive
    CTTY con
    :: %opt%ECHO Esc[2A
    ECHO. drive %1 invalid
    :end
    ::

     
  • Check if there is a disk in the a: drive; with user prompt and time out.
    Ansi.sys optional; Esc represents the escape character, created in edit by 'Ctrl+P Esc'.

     
    :: Begin ckdrva.bat
    @ECHO off
    :start
    CTTY nul
    %COMSPEC%/F/CDIR/ADH/W/-P a: |FIND.EXE ":\">nul
    CTTY con
    IF errorlevel=1 GOTO no_disk
    :: %opt%ECHO Esc[2A
    ECHO    disk in drive A:
    GOTO end
     
    :no_disk
    CLS
    FOR %%v in (1 2 3 4 5) DO ECHO.
    ECHO     Insert disk in A: and press C to continue
    ECHO      % 26 spaces %       or press Q to quit
    CHOICE.COM /CCQ/TQ,10 /N >nul
    IF not errorlevel=2 GOTO start
    :end
    ::

     
  • Get drive type: hard, floppy, network/CD-ROM, RAM, or unknown.
    Ansi.sys optional; Esc represents the escape character, created in edit
    by 'Ctrl+P Esc'.

     
    :: Begin drvtype.bat
    :: The symbol after ECHO is made in edit by: [Ctrl+P][C]
    @ECHO off
    :: parm1 is drive letter without colon
    CTTY nul
    :: check if valid drive specification
    ECHO [Ctrl+P][C]|FORMAT.COM %1: /F:160 >%temp%.\flag1.flg
    IF exist %temp%.\flag2.flg DEL %temp%.\flag2.flg
    COPY %temp%.\flag1.flg %temp%.\flag2.flg
    DEL %temp%.\flag1.flg
    IF not exist %temp%.\flag2.flg SET type=invalid
    IF not exist %temp%.\flag2.flg GOTO end
    DEL %temp%.\flag2.flg
     
    :: check if hard drive
    ECHO [Ctrl+P][C]|FORMAT.COM %1: /F:160 |FIND.EXE "NON-REMOVABLE DISK"
    IF not errorlevel=1 SETtype=hd
    IF not errorlevel=1 GOTO end
     
    :: check if diskette drive
    ECHO [Ctrl+P][C]|FORMAT.COM %1: /F:160 |FIND.EXE "Insert new diskette"
    IF not errorlevel=1 SET type=flop
    IF not errorlevel=1 GOTO end
     
    :: check if network or CD-ROM drive
    ECHO [Ctrl+P][C]|FORMAT.COM %1: /F:160 |FIND.EXE "a network drive"
    IF not errorlevel=1 SET type=net/cd
    IF not errorlevel=1 GOTO end
     
    :: check if other type drive
    ECHO [Ctrl+P][C]|FORMAT.COM %1: /F:160|FIND.EXE "Format not supported"
    IF errorlevel=1 SET type=unknown
    IF errorlevel=1 GOTO end
     
    :: check if RAM drive
    %COMSPEC%/F/E:10240/CVOL %1: |FIND.EXE "MS-RAMDRIVE" >nul
    IF not errorlevel=1 SET type=RAM
    IF     errorlevel=1 SET type=unknown
    :end
    CTTY con
    :: %opt%ECHO Esc[2A
    ECHO. drive %1: is %type% type
    ::

     
  • List all local hard drivesincluding megabytes free and megabytes total.
    DRVSPACE /LI |FIND.EXE "Local hard drive"
     
  • Check if the %temp% drive/directory is full.
    All methods using a pipe will fail if the %temp% directory is full or cannot
    be written to. This method can safely be used because it does not use pipes.

     
    :: oktemp.bat
    @ECHO off
    CTTY nul
    ECHO.>%temp%.\flag1.tmp
    COPY/B/Y %temp%.\flag1.tmp %temp%.\flag2.tmp
    ECHO. >con
    IF not exist %temp%.\flag1.tmp ECHO %temp% is full >con
    IF exist %temp%.\flag1.tmp ECHO %temp% is ok >con
    IF exist %temp%.\flag?.tmp DEL %temp%.\flag?.tmp
    CTTY con
    ::

     
  • Get the length of a simple (single word) string.
    :: wdlngth.bat
    IF "%1"=="" ECHO nothing to do
    IF "%1"=="" GOTO end
    SETstr=
    ECHO ; |CHOICE.COM /C%1; >nul
    IF errorlevel 9 IF not errorlevel 10 SET str=8
    IF errorlevel 8 IF not errorlevel 9 SET str=7
    IF errorlevel 7 IF not errorlevel 8 SET str=6
    IF errorlevel 6 IF not errorlevel 7 SET str=5
    IF errorlevel 5 IF not errorlevel 6 SET str=4
    IF errorlevel 4 IF not errorlevel 5 SET str=3
    IF errorlevel 3 IF not errorlevel 4 SET str=2
    IF errorlevel 2 IF not errorlevel 3 SET str=1
    IF "%str%"=="" ECHO %1 has more than 8 characters
    IF not"%str%"=="" ECHO %1 is %str% in length
    SET str=
    :end
    ::

     
  • Check if the root directory is current.
    Ansi.sys optional; Esc represents the escape character, created in edit by 'Ctrl+P Esc'.

     
    :: isroot.bat
    @ECHO off
    CTTY nul
    DIR/A/W/-P |FIND.EXE "file(s)"
    IF errorlevel=1 ECHO no files on drive >con
    IF errorlevel=1 GOTO end
    DIR/A/W/-P|FIND.EXE ".." >nul
    IF not errorlevel=1 ECHO root directory is not current >con
    IF     errorlevel=1 ECHO root directory is current >con
    :end
    CTTY con
    :: %opt%ECHO Esc[2A
    ::

     
  • Make a batch file keep a limited count of how many times it's run.
    Thanks to Todd Vargo for optimization suggestions.

     
    :: countme.bat
    @ECHO off
    ::ECHO %0 |FIND.EXE ":\" >nul
    ::IF errorlevel=1 ECHO Enter complete path to %0
    ::IF errorlevel=1 GOTO end
    :: This example counts up to five and will echo a message to the
    :: screen every fifth time it is run. If *your code* changes the
    :: directory, un-comment the first three lines @ECHO off.
    SET src=
    IF exist %0.bat SET src=%0.bat
    IF "%src%"=="" IF exist %0 SET src=%0
    IF "%src%"=="" ECHO "%0" must exist as entered
    IF "%src%"=="" GOTO end
    :: * yourcode for every run goes here *
    SET key=::#
    ECHO %key%# Do not remove or change this line!>> %src%
     
    :: Change the 5 below to set the counter.
    FIND/C "%key%#" <%src% |FIND "5">nul %Set counter here%
    IF errorlevel=1 GOTO end
    :clear
    FIND/V "%key%#" <%src% |FIND/V ""> %src%
    :: The number below is the same as in the "Set counter" line above.
    ECHO %src% has run 5 times
    :: *your code for run 5 goes here*
     
    :end
    SET key=
    SET src=
    ::

    52.  Get the basename of a file.
    This is language dependent; invalid.bat must be renamed to whatever is appropriate.

     
    @ECHO off
    :: basenam2.bat
    IF "%1"=="" ECHO. filename required
    IF "%1"=="" GOTO end
    IF exist invalid.bat ECHO. invalid.bat in current
    IF exist invalid.bat GOTO end
    MODE %1 >%temp%.\bnam.bat
    ECHO SET bnam=%%3>%temp%.\invalid.bat
    PATH >%temp%.\respath.bat
    PATH %temp%;%path%
    FOR %%c in (call del) do %%c %temp%.\bnam.bat
    FOR %%c in (call del) do %%c %temp%.\respath.bat
    DEL %temp%.\invalid.bat
    :end

     

    53.  Get the CD-ROM letter.
    cd-roms.com required. Ansi.sys optional; Esc represents the escape character, created
    in edit by 'Ctrl+P Esc'.

     
    @ECHO off
    :: getcd.bat
    :: tested on a single CD drive system
    CD-ROMS.COM SET=cd=>%temp%.\temp.bat
    FOR %%c in (call del) do %%c %temp%.\temp.bat
    :: %opt%ECHO Esc[2A
    IF "%cd%"=="" ECHO CD-ROM not present
    IF "%cd%"=="" GOTO end
    FOR %%v in (%cd%) do set cd=%%v
    ECHO. CD-ROM letter is '%cd%'
    :end

     

    54.  Get bytes required by current environment.
    @ECHO off
    :: envsize.bat
    IF not "%1"=="" GOTO ok
    SET >%temp%.\envsize.dat
    DIR/-P %temp%.\envsize.dat |FIND "ENVSIZE" >%temp%.\envsiz1.bat
    %temp%.\envsiz1.bat

    :ok
    DEL %temp%.\envsiz?.?at
    ECHO.
    ECHO. Environment requires %2 bytes.

     

    55. Get serial number of drive.
    If you use WfW 3.x with 32BFA enabled, substitute CHKDSK for VOL
    (VOL won't work). Ansi.sys optional; Esc represents the escape
    character, created in edit by 'Ctrl+P Esc'.

     
    @ECHO off
    :: getserno.bat
    IF "%1"=="" ECHO. drive letter required
    IF "%1"=="" GOTO end
    CTTY nul
    %COMSPEC%/F/CDIR/ADH/W/-P %1: |FIND.EXE ":\" >nul
    CTTY con
    :: %opt%ECHO Esc[2A
    IF errorlevel=1 ECHO. drive %1: invalid
    IF errorlevel=1 GOTO end
    IF exist volume.bat ECHO. volume.bat in current
    IF exist volume.bat GOTO end
    SET sn=
    VOL %1: |FIND "Volume Serial Number" >%temp%.\ser_nbr.bat
    IF errorlevel=1 DEL %temp%.\ser_nbr.bat
    IF errorlevel=1 GOTO end
    ECHO. SET sn=%%4>%temp%.\volume.bat
    PATH >%temp%.\respath.bat
    PATH %temp%;%path%
    FOR %%C in (call del) do %%C %temp%.\SER_NBR.BAT
    FOR %%C in (call del) do %%C %temp%.\respath.bat
    DEL %temp%.\volume.bat
    IF not "%sn%"=="" ECHO. serial number is %sn%
    :end

     

    56.  Get volume label of drive.
     
    Getlabel.bat (inspired by an idea from John Savage) will retrieve the
    volume label even if it contains spaces. Getlabel.bat leaves an extra
    _set_ variable %dvl% which indicates the drive just checked. This can be
    useful when checking several drives and knowing where you left off even
    if there was no label or the drive was invalid. Also, if a drive has no
    Volume label, the %vl% variable is cleared, but not the corresponding
    'drive letter' variable, if previously set in the same command environment.
    Ansi.sys optional; Esc represents the escape character, created in edit
    by 'Ctrl+P Esc'.

     
    @ECHO off
    :: example.bat
    :: displays volume label of all drives
    FOR %%d in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do CALL getlabel %%d

     
    @ECHO off
    :: getlabel.bat
    IF "%1"=="ReCuRs" GOTO recurs
    IF "%1"=="" ECHO. drive letter required (no colon)
    IF "%1"=="" GOTO end
    CTTY nul
    COMMAND/F/CDIR/ADH/W %1: |FIND.EXE ":\" >nul
    CTTY con
    :: comment out following line if ansi.sys not used
    ECHO Esc[2A
    SET dvl=%1
    IF errorlevel=1 ECHO. drive %1: invalid
    IF errorlevel=1 FOR %%c in (SET GOTO:end) do %%c vl=
    IF errorlevel=1 GOTO end
    VOL %1: |FIND.EXE "has no label"> nul
    IF not errorlevel=1 ECHO. drive %1: has no volume label
    IF not errorlevel=1 FOR %%c in (SET GOTO:end) do %%c vl=
    IF not errorlevel=1 GOTO end
    VOL %1: |FIND.EXE "Volume in drive"> %temp%.\label$.bat
    IF errorlevel=1 FOR %%v in (vl dvl) do SET %%v=
    IF errorlevel=1 ECHO. error: "Volume in drive" not found
    IF errorlevel=1 FOR %%c in (DEL GOTO:end) do %%c %temp%.\label$.bat
    ECHO FOR %%%%n IN (1 2 3 4) do SHIFT> %temp%.\volume.bat
    ECHO SET vl=%%1>>%temp%.\volume.bat
    ECHO IF not "%%2"=="" SET vl=%%1 %%2 %%3 %%4 %%5 %%6>> %temp%.\volume.bat
    ECHO @PROMPT $N:$_CD $P> %temp%.\setback1.bat
    %COMSPEC%/E:2048/C%temp%.\setback1.bat> %temp%.\goback.bat
    DEL %temp%.\setback1.bat
    FOR %%c in (%temp%.\ CD) do %%c %temp%.
    CALL %temp%.\label$.bat
    FOR %%c in (CALL DEL) do %%c %temp%.\goback.bat
    DEL %temp%.\volume.bat
    %0 ReCuRs %vl%
     
    :recurs
    SHIFT
    IF "%2"=="" GOTO last
    SET w1=%1
    SET w2=%2
    SET s=
     
    :setword
    SET s= %s%
    FIND.EXE "%w1%%s%%w2%" <%temp%.\label$.bat> nul
    IF errorlevel 1 GOTO setword
    SET vl=%w1%%s%%w2%
    SHIFT
    SET s=
    IF "%2"=="" GOTO last
    SET w1=%vl%
    set w2=%2
    GOTO setword
     
    :last
    ECHO SET %%dvl%%=%vl%> %temp%.\label$.bat
    FOR %%c in (CALL DEL) do %%c %temp%.\label$.bat
    ECHO.   vl=%vl%
    ECHO.    %dvl%=%vl%
    ::SET dvl=
    SET w1=
    SET w2=
    SET s=
    :end
    ::

     

    57.  Place command output or line of text into a variable with a helping file.  {last modified Sep. 14, 2004}
    Requires a permanent helping file, setvar.inp. I recommend creating an
    input directory c:\input and keeping all batch input files there. Instructions
    for creating setvar.inp below. See also example.bat

     
    @ECHO off
    :: setvar.bat
    IF not exist c:\input\setvar.inp ECHO. c:\input\setvar.inp not present; look in setvar.bat
    IF not exist c:\input\setvar.inp GOTO end
    TYPE c:\input\setvar.inp> %temp%.\setvar0.bat
    :end
    ::: EOF (End Of File) Character: In the DOS editor (edit.com) press
    ::: [Ctrl] and while holding it down; press and release [P] and
    ::: press [Z]. You will see a little right pointing arrow. This
    ::: character is 26 in decimal or 1A in hexadecimal.
    ::: Setvar.inp contents below this line (without the colon).
    :: @SET %1=[EOF character]

     
    Running setvar.bat will create %temp%.\setvar0.bat to which a single line may
    be appended, forming a continuation of the line with the SET command.
    Run %temp%.\setvar0.bat with the desired variable name as the argument.

    The following example shows setvar.bat used to catch the output of the
    CD command and place it into the %curdir% variable.
     
    :: example.bat
    @ECHO off
    CALL setvar.bat
    CD>>%temp%.\setvar0.bat
    TYPE %temp%.\setvar0.bat |FIND "SET %%1=">%temp%.\setvar0.bat
    CALL %temp%.\setvar0.bat curdir
    DEL %temp%.\setvar0.bat

     

    58. Get filename of an existing file into a variable.
     
    @ECHO off
    :: filname.bat
    IF "%1"=="" ECHO. must enter existing filename
    IF "%1"=="" GOTO end
    IF not exist %1 ECHO. file "%1" does not exist
    IF not exist %1 GOTO end
    IF exist enter.bat ECHO. enter.bat in current directory
    IF exist enter.bat GOTO end
    PATH>%temp%.\respath.bat
    PATH %temp%;%path%
    DIR/A-D/B/L/-P %1. > %temp%.\filname1.dat
    ECHO. >> %temp%.\filname1.dat
    DATE <%temp%.\filname1.dat | find "):" > %temp%.\filname1.bat
    ECHO set fnam=%%4> %temp%.\enter.bat
    FOR %%C in (call del) do %%C %temp%.\filname1.bat
    FOR %%F IN (%temp%.\filname1.dat %temp%.\enter.bat) do DEL %%F
    FOR %%C in (call del) do %%C %temp%.\respath.bat
    :end

     

    59.  Get directory of an existing file into a variable.
     
    @ECHO off
    :: fildir.bat
    IF "%1"=="" ECHO. must enter existing filename
    IF "%1"=="" GOTO end
    IF not exist %1 ECHO. file "%1" does not exist
    IF not exist %1 GOTO end
    IF exist enter.bat ECHO. enter.bat in current directory
    IF exist enter.bat GOTO end
    PATH>%temp%.\respath.bat
    PATH %temp%;%path%
    DIR/A/L/-P %1. |FIND "Directory of"> %temp%.\fildir1.dat
    ECHO. >> %temp%.\fildir1.dat
    DATE <<%temp%.\fildir1.dat | find "):" > %temp%.\fildir1.bat
    ECHO set fdir=%%6> %temp%.\enter.bat
    FOR %%C in (call del) do %%C %temp%.\fildir1.bat
    FOR %%F IN (%temp%.\fildir1.dat %temp%.\enter.bat) do DEL %%F
    FOR %%C in (call del) do %%C %temp%.\respath.bat
    :end

     

    60.  Get full path of an existing file into a variable.
     
    @ECHO off
    :: filpath.bat
    IF "%1"=="" ECHO. must enter existing filename
    IF "%1"=="" GOTO end
    IF not exist %1 ECHO. file "%1" does not exist
    IF not exist %1 GOTO end
    IF exist enter.bat ECHO. enter.bat in current directory
    IF exist enter.bat GOTO end
    PATH>%temp%.\respath.bat
    PATH %temp%;%path%
    DIR/A-D/B/L/S/-P %1. > %temp%.\filpath1.dat
    ECHO. >> %temp%.\filpath1.dat
    DATE <%temp%.\filpath1.dat | find "):" > %temp%.\filpath1.bat
    ECHO set fpath=%%4> %temp%.\enter.bat
    FOR %%C in (call del) do %%C %temp%.\filpath1.bat
    FOR %%F IN (%temp%.\filpath1.dat %temp%.\enter.bat) do DEL %%F
    FOR %%C in (call del) do %%C %temp%.\respath.bat
    :end

     

    61.  Get drive letter of a full path into a variable.
     
    @ECHO off
    :: fildrv.bat
    IF "%1"=="" ECHO. enter path, including drive letter
    IF "%1"=="" GOTO end
    ECHO %1 |FIND ":" >nul
    IF errorlevel=1 ECHO. enter path, including drive letter
    IF errorlevel=1 GOTO end
    IF exist invalid.bat ECHO. invalid.bat in current directory
    IF exist invalid.bat GOTO end
    PATH>%temp%.\respath.bat
    PATH %temp%;%path%
    MODE %1 >%temp%.\fildrv1.bat
    ECHO SET fdrv=%%3>%temp%.\invalid.bat
    FOR %%c in (call del) do %%c %temp%.\fildrv1.bat
    DEL %temp%.\invalid.bat
    FOR %%C in (call del) do %%C %temp%.\respath.bat
    :end

     

    62.  Get file extension into a variable.
     
    @ECHO off
    :: filext.bat
    IF "%1"=="ReCuRs[" FOR %%C IN (SHIFT GOTO:parse) do %%C
    IF "%1"=="" FOR %%C IN (ECHO GOTO:end) do %%C no filename entered
    ECHO ; | CHOICE.COM/S/C;%1; %0; ReCuRs>%temp%.\parse1.bat
    SET fext=++
    %temp%.\PARSE1.BAT
     
    :parse
    IF "%1"=="." FOR %%C IN (SHIFT SET) do %%C fext=
    IF not "%fext%"=="++" SET fext=%fext%%1
    SHIFT
    IF "%1"=="]?" SHIFT
    IF not "%1"=="" GOTO parse
    DEL %temp%.\parse1.bat
    IF "%fext%"=="++" SET fext=
    IF "%fext%"=="]?" SET fext=
    IF "%fext%"=="" ECHO. no extension
     
    :end

     

    63.  Get file date into a variable.
     
    @ECHO off
    :: fildate.bat
    IF "%1"=="ReCuRs" GOTO recurs
    IF "%1"=="" ECHO. must enter existing filename
    IF "%1"=="" GOTO end
    IF not exist %1 ECHO. file "%1" does not exist
    IF not exist %1 GOTO end
    IF exist enter.bat ECHO. enter.bat in current directory
    IF exist enter.bat GOTO end
    PATH>%temp%.\respath.bat
    PATH %temp%;%path%
    DIR %1. > %temp%.\filtime1.dat
    FIND/V " bytes" <%temp%.\fildate1.dat |FIND/V " Volume " |FIND/V "Directory of" |SORT/R> %temp%.\fildate1.dat
    ECHO. >> %temp%.\fildate1.dat
    DATE <%temp%.\fildate1.dat | find "):" > %temp%.\fildate1.bat
    ECHO FOR %%%%n in (1 2 3 4) do SHIFT> %temp%.\enter.bat
    ECHO %0 ReCuRs %%1 %%2 %%3 %%4 %%5 %%5 %%6 %%7 %%8 %%9>>%temp%.\enter.bat
    FOR %%C in (call del) do %%C %temp%.\fildate1.bat
    GOTO end
     
    :recurs
    FOR %%F IN (%temp%.\fildate1.dat %temp%.\enter.bat) do DEL %%F
    FOR %%C in (call del) do %%C %temp%.\respath.bat
    SHIFT
    ECHO %1 %2 %3 %4 %5 %6 |FIND.EXE "-" >nul
    IF not errorlevel=1 SET sep=-
    IF not errorlevel=1 GOTO again
    ECHO %1 %2 %3 %4 %5 %6 |FIND.EXE "/" >nul
    IF not errorlevel=1 SET sep=/
    IF errorlevel=1 FOR %%c in (ECHO GOTO:end) do %%c unknown date separator
    SET fdat=
     
    :again
    ECHO %1 |FIND "%sep%" >nul
    IF not errorlevel=1 SET fdat=%1
    IF errorlevel=1 SHIFT
    IF errorlevel=1 GOTO again
    SET sep=
    :end

     

    64.  Get the size of a file into a variable (1).
     
    @ECHO off
    :: filsize.bat
    IF "%rec%"=="01" GOTO recurs
    IF "%1"=="" ECHO. filespec required
    IF "%1"=="" GOTO end
    IF not exist %1 ECHO. %1 is invalid
    IF not exist %1 GOTO end
    IF exist 1.bat ECHO. 1.bat in current; terminated
    IF exist 1.bat GOTO end
    DIR/A-D %1. |FIND.EXE "1 file(s)" >%temp%.\getsize.bat
    ECHO %0 %%2 %%3 %%4 %%5 %%6 %%7 %%8 %%9>%temp%.\1.bat
    SET tar=%1
    PATH>%temp%.\respath.bat
    PATH %temp%;%path%
    SET fsiz=
    SET rec=01
    %temp%.\getsize.bat
     
    :recurs
    SET rec=
    FOR %%v in (%temp%.\getsize.bat %temp%.\1.bat) DO DEL %%v
    FOR %%c in (call del) do %%c %temp%.\respath.bat
     
    :again SET fsiz=%fsiz%%1
    SHIFT
    IF not "%1"=="bytes" GOTO again
    IF not "%fsiz%"=="" ECHO. %tar% is %fsiz% bytes
    SET tar=
    :end

     

    65.  Get the size of a file into a variable (2).
    If your delimiter is a comma, the "GOTO parse" can be replaced with
    "GOTO end" and the :parse, :recurs2 and recurs2a modules may be
    deleted and the following command line removed: IF "%1"=="ReCuRs[" GOTO recurs2
    If your delimiter is not a comma, you may remove the :loop module only.
    To handle both comma and non-comma delimiters, leave as is. Ansi.sys optional;
    Esc represents the escape character, created in edit by 'Ctrl+P Esc'.

     
    @ECHO off
    :: filsize2.bat
    :: author: Laurence Soucy, http://purl.oclc.org/net/Batfiles/
    IF "%1"=="ReCuRs1" GOTO recurs1
    IF "%1"=="ReCuRs[" GOTO recurs2
    IF "%1"=="" ECHO. filespec required
    IF "%1"=="" GOTO end
    IF not exist %1 ECHO. %1 is invalid
    IF not exist %1 GOTO end
    SET fsiz=
    DIR/A-D %1. |FIND.EXE "1 file(s)" >%temp%.\filsize0.tmp
    ECHO @PROMPT SET fsiz=%%%%fsiz%%%%>%temp%.\filsize0.bat
    FC/N/LB1 %temp%.\filsize0.tmp nul |find "1:">>%temp%.\filsize0.bat
    CTTY nul
    %COMSPEC%/C%temp%.\filsize0.bat> %temp%.\filsize1.bat
    CTTY con
    :: %opt%ECHO Esc[2A
    CALL %temp%.\filsize1.bat
    DEL %temp%.\filsize?.*
    %0 ReCuRs1 %fsiz%
     
    :recurs1
    SET src=%0
    FOR %%n in (1 2 3 4) do SHIFT
    SET fsiz=%1
     
    :loop
    SHIFT
    IF "%1"=="bytes" GOTO parse
    SET fsiz=%fsiz%%1
    GOTO loop
     
    :parse
    ECHO ; | CHOICE/C;%fsiz%; %src%;ReCuRs>%temp%.\parse.bat
    %temp%.\parse.bat
     
    :recurs2
    SET fsiz=
    SET src=
     
    :recurs2a
    SHIFT
    IF "%1"=="" GOTO end
    SET nbr=
    FOR %%n in (0 1 2 3 4 5 6 7 8 9) do IF %1==%%n SET fsiz=%fsiz%%1
    GOTO recurs2a
    :end

     

    66.  Get file time into a variable.
     
    @ECHO off
    :: filtime.bat
    IF "%1"=="ReCuRs" GOTO recurs
    IF "%1"=="" ECHO. must enter existing filename
    IF "%1"=="" GOTO end
    IF not exist %1 ECHO. file "%1" does not exist
    IF not exist %1 GOTO end
    IF exist enter.bat ECHO. enter.bat in current directory
    IF exist enter.bat GOTO end
    PATH>%temp%.\respath.bat
    PATH %temp%;%path%
    DIR %1. > %temp%.\filtime1.dat
    FIND/V " bytes" <%temp%.\filtime1.dat |FIND/V " Volume " |FIND/V "Directory of" |SORT/R >%temp%.\filtime1.dat
    ECHO. >> %temp%.\filtime1.dat
    DATE <%temp%.\filtime1.dat | find "):" > %temp%.\filtime1.bat
    ECHO FOR %%%%n in (1 2 3 4) do SHIFT> %temp%.\enter.bat
    ECHO %0 ReCuRs %%1 %%2 %%3 %%4 %%5 %%5 %%6 %%7 %%8 %%9>> %temp%.\enter.bat
    FOR %%C in (call del) do %%C %temp%.\filtime1.bat
    GOTO end
     
    :recurs
    FOR %%F IN (%temp%.\filtime1.dat %temp%.\enter.bat) do DEL %%F
    FOR %%C in (call del) do %%C %temp%.\respath.bat
    SHIFT
    SET ftim=
     
    :again
    IF "%2"=="" SET ftim=%1
    SHIFT
    IF not "%1"=="" GOTO again
     
    :end

     

    67.  Edit the path at the DOS prompt.  {last modified Dec. 30, 2002}
    Ansi.sys required; Esc represents the escape character, created in edit
    by 'Ctrl+P Esc'. This is a slightly modified version of the original batch by
    Edwin.Schwartz@dlr.de.

     
    @ECHO off
    :: epath.bat
    :: ansi.sys required
    IF "%1"=="" echo. Use [Shift]+[F9] to edit path.
    IF not "%1"=="" PATH %1
    :loop
    SHIFT
    IF not "%1"=="" PATH %path%;%1
    IF not "%1"=="" GOTO loop
    ECHO Esc[0;92;"epath.bat %path%"p

     

    68.  Isolate the first (non-system/hidden) file in a directory.
     
    @ECHO off
    :: 1stfil.bat
    :: defaults to current directory if none specified
    IF "%1"=="ReCuRs" GOTO recurs
    IF not exist %1.\*.* ECHO. no files in directory
    IF not exist %1.\*.* GOTO end
    FOR %%F in (%1.\*.*) do %0 ReCuRs %%F
     
    :recurs
    ECHO. 1st non-hidden/system file in directory is "%2"
    :end

     

    69.  Display boot drive letter or place into a variable.
     
    @ECHO off
    :: bootdrv.bat
    IF not "%1"=="?" IF not "%1"=="/?" GOTO start
    ECHO.
    ECHO. BOOTDRV with no arguments will display drive letter.
    ECHO. BOOTDRV with any argument will set bootdrv variable
    ECHO. to drive letter specified by %%COMSPEC%% variable.
    ECHO. author: Laurence Soucy, http://purl.oclc.org/net/Batfiles/
    GOTO end
     
    :start
    IF "%COMSPEC%"=="" ECHO. comspec variable not set
    IF "%COMSPEC%"=="" GOTO end
    IF "%1"=="" GOTO display only
    ECHO %COMSPEC% |CHOICE.COM/C%COMSPEC%/n set bootdrv=> %temp%.\bootdrv$.bat
    FOR %%c in (call del) do %%c %temp%.\bootdrv$.bat
    GOTO end
     
    :display only
    ECHO %COMSPEC% |CHOICE.COM/C%COMSPEC%/n " "
     
    :end

     

    70.  Place first character of string into a variable.
     
    @ECHO off
    :: 1stchar.bat
    IF not "%1"=="?" IF not "%1"=="/?" GOTO start
     
    :help
    ECHO.
    ECHO. Syntax: 1STCHAR [/s] VarName String
    ECHO. Places 1st character of string into variable.
    ECHO. The /s or /S switch makes batch case-sensitive;
    ECHO. otherwise 1st character is set as uppercase.
    ECHO. author: Laurence Soucy, http://purl.oclc.org/net/Batfiles/
    GOTO end
     
    :start
    SET /s=
    IF "%1"=="/s" SET /s=/s
    IF "%1"=="/S" SET /s=/s
    IF "%/s%"=="/s" SHIFT
    IF "%2"=="" GOTO help
    ECHO %2 |CHOICE.COM/C%2/n%/s% SET %1=>%temp%.\1stchar$.bat
    FOR %%c in (call del) do %%c %temp%.\1stchar$.bat
    :end
    SET /s=

     

    71.  Display current drive letter or place into a variable.
     
    @ECHO off
    :: curdrvl.bat
    IF not "%1"=="?" IF not "%1"=="/?" GOTO start
     
    :help
    ECHO.
    ECHO. Syntax: CURDRVL [VarName]
    ECHO. Places drive letter of current drive into variable
    ECHO. given as argument. If no argument, displays current
    ECHO. drive letter only.
    ECHO. author: Laurence Soucy, http://purl.oclc.org/net/Batfiles/
    GOTO end
     
    :start
    IF "%1"=="" GOTO display only
    CD|CHOICE.COM/N/Ccdefghijklmnopqrstuvwxyz SET %1=>%temp%.\curdrvl$.bat
    FOR %%c in (call del) do %%c %temp%.\curdrvl$.bat
    GOTO end
     
    :display only
    CD |CHOICE.COM/Ccdefghijklmnopqrstuvwxyz " "
    :end

     

    72.  Remove 1st character of string and place remainder into variable.
     
    @ECHO off
    :: remchar1.bat
    IF not "%1"=="?" IF not "%1"=="/?" GOTO start
     
    :help
    ECHO.
    ECHO. Syntax: REMCHAR1 SingleWordString
    ECHO. Removes 1st character; places remainder into %%rest%% variable
    ECHO. Also places 1st character into %%chr1%% variable.
    ECHO. author: Laurence Soucy, http://purl.oclc.org/net/Batfiles/
    GOTO end
     
    :start
    IF "%1"=="" GOTO help
    ECHO %1 |choice.com/c%1/n/s set chr1=>%temp%.\1stchar$.bat
    FOR %%c in (call del) do %%c %temp%.\1stchar$.bat
    ECHO @PROMPT $N:$_CD $P> %temp%.\setback1.bat
    %COMSPEC% /E:2048 /C%temp%.\setback1.bat> %temp%.\goback.bat
    DEL %temp%.\setback1.bat
    %temp%.\
    CD %temp%.\.
    :: Thanks to John Savage for the following line of code:
    FOR %%%chr1% in (%%) do ECHO set rest=%%%1%>remchr1.bat
    FOR %%c in (call del) do %%c remchr1.bat
    ECHO. character 1 is "%chr1%"
    ECHO. remainder of word is "%rest%"
    :: %opt%SET chr1=
    :: %opt%SET rest=
    FOR %%c in (call del) do %%c %temp%.\goback.bat
     
    :end

     

    73.  Make a batch file self-delete.
    Note: EOF represents the End Of File character, created in edit by 'Ctrl+P+Z'.

     
    @ECHO off
    :: delme.bat
    :: deletes self after running
    :: author: Laurence Soucy, http://purl.oclc.org/net/Batfiles/
    TYPE nul> %temp%.\batexit.bat
    ::ECHO %0 |FIND.EXE ":\" >nul
    ::IF errorlevel=1 ECHO. Enter full path to %0 including extension
    ::IF errorlevel=1 %temp%.\batexit.bat
    ::: If *your code* changes the directory, un-comment the
    ::: previous three lines.
    IF exist %0.bat ECHO. extension must be used
    IF exist %0.bat %temp%.\batexit.bat
    IF not exist %0 ECHO. %0 must exist as entered or be in current
    IF not exist %0 %temp%.\batexit.bat
    IF exist %temp%.\batexit.bat DEL %temp%.\batexit.bat
    :: *your code* goes here
    ::: EOF represents the DOS end of file character, ASCII 26,
    ::: created in edit by [Ctrl]+[p]+[z].
    ::DEL %0EOF   % uncomment this line to self-delete %

     

    74.  Make a batch file self-delete with doskey.
     
    @ECHO off
    :: delmedk.bat
    :: deletes self after running with doskey
    :: author: Laurence Soucy, http://purl.oclc.org/net/Batfiles/
    ::ECHO %0 |FIND.EXE ":\" >nul
    ::IF errorlevel=1 ECHO. Enter complete path to %0
    ::IF errorlevel=1 GOTO end
    ::: If *your code* changes the directory, un-comment the
    ::: first three lines after @ECHO off.
    SET cnt=
     
    :check for doskey
    SET cnt=%cnt%*
    MEM/C |FIND.EXE "DOSKEY" >nul
    IF not errorlevel=1 GOTO start
    IF "%cnt%"=="**" ECHO. unable to load doskey.com
    IF "%cnt%"=="**" GOTO end
    LH DOSKEY.COM
    GOTO check
     
    :start
    SET cnt=
    SET src=
    IF exist %0.bat SET src=%0.bat
    IF "%src%"=="" IF exist %0 SET src=%0
    IF "%src%"=="" ECHO. "%0" must exist as entered
    IF "%src%"=="" GOTO end
    :: * your code goes here *
     
    :delete
    ECHO. Deleting %src%
    doskey delme=del %src% $t exit $t set src=$t doskey delme=$t cls
    ::echo delme | %COMSPEC% /k % uncomment this line to self-delete %
     
    :end
    SET cnt=

     

    75.  Parse system date with / (slash) as separator.
     
    @ECHO off
    :: slashdat.bat

    :: Parses system date with / (slash) as separator
    :: Valid system format is: (mo/da/year)
    :: If system date is 09/15/2000, sets following variables:
    :: DAY=Fri; DATE=09/15/2000; DA=09; MO=15; Y2=00; Y4=2000
    :: D1=09; D2=15; D3=2000
    :: Tested in MS-DOS 6.22 and 7.1.
    IF "%1"=="ReCuRs[" GOTO recurs
    ECHO. |date |find "/" |IF errorlevel 1 FOR %%c in (echo goto:end) do %%c no slash in date
    ECHO.>%temp%.\flag.tmp
    XCOPY /Y %temp%.\flag.tmp %temp%.\temp2\ >nul
    DEL %temp%.\flag.tmp
    IF errorlevel 1 ECHO. xcopy error
    IF errorlevel 1 GOTO end
    ECHO @PROMPT SET date=$d> %temp%.\setdate1.bat
    %COMSPEC% /e:2048 /c %temp%.\setdate1.bat> %temp%.\setdate2.bat
    CALL %temp%.\setdate2.bat
    FOR %%f IN (%temp%.\setdate?.bat) do DEL %%f
    ECHO SET day=%%1>%temp%.\setday.bat
    ECHO SET date=%%2>>%temp%.\setday.bat
    CALL %temp%.\setday.bat %date%
    DEL %temp%.\setday.bat
    IF exist %temp%.\temp2\*. DEL %temp%.\temp2\*.
    ECHO @PROMPT $N:$_CD $P> %temp%.\setback1.bat
    %COMSPEC%/E:2048/C%temp%.\setback1.bat> %temp%.\goback.bat
    DEL %temp%.\setback1.bat
    %temp%.\
    CD %temp%.\temp2
    SET da=
    SET mo=
    SET y4=
    SET fil=%date%
     
    :loop
    ECHO SET fil=> %fil%
    FOR %%f in (*.) do SET fil=%%f
    IF not "%da%"=="" IF not "%mo%"=="" SET y4=%fil%
    IF not "%y4%"=="" GOTO last
    IF not "%da%"=="" IF "%mo%"=="" SET mo=%fil%
    IF "%da%"=="" SET da=%fil%
    FOR %%f in (*.) do COPY /Y %%f setfil.bat >nul
    CALL setfil.bat
    ECHO%fil%>setfil.dat
    ECHO. >>setfil.dat
    DATE <setfil.dat |find "):"> setfil.bat
    ECHO set fil=%%4> enter.bat
    CALL setfil.bat
     
    :last
    DEL *.
    IF "%y4%"=="" GOTO loop
    SET fil=
    SET d1=%da%
    SET d2=%mo%
    SET d3=%y4%
    FOR %%f in (enter.bat setfil.?at flag.tmp) do DEL %%f
    FOR %%c in (call del) do %%c %temp%.\goback.bat
    ECHO ; |CHOICE.COM/C;%y4%; %0;ReCuRs>%temp%.\parse1.bat
    %temp%.\PARSE1.BAT
     
    :recurs
    DEL %temp%.\parse1.bat
    FOR %%c in (1 2 3) do SHIFT
    SET y2=%1%2
    :end

     

    76.  Parse system dates without / (slash) as separator.
     
    @ECHO off
    :: nosldate.bat
    IF "%1"=="ReCuRs[" GOTO recurs
    IF "%1"=="ReCuRs2[" GOTO recurs2
    :: Author: Laurence Soucy, http://purl.oclc.org/net/Batfiles/
    :: Tested in MS-DOS 6.22
    :: Works for the following date formats
    :: 01-23-1993; 1993-01-23; 23-01-1993; 23.01.1993
    ::
    :: If system date is 01-15-2000, sets following variables:
    :: DAY=Fri; DATE=01-15-2000; DA=23; MO=01; Y2=00; Y4=2000
    :: D1=01; D2=23; D3=2000
    IF "%1"=="c" FOR %%v in (d1 d2 d3 y4 y2 day da mo date) do set %%v=
    IF "%1"=="C" FOR %%v in (d1 d2 d3 y4 y2 day da mo date) do set %%v=
    IF "%1"=="c" GOTO end
    IF "%1"=="C" GOTO end
    SET d1=
    SET d2=
    SET d3=
    SET y4=
    SET sep=
    SET src=
    ECHO. |date |find "/" >nul
    IF not errorlevel 1 ECHO. date must not use slash as separator
    IF not errorlevel 1 GOTO end
    ECHO @PROMPT SET date=$d> %temp%.\setdate1.bat
    %COMSPEC% /e:2048 /c %temp%.\setdate1.bat> %temp%.\setdate2.bat
    CALL %temp%.\setdate2.bat
    FOR %%f IN (%temp%.\setdate?.bat) do DEL %%f
    ECHO SET day=%%1>%temp%.\setday.bat
    CALL %temp%.\setday.bat %date%
    DEL %temp%.\setday.bat
    FOR %%v in (%date%) DO SET date=%%v
    SET src=%0
    SET d2=no
    SET d3=no
    SET src=%0
    ECHO ; | choice.com/s/c;%date%; %0;ReCuRs>%temp%.\parse1.bat
    %temp%.\PARSE1.BAT
     
    :recurs
    SHIFT
    SET d1=%1%2
    SHIFT
     
    :parse
    SHIFT
    IF "%1"=="]?" GOTO next
    FOR %%n in (0 1 2 3 4 5 6 7 8 9) do IF "%1"=="%%n" GOTO parse2
    SET sep=%1
    IF not "%d2%"=="no" IF "%d3%"=="no" SET d3=
    IF "%d2%"=="no" SET d2=
    SHIFT
     
    :parse2
    IF "%d2%"=="no" FOR %%c in (SET GOTO:parse) do %%c d1=%d1%%1
    IF "%d3%"=="no" FOR %%c in (SET GOTO:parse) do %%c d2=%d2%%1
    FOR %%c in (SET GOTO:parse) do %%c d3=%d3%%1
     
    :next
    ECHO ; |CHOICE.COM/C%d3%; >nul
    IF errorlevel 5 SET y4=%d3%
    IF "%y4%"=="" SET y4=%d1%
    IF "%y4%"=="%d1%" SET mo=%d2%
    IF "%y4%"=="%d1%" SET da=%d3%
    IF "%y4%"=="%d1%" GOTO next2
    ECHO. |DATE 01%sep%31%sep%%y4% |FIND "):" >nul
    IF not errorlevel 1 SET da=%d1%
    IF not errorlevel 1 SET mo=%d2%
    IF     errorlevel 1 SET da=%d2%
    IF     errorlevel 1 SET mo=%d1%
     
    :next2
    ECHO. |DATE %date% >nul
    SET sep=
    ECHO ; |CHOICE.COM/C;%y4%; %src%;ReCuRs2>%temp%.\parse1.bat
    %temp%.\PARSE1.BAT
     
    :recurs2
    SET src=
    DEL %temp%.\parse1.bat
    FOR %%c in (1 2 3) do SHIFT
    SET y2=%1%2

    :end

     

    77.  Get space free on drive into variable.
    Ansi.sys optional; Esc represents the escape character, created in edit by 'Ctrl+P Esc'.

     
    @ECHO off
    :: spacfri.bat
    :: Author: Laurence Soucy, http://purl.oclc.org/net/Batfiles/
    :: Does not work in NT
    IF "%1"=="ReCuRs" GOTO recurs
    SET free=
    DIR/A/W *. |FIND.EXE "bytes free" >%temp%.\spacfri0.tmp
    ECHO @PROMPT SET free=%%%%free%%%%>%temp%.\spacfri1.bat
    FC/N/LB1 %temp%.\spacfri0.tmp nul |find "1:">>%temp%.\spacfri1.bat
    CTTY nul
    %COMSPEC%/C%temp%.\spacfri1.bat> %temp%.\spacfri2.bat
    CTTY con
    :: %opt%ECHO Esc[2A
    CALL %temp%.\spacfri2.bat
    DEL %temp%.\spacfri?.*
    %0 ReCuRs %free%
     
    :recurs
    FOR %%n in (1 2) do SHIFT
    SET free=%1
     
    :loop
    SHIFT
    IF "%1"=="bytes" GOTO end
    SET free=%free%%1
    GOTO loop
     
    :end

     

    78.  Get up to nine words of first line of input file into variables.
    Ansi.sys optional; Esc represents the escape character, created in edit by 'Ctrl+P Esc'.
     
    @ECHO off
    :: setvars.bat
    :: Author: Laurence Soucy, http://purl.oclc.org/net/Batfiles/
    :: Sets variables in sequence var1...var9, for
    :: up to 9 words on first line of input file.
    IF "%1"=="ReCuRs+" FOR %%c in (SHIFT SHIFT GOTO:recurs) do %%c
    IF "%1"=="" FOR %%c in (ECHO GOTO:end) do %%c existing filename required
    IF not exist %1 FOR %%c in (ECHO GOTO:end) do %%c existing filename required
    SET str=
    ECHO @PROMPT SET str=%%%%str%%%%>%temp%.\setvar1.bat
    FC/N/LB1 %1 nul |FIND "1:" >>%temp%.\setvar1.bat
    CTTY nul
    %COMSPEC%/E:2048/C%temp%.\setvar1.bat> %temp%.\setvar2.bat
    CTTY con
    ::%opt%ECHO Esc[2A
    for %%c in (%temp%.\setvar2.bat del) do call %%c %temp%.\setvar?.bat
    %0 ReCuRs+ %str%
     
    :recurs
    SET str=
    IF "%1"=="" ECHO. first line of input file is blank
    SET var1=%1
    SET var2=%2
    SET var3=%3
    SET var4=%4
    SET var5=%5
    SET var6=%6
    SET var7=%7
    SET var8=%8
    SET var9=%9
    :end

     

    79.  Compare two numbers and determine which is larger/smaller.
     
    @ECHO off
    :: cmp2nbrs.bat
    :: author: Laurence Soucy, http://purl.oclc.org/net/Batfiles/
    :: base 10 or decimal positive integers only, no delimiters
    :: leading 0 only accepted in numbers of equal length
    IF "%2"=="" ECHO. enter two numbers to compare
    IF "%2"=="" GOTO end
    IF "%1"=="%2" ECHO. numbers are identical
    IF "%1"=="%2" GOTO end
    SET zero=
    ECHO +%1 +%2 |FIND "+0" >nul
    IF not errorlevel 1 SET zero=yes
    IF exist %temp%.\compstr?.tmp DEL %temp%.\compstr?.tmp
    ECHO %1>%temp%.\compstr1.tmp
    ECHO %2>>%temp%.\compstr2.tmp
    DIR/A-D/OS/B %temp%.\compstr?.tmp | FIND/N/V "" |FIND/I "1]compstr2.tmp" >nul
    IF not "%zero%"=="yes" IF not errorlevel 1 ECHO. integer two is smaller than integer one
    IF not "%zero%"=="yes" IF not errorlevel 1 GOTO last
    IF "%zero%"=="yes" IF not errorlevel 1 ECHO. leading 0 only accepted in numbers of equal length
    IF "%zero%"=="yes" IF not errorlevel 1 GOTO last
    DIR/A-D/O-S/B %temp%.\compstr?.tmp | FIND/N/V "" |FIND/I "1]compstr2.tmp" >nul
    IF not "%zero%"=="yes" IF not errorlevel 1 ECHO. integer one is smaller than integer two
    IF not "%zero%"=="yes" IF not errorlevel 1 GOTO last
    IF "%zero%"=="yes" IF not errorlevel 1 ECHO. leading 0 only accepted in numbers of equal length
    IF "%zero%"=="yes" IF not errorlevel 1 GOTO last
    ECHO %1>%temp%.\compstr1.tmp
    ECHO %2>>%temp%.\compstr1.tmp
    SORT.EXE < %temp%.\compstr1.tmp | FIND/N/V "" |FIND/I "1]%2" >nul
    IF not errorlevel 1 ECHO. integer two is smaller than integer one
    IF     errorlevel 1 ECHO. integer one is smaller than integer two
     
    :last
    SET zero=
    DEL %temp%.\compstr?.tmp
    :end

     

    80.  Get newest file in current or specified directory into a variable.
     
    :: newest.bat
    @ECHO off
    :: Author: Laurence Soucy, http://purl.oclc.org/net/Batfiles/
    :: Get newest file, by date or last modified, into a variable.
    :: Accepts wildcard specification for parameter 1.
    IF "%1"=="ReCuRs" GOTO recurs
    SET new=
    SET fspec=%1
    DIR/A-D/O-D/B/-P %1> %temp%.\newest1.tmp
    ECHO @PROMPT SET new=%%%%new%%%%>%temp%.\newest1.bat
    FC/N/LB1 %temp%.\newest1.tmp nul |find "1:">>%temp%.\newest1.bat
    CTTY nul
    %COMSPEC%/C%temp%.\newest1.bat> %temp%.\newest2.bat
    CTTY con
    CALL %temp%.\newest2.bat
    DEL %temp%.\newest?.*
    %0 ReCuRs %new%
     
    :recurs
    SHIFT
    SHIFT
    SET new=%1
    IF not "%fspec%"=="" ECHO. newest %fspec% file (by date) is %new%
    IF     "%fspec%"=="" ECHO. newest file (by date) is %new%
    SET fspec=
    ::

     

    81.  Display all logical hard drives.
     
    ::@ECHO off
    is-hd.bat
    :: author: Laurence Soucy, http://purl.oclc.org/net/Batfiles/
    :: displays all logical hard drives
    IF "%1"=="ReCuRs" GOTO recurs
    SET con=con
    IF not "%1"=="" SET con=%1
    :: ASCII Assembler by Laura Fairhead (used with permission).
    ECHO XPYP[*'CC-\1P\QX,=P,APZ5O!PQ2O~5aaI~}Ksx>%temp%.\echos.com
    CTTY nul
    %0 ReCuRs c d e f g h i j k l m n o p q r s t u v w x y z
     
    :recurs
    SHIFT
    IF "%1"=="" GOTO end
    :: There is a single symbol after ECHO which is made in
    :: edit by: [Ctrl+P][C] and shows up as a small heart.
    :: This is ASCII character number 3.
    ECHO [Ctrl+P][C]|FORMAT.COM %1: /F:160 |FIND.EXE "NON-REMOVABLE DISK" >nul
    IF not errorlevel=1 %temp%.\echos.com %1>>%con%
    GOTO recurs
     
    :end
    SET con=
    DEL %temp%.\echos.com
    CTTY con

     

    82.  Get the number of files in current or specified directory into a variable.
     
    @ECHO off
    :: filecnt.bat
    :: author: Laurence Soucy, http://purl.oclc.org/net/Batfiles/
    :: Get the number of files in current or specified
    :: directory into a variable. Use /S switch as parm2
    :: to include subdirectories in file count.
    :: useage: filecnt [dir] [/s]
    DIR/A-D/B/-P%2 %1 >%temp%.\setcnt1.tmp
    ECHO @PROMPT $N:$_CD $P> %temp%.\setback1.bat
    %COMSPEC%/E:2048/C%temp%.\setback1.bat> %temp%.\goback.bat
    FOR %%c in (%temp%.\ CD) do %%c %temp%.
    ECHO SET cnt=%%2>----------.bat
    FIND.EXE/C/V "" setcnt1.tmp >setcnt1.bat
    FOR %%c in (call del) do %%c setcnt1.bat
    FOR %%f in (setcnt1.tmp ----------.bat setback1.bat) do DEL %%f
    FOR %%c in (call del) do %%c %temp%.\goback.bat
    IF "%1"=="" ECHO. %cnt% files in current directory
    IF not "%1"=="" ECHO. %cnt% files in %1

     

    83.  Get number of lines in a file containing a specific character/word into a variable.
    This batch can easily be further developed to include multiple words and the /I switch for case
    insensitive searches as an option.

     
    @ECHO off
    :: charcnt.bat
    :: author: Laurence Soucy, http://purl.oclc.org/net/Batfiles/
    :: Get the number of lines in a file containing a specified
    :: character or word into a variable. Case sensitive.
    :: useage: charcnt file char
    IF "%2"=="" ECHO. Useage: CHARCNT file char/word
    IF "%2"=="" GOTO end
    IF not exist %1 ECHO. file '%1' is invalid
    IF "%2"=="" GOTO end
    FIND.EXE/C "%2" %1 >%temp%.\setcnt1.bat
    IF errorlevel 1 ECHO. no '%2' characters found
    IF errorlevel 1 GOTO end
    ECHO @PROMPT $N:$_CD $P> %temp%.\setback1.bat
    %COMSPEC%/E:2048/C%temp%.\setback1.bat> %temp%.\goback.bat
    FOR %%c in (%temp%.\ CD) do %%c %temp%.
    ECHO SET cnt=%%2>----------.bat
    FOR %%c in (call del) do %%c setcnt1.bat
    FOR %%f in (----------.bat setback1.bat) do DEL %%f
    FOR %%c in (call del) do %%c %temp%.\goback.bat
    ECHO. %cnt% line(s) containing '%2' in file '%1'
    :end
    ::

     

    84.  Get computer name into a variable (1).
     
    @ECHO off
    :: compname.bat
    :: Get computer name into a variable - for computer names without spaces
    :: From "Batfiles: The DOS batch file programming handbook & tutorial"
    :: Copyright 1997-2003 by Laurence Soucy
    :: http://purl.oclc.org/net/batfiles/
    ECHO n|net config |find "service is not started"
    IF not errorlevel 1 GOTO end
    :: ASCII Assembler by Laura Fairhead
    ECHO XPYP[*'CC-\1P\QX,=P,APZ5O!PQ2O~5aaI~}Ksx>%temp%.\echos.com
    %temp%.\echos.com SET compname=>%temp%.\cmpnam1¤.bat
    DEL %temp%.\echos.com
    NET config | find "Computer name">>%temp%.\cmpnam1¤.bat
    IF     errorlevel 1 ECHO. computer name not found
    IF not errorlevel 1 CALL %temp%.\cmpnam1¤.bat
    IF not errorlevel 1 DEL %temp%.\cmpnam1¤.bat
    FOR %%v in (%compname%) do SET compname=%%v
    ECHO. Computer name is %compname%
    :end
    ::
    ::

     

    85.  Get computer name into a variable (2).
     
    @ECHO off
    :: compnam2.bat
    :: Get computer name into a variable - for computer names with (single) spaces.
    :: From "Batfiles: The DOS batch file programming handbook & tutorial"
    :: Copyright 1997-2003 by Laurence Soucy
    :: http://purl.oclc.org/net/batfiles/
    IF "%1"=="ReCuRs" FOR %%c in (SET GOTO:recurs) do %%c compname=
    ECHO N|NET config |find "service is not started"
    IF not errorlevel 1 GOTO end
    :: ASCII Assembler by Laura Fairhead
    ECHO XPYP[*'CC-\1P\QX,=P,APZ5O!PQ2O~5aaI~}Ksx>%temp%.\echos.com
    %temp%.\echos.com SET compname=>%temp%.\cmpnam2¤.bat
    DEL %temp%.\echos.com
    NET config | find "Computer name">>%temp%.\cmpnam2¤.bat
    IF errorlevel 1 ECHO. computer name not found
    IF errorlevel 1 GOTO end
    CALL %temp%.\cmpnam2¤.bat
    DEL %temp%.\cmpnam2¤.bat
    %0 ReCuRs %compname%
     
    :recurs
    SHIFT
    SHIFT
    SHIFT
    SET compname=%1
     
    :loop
    SHIFT
    SET compname=%compname% %1
    IF not "%2"=="" GOTO loop
    IF not "%compname%"=="" ECHO. Computer name is %compname%
    :end
    ::

     

    86.  Get computer name into a variable (3).
     
    @ECHO off
    :: computer.bat (name must not be changed)
    :: Get computer name into a variable - for computer names without spaces
    :: From "Batfiles: The DOS batch file programming handbook & tutorial"
    :: Copyright 1997-2003 by Laurence Soucy
    :: http://purl.oclc.org/net/batfiles/
    IF "%1"=="name" GOTO recurs
    SET cname=
    ECHO n|net config |find "service is not started"
    IF not errorlevel 1 GOTO end
    NET config |find "Computer name"> %temp%.\cname¤.bat
    IF not errorlevel 1 %temp%.\cname¤.bat
     
    :recurs
    SET cname=%2
    FOR %%\ in (%%) do echo set cname=%%%cname%>%temp%.\cname¤.bat
    CALL %temp%.\cname¤.bat
    FOR %%\ In (%%) do echo set cname=%%%cname%>%temp%.\cname¤.bat
    FOR %%c in (call del) do %%c %temp%.\cname¤.bat
    IF not "%cname%"=="" ECHO. computer name is %cname%
    IF     "%cname%"=="" ECHO. computer name not found
    :end
    ::

     

    87.  Get computer name into a variable (4). {added 19 Oct 2002}
    This batch was kindly submitted by Frank-Peter Schultze, http://www.fpschultze.de/batstuff.html.
    It uses a DOS ported UNIX tool, SED (see links.htm#batchutil and download.htm#texttools).
     
    @Echo off
    Net Config | Sed -n "s/.*\\\\/Set COMPUTERNAME=/w %TEMP%.\¤CNAME.BAT"
    For %%C In (Call Del) Do %%C %TEMP%.\¤CNAME.BAT

    88.  Check for network connection through network adapter.
     
    @ECHO off
    :: ipconect.bat
    IPCONFIG.exe |find "IP Address" >nul
    IF not errorlevel 1 ECHO. connected
    IF     errorlevel 1 ECHO. not connected

     

    89.  Get IP address.
     
    @ECHO off
    :: ip.bat (name must not be changed)
    :: Get IP number (IP address) into avariable
    :: From "Batfiles: The DOS batch file programming handbook & tutorial"
    :: Copyright 1997-2002 by Laurence Soucy
    :: http://purl.oclc.org/net/Batfiles/
    IF "%1"=="Address." GOTO recurs
    IPCONFIG.exe |find/v ": 0" |find "IP Address."> %temp%.\¤ip.bat.bat
    IF not errorlevel 1 %temp%.\¤ip.bat.bat
     
    :recurs
    SHIFT
    SET ipnr=%1
    IF not "%2"=="" goto recurs
    IF exist %temp%.\¤ip.bat.bat del %temp%.\¤ip.bat.bat
    IF not "%ipnr%"=="" ECHO. IPnumber is %ipnr%
    IF     "%ipnr%"=="" ECHO. IP address not found
    ::
    :: The extra find statement should eliminate a dial-up adapter IP address,
    :: but find the ethernet adapter address. This MSDOS batch does not work
    :: in Window version 4.0 (a.k.a. "95") which is missing ipconfig.exe, but
    :: according to reports, actually does work in NT (NT/2000/XP).
     
    :end

     

    90.  Get username.
     
    @ECHO off
    :: user.bat (name must not be changed)
    :: Get username into a variable - for names without spaces
    :: From "Batfiles: The DOS batch file programming handbook & tutorial"
    :: Copyright 1997-2002 by Laurence Soucy
    :: http://purl.oclc.org/net/Batfiles/
    IF "%1"=="name" goto recurs
    ECHO n|net config |find "service is not started"
    IF not errorlevel 1 GOTO end
    NET config |find "User name"> %temp%.\getuser¤.bat
    IFnot errorlevel 1 %temp%.\getuser¤.bat
     
    :recurs
    SET user=%2
    IF not "%user%"=="" ECHO. User name is %user%
    IF     "%user%"=="" ECHO. No User name set
    DEL %temp%.\getuser¤.bat
    :end
    ::

     

    91.  Get physical address or MAC.
     
    @ECHO off
    :: physical.bat (name must not be changed)
    :: Get physical address (MAC)
    :: From "Batfiles:The DOS batch file programming handbook & tutorial"
    :: Copyright 1997-2002 by Laurence Soucy
    :: http://purl.oclc.org/net/Batfiles/
    IF "%1"=="Address." GOTO recurs
    IPCONFIG /all |find "Physical Address. "> %temp%.\physical¤.bat
    IF not errorlevel 1 %temp%.\physical¤.bat
     
    :recurs
    SHIFT
    SET mac=%1
    IF not "%2"=="" goto recurs
    DEL %temp%.\physical¤.bat
    IF not "%mac%"=="" ECHO. MAC number is %mac%
    IF    "%mac%"=="" ECHO. MAC number not found
    ::

     

    92.  Get physical address or MAC (2).
     
    @ECHO off
    :: getmac.bat
    :: Get MAC number
    :: From "Batfiles: The DOS batch file programming handbook & tutorial"
    :: Copyright 1997-2002 by Laurence Soucy
    :: http://purl.oclc.org/net/Batfiles/
    IPCONFIG.exe /all |FIND.EXE "PhysicalAddress">%temp%\physica¤.bat
    IF errorlevel 1 DEL %temp%\physica¤.bat
    IF errorlevel 1 ECHO. MAC number not found
    IF errorlevel 1 GOTO end
    ECHO @PROMPT $N:$_CD $P$_ >%temp%.\setback1.bat
    %COMSPEC%/E:4096/C%temp%.\setback1.bat> %temp%.\goback.bat
    DEL %temp%.\setback1.bat
    FOR %%c in (%temp%.\ cd) do %%c %temp%.
    ECHO set mac=%%8>Physical.bat
    CALL physica¤.bat
    DEL physica?.bat
    FOR %%c in (call del) do %%c %temp%.\goback.bat
    IF not"%mac%"=="" ECHO. MAC number is %mac%
     
    :end
    ::

     

    93.  Print special characters to the console (screen) or to a file
     
    :: ecospchr.bat
    @ECHO off
    :: Print special characters to the console (screen) or to a file
    :: From "Batfiles: The DOS batch file programming handbook & tutorial"
    :: Copyright 1997-2002 by Laurence Soucy
    ::http://purl.oclc.org/net/Batfiles/
    IF "%1"=="GOTO:" GOTO recurs
    CLS
    ECHO.
    ECHO.
    ECHO          Welcome to the mini special character demo :-)
    ECHO.
    PAUSE
    CLS
    ECHO.
    ECHO.
    ECHO. ============= 1 =============
    @ECHO on
    %COMSPEC%/CFOR %%n in (1 2) do PROMPT Here comes a pipe $b $_|FIND/v/i "PROMPT">%temp%.\temp.txt
    FIND "a pipe"<<%temp%.\temp.txt
    @ECHO off
    DEL %temp%.\temp.txt
    ECHO.
    PAUSE
    CLS
    ECHO.
    ECHO.
    ECHO. ============= 2 =============
    @ECHO on
    ECHO @PROMPT Press $lthis particular key$g to continue...>%temp%.\temp.bat
    %COMSPEC%/E:4096/C%temp%.\temp.bat
    @ECHO off
    ECHO.
    PAUSE
    CLS
    ECHO.
    ECHO.
    ECHO. ============= 3 =============
    @ECHO on
    ECHO @PROMPT Make a fence $b=$b=$b=$b=$b=$b> %temp%.\temp.bat
    %COMSPEC%/E:4096/C%temp%.\temp.bat
    @ECHO off
    ECHO.
    PAUSE
    CLS
    ECHO.
    ECHO.
    ECHO. ============= 4 =============
    @ECHO on
    ECHO. Now let's place a ">" character into a file.
    ECHO @PROMPT a+b $g c+d$_> %temp%.\spchar1.bat
    COMMAND/E:4096/C %temp%.\spchar1.bat |FIND "+" >%temp%.\results.txt
    TYPE %temp%.\results.txt
    @ECHO off
    DEL %temp%.\spchar1.bat
    DEL %temp%.\results.txt
    ECHO.
    PAUSE
    CLS
    ECHO.
    ECHO.
    ECHO. =============5 =============
    @ECHO on
    ECHO %%"%% ECHO Y |DEL temp.tmp %%"%%> %temp%.\temp.bat
    TYPE %temp%.\temp.bat
    @ECHO off
    ECHO.
    PAUSE
    CLS
    ECHO.
    ECHO.
    ECHO. ============= 6 =============
    @ECHO on
    ECHO dir/ad/w%% "%%|%% "%% more> %temp%.\temp.bat
    TYPE %temp%.\temp.bat
    @ECHO off
    ECHO.
    PAUSE
    CLS
    %COMSPEC%/E:4096/C%0 GOTO: recurs>>%temp%.\temp.bat
    FOR %%c in (type del) do %%c %temp%.\temp.bat
    ECHO.
    PAUSE
    CLS
    ECHO.
    ECHO.
    ECHO                             I hope you enjoyed it!
    GOTO end
     
    :recurs
    CLS
    ECHO.
    ECHO.
    ECHO. ============= 7 =============
    ECHO.
    PROMPT [This is a user-defined prompt in three lines]$_[$d $b$t$_$p$g
    ECHO on
    % This line needed to echo the temporary PROMPT to temp.bat %
    @ECHO off
    :end

     

    94.  Get a pseudo random number into a variable  {added 12 Sep 2002, revised 14 Sep 2002}
    As written, the options are embedded or hardwired, and the batch must be edited manually
    to change them. It is, however, straightforward to enhance this batch so the options may be
    selected on the fly with command line switches. This I leave as an exercise for the reader.
    See batch for options.
     
    :: random#.bat
    :: Get a pseudo random number (1-99, 0-99, or 1-100) into
    :: [%rnbr%] variable with optional leading zero suppresion.
    @ECHO off
    :: From "Batfiles: The DOS batch file programming handbook & tutorial"
    :: Copyright 1997-2002 by Laurence Soucy
    :: http://purl.oclc.org/net/Batfiles/
    IF "%1"=="PaRsE[" FOR %%c in (SET GOTO:parse) do %%c rnbr=
    ECHO %0> %temp%.\rand¤0.bat
    ECHO @PROMPT SET rnbr=$t> %temp%.\rand¤1.bat
    %COMSPEC%/e:2048/c%temp%.\rand¤1.bat> %temp%.\rand¤2.bat
    CALL %temp%.\rand¤2.bat
    FOR %%n in (%rnbr%) do SET rnbr=%%n
    ECHO ; |CHOICE.com/C;%rnbr%; %0;PaRsE>%temp%.\rand¤1.bat
    %temp%.\rand¤1.bat
     
    :parse
    SHIFT
    :::[opt 1] The following line active makes range from 1-99 instead of 0-99
    ::IF "%1"=="0" IF "%0"=="0" %temp%.\rand¤0.bat
    ::: [opt 2] The following line active provides leading zero suppression.
    IF "%2"=="]?" IF"%0"=="0" SET rnbr=%1
    IF "%2"=="]?" IF "%rnbr%"=="" SET rnbr=%0%1
    IF not "%1"=="]?" GOTO parse
    ::: [opt 3] The following line active makes range from 1-100 instead of 0-99
    ::: (this option automatically deactivated if [opt 1] is active)
    FOR %%n in (0 00) do if "%%n"=="%rnbr%" SET rnbr=100
    IF not "%rnbr%"=="" ECHO. random number is %rnbr%
    IF     "%rnbr%"=="" ECHO. something went wrong :(
    DEL %temp%.\rand¤?.bat
    ::
    :: The following table showsthe various possible results
    :: determined by the effective options (1=active, 0=deactive).
    ::
    :: [opt 1]  [opt 2]  [opt 3]          Returns
    ::    0        0        0       random nbr btw 00-99
    ::    0        0        1       random nbr btw 01-100
    ::    0        1        0       random nbr btw 0-99
    ::    0        1        1       random nbr btw 1-100
    ::    1        0       0/1      random nbr btw 01-99
    ::    1        1       0/1      random nbr btw 1-99
     

    95.  Remove (zero byte) empty directories  {added 24 Jan 2004}
    Removes empty directories from current drive location.

     
    :: rmv0dirs.bat (remove 0 byte directories)
    @echo off
    set quote=
    :: Author: Laurence Soucy, Jan 6, 2003
    :: From "Batfiles: TheDOS batch file programming handbook & tutorial"
    :: Copyright 1997-2004 by Laurence Soucy
    :: http://purl.oclc.org/net/Batfiles/
    :: Tested in MS-DOS 6.22/Windows 3.11, MS-DOS 7.1/Windows 4.1 (a.k.a. 98)
    for %%x in (/ %msdos7.0+%) do set quote="
    echo @prompt rd %quote%>%temp%.\¤1.bat
    set quote=
    dir/ad/b/-p>>%temp%.\¤1.bat
    echo @prompt $n:$_cd $p$_ %=%>%temp%.\¤goback1.bat
    %comspec%/e:4096/c%temp%.\¤goback1.bat>%temp%.\¤goback2.bat
    for %%c in (%temp%.\ cd) do %%c %temp%.
    path>%temp%.\¤respath.bat
    path;
    ctty nul
    %comspec%/e:4096/c%temp%.\¤1.bat>%temp%.\¤2.bat
    for %%c in (call del) do %%c %temp%.\¤respath.bat
    call %temp%.\¤goback2.bat
    del %temp%.\¤goback?.bat
    call %temp%.\¤2.bat
    ctty con
    del %temp%.\¤?.bat