DOS Batch file programming
|
FOR %%f in (%1) do IF %%f==%1 GOTO nowilds 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 ": " 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 .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" 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 :: 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 :: COMMAND/CTEST.BAT >results.txt PATH drv:\DirName;%path% :: Or PATH %path%;drv:\DirName FIND.EXE /V "String" <anyfile> %temp%.\anyfile.tmp COPY/Y %temp%.\anyfile.tmp anyfile DEL %temp%.\anyfile.tmp 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 :: 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% :: 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%" 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 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 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 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 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. MEM.EXE /D |FIND.EXE "LASTDRIVE" 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 PATH>%temp%.\respath.bat :: restore CALL %temp%.\respath 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 :: 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 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 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 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 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 :: :: 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 :: 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 :: :: 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 :: 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% :: 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= :: :: 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 :: 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 :: 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. :: 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 :: 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 :: 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= :: 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_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.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. :: 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 :: 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 :: 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 :: 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 :: 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 :: 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 :: DRVSPACE /LI |FIND.EXE "Local hard drive" 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 :: :: 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 :: 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 :: 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.
54. Get bytes required by current environment.
55. Get serial number of drive.
56. Get volume label of drive.
57. Place command output or line of text
into a variable with a helping file. {last modified Sep. 14, 2004}
58. Get filename of an existing file into a variable.
59. Get directory of an existing file into a variable.
60. Get full path of an existing file into a variable.
61. Get drive
letter of a full path into a variable.
62. Get file extension into a variable.
63.
Get file date into a variable.
64. Get the size of a file into a variable (1).
65. Get the size of a file into a variable (2).
66. Get file time into a variable.
67. Edit the path at the DOS prompt. {last modified Dec. 30, 2002} 68. Isolate the first (non-system/hidden) file in a directory. 69. Display boot drive letter or place into a variable.
70. Place first character of string into a
variable.
71. Display current drive letter or place into a variable.
72. Remove 1st character of string and place remainder into variable.
73. Make a batch file self-delete.
74. Make a batch
file self-delete with doskey.
75. Parse system date with / (slash) as separator. 76. Parse system dates without / (slash) as separator.
77. Get space free on drive into variable.
78. Get up to nine words of first line of input file into variables.
79. Compare two numbers and determine which is larger/smaller.
80. Get newest file in current or specified
directory into a variable.
81. Display all
logical hard drives.
82. Get
the number of files in current or specified directory into a variable.
83. Get number of lines in a file containing a specific character/word into a variable.
84. Get computer name into a variable (1).
85. Get computer name into a variable (2).
86. Get computer name into a variable (3).
87. Get computer name into a variable (4). {added 19 Oct 2002}
89. Get IP address.
90. Get username.
91. Get physical address or MAC.
92. Get physical address or MAC (2).
93. Print special characters to the console (screen) or to a file
94. Get a pseudo random number into a variable {added 12 Sep 2002, revised 14 Sep 2002}
95. Remove (zero byte) empty directories {added 24 Jan 2004} |