THE BATPOWER FAQ, or Frequently Asked Questions *** Last revision: 03 May 96 *** +----------------------------------------------------------------+ | Table of Contents for | 14. Path over 128 characters | | FREQUENTLY ASKED QUESTIONS, | 15. What is the 'MUF'? | | (FAQ) for the BATPOWER Echo | 16. Recursion/Loops in a BAT | |-------------------------------+ 17. Start-up screen rotation | | 1. How to reboot in a BATch | 18. Tidying output from a BAT | | 2. Communicate with a modem | 19. Using ANSI codes in a BAT | | 3. Date & Time Use in a BAT | 20. REM or :: in a BATch | | 4. Combine files into one | 21. Is bat run from Windows ? | | 5. Formatting Disks Q&A | 22. Using Ctrl+Z in a BATch | | 6. Getting errorlevel Q&A | 23. Zero byte files- questions | | 7. Help with making a menu | 24. Banish 'Retry,Abort,Ignore'| | 8. Is TSR loaded in memory ? | 25. Getting user BATch input | | 9. Jump back to prior dir | 26. BATch Programming Books | | 10. Find&Replace text in file | 27. | | 11. Low level HD format Q&A | 28. | | 12. FOR..IN..DO Q&A's | 29. | | 13. Out of Environment Q&A's | 30. | +----------------------------------------------------------------+ 1. HOW TO REBOOT IN A BATch ~~~~~~~~~~~~~~~~~~~~~~~~ Q: How do I reboot the PC from inside a batch file ? A: ECHO G=FFFF:0 | DEBUG >NUL for MSDOS} these occasionally ECHO GFFFF:0000 | SID >NUL for DRDOS} will give garbage -------------------------------------------------------------- ECHO HPSË>REBOOT.COM This seems reliable, most editors REBOOT.COM will allow Alt+203 to get the Ë Hold the ALT key while 2 0 3 is entered at the r/h keypad (Numlock ON) -------------------------------------------------------------- This DEBUG script will produce COLDBOOT.COM when you run it ------------------------------------+ thus: DEBUG to ECHO AT commands to it. E.G. MODE COM1 baud=19200 data=8 parity=n stop=1 ECHO AT&FF10\N4&D3E0M0Q1S0=1&W0>COM1 might be used to set up a modem loading a profile into it or if you wanted to leave a computer accessible from a remote location you might shell out of a communications program and run a batch file containing: %COMSPEC% /e:1024 >COM1 $$TEMP$$.BAT ECHO SET CTIME=%%3>CURRENT.BAT $$TEMP$$.BAT will place the current TIME into an envar called CTIME and TIME can be replaced with DATE above to create CDATE. To create a log of system boots the following could be inserted into the AUTOEXEC.BAT file: ECHO -=-=-=-=-=-=-=-=-=-=-=-=-=-=->>BOOTLOG.TXT ECHO System booted !!! >>BOOTLOG.TXT : For more : VER|TIME|FIND "Current">>BOOTLOG.TXT : techniques : VER|DATE|FIND "Current">>BOOTLOG.TXT : type $ : Using P2E to create filenames in YYMMDD.* format : @ECHO OFF ECHO.|DATE|P2E /E MM /L 1 /M 21,2 /E DD /L 1 /M 24,2 /E YY /L 1 /M 29,2 ECHO This is a New File>%YY%%MM%%DD%.TXT Using PROMPT and CALL to create an 'ECHO'able Current Date & Time : @ECHO OFF ECHO @PROMPT LOG ON: $d at $t$h$h$h>C:\TEMP\TEMP$$$$.BAT %COMSPEC% /C C:\TEMP\TEMP$$$$|FIND ":">>C:\LOGONS.TXT Typical resultant contents of c:\logons.txt will be: LOG ON: Fri 12-27-1993 at 09:37:48 LOG ON: Tue 01-04-1994 at 08:54:26 --etc.--etc.-- The section of the line between @PROMPT and the > is customizable and all $codes valid for the PROMPT command can be used as well as the current system date and time. 4. COMBINE FILES INTO ONE ~~~~~~~~~~~~~~~~~~~~~~ Q: I have lots of text files that I want to combine into one, how can I automate this with a BATch. A: The DOS COPY command can be used or redirection of TYPE's output as follows: COPY *.TXT ALL.XXX RENAME ALL.XXX ALL.TXT or FOR %%f IN (*.TXT) DO TYPE %%f>>ALL.XXX RENAME ALL.XXX ALL.TXT The renaming of the resultant file is necessary, as when it is created by the first COPY or TYPE operation, were it called ALL.TXT it would immediately qualify for inclusion in the command as it would match the filespec. COPY can do binary concatenations too, with the /B param MS-Windows remakes WIN.COM everytime Setup is used similar to: COPY /B WIN.CNF+VGALOGO.LGO+VGALOGO.RLE WIN.COM 5. FORMATTING DISKS- Q & A ~~~~~~~~~~~~~~~~~~~~~~~ Q: How do I format a disk without DOS asking me questions ? A: With MS-DOS 3.3 use FORMAT A: /H With MS-DOS 4.0+ use FORMAT A: /AUTOTEST !!! BEWARE - THIS WORKS ON HARD DISKS TOO !!! There are a number of other undocumented switches for both FORMAT and other MS-DOS features detailed in the MUF. (For information about the MUF see item # 15. below) 6. GETTING ERRORLEVELS- Q & A ~~~~~~~~~~~~~~~~~~~~~~~~~~ Q: How do I echo the errorlevel returned by a program to the screen so I can 'debug' my batch files ? A: There are a couple of utilities that can do this but the method used for the FAQ is the pure DOS solution below : FOR %%E IN (0,1,2) DO IF ERRORLEVEL %%E00 SET ERLVL=%%E IF "%ERLVL%"=="0" SET ERLVL= SET RANGE=0,1,2,3,4,5,6,7,8,9 IF "%ERLVL%"=="2" SET RANGE=0,1,2,3,4,5 FOR %%E IN (%RANGE%) DO IF ERRORLEVEL %ERLVL%%%E0 SET ERLVL=%ERLVL%%%E IF "%ERLVL%"=="0" SET ERLVL= IF NOT "%ERLVL%"=="25" SET RANGE=0,1,2,3,4,5,6,7,8,9 FOR %%E IN (%RANGE%) DO IF ERRORLEVEL %ERLVL%%%E SET ERLVL=%ERLVL%%%E SET RANGE= ECHO Errorlevel is %ERLVL% Here is a line-by-line walk thru' of this courtesy Rudy Lachin: FOR %%E IN (0,1,2) DO IF ERRORLEVEL %%E00 SET ERLVL=%%E If errorlevel is greater than 0 set ERLVL to 0, if >100 to 1 and if >200 to 2. IF "%ERLVL%"=="0" SET ERLVL= If ERLVL=0 clear it (suppress leading 0) SET RANGE=0,1,2,3,4,5,6,7,8,9 Now we've set the hundreds check tens except when the errorlevel is >200 in which case we only want to go up IF "%ERLVL%"=="2" SET RANGE=0,1,2,3,4,5 to the fifties. FOR %%E IN (%RANGE%) DO IF ERRORLEVEL %ERLVL%%%E0 SET ERLVL=%ERLVL%%%E If the errorlevel in this range of tens is above x00 set ERLVL to x00, if > x10 set ERLVL to x10, if > x20 set ERLVL to x20, etc. IF "%ERLVL%"=="0" SET ERLVL= If ERLVL=0 clear it (suppress leading 0) IF NOT "%ERLVL%"=="25" SET RANGE=0,1,2,3,4,5,6,7,8,9 Now we check the units except if the errorlevel is 250+ where we only check up to 5 by not resetting the RANGE variable contents but re-using it as set above. FOR %%E IN (%RANGE%) DO IF ERRORLEVEL %ERLVL%%%E SET ERLVL=%ERLVL%%%E If the errorlevel in range of units is above xy0 set ERLVL to xy0, etc SET RANGE= Clear the RANGE variable from the environment ECHO Errorlevel is %ERLVL% and display the variable ERLVL on screen. 7. HELP WITH MAKING A MENU ~~~~~~~~~~~~~~~~~~~~~~~ Q: How do I make a menu system with batch files ? A: Here's how you could do it with all BATs in a DIR in your PATH This is MENU.BAT | This is 3.BAT @ECHO OFF | @ECHO OFF CD\ | CD\ ECHO MAIN MENU | ECHO GAMES MENU ECHO [1] Word Processor | ECHO [31] DooM -Save The Universe! ECHO [2] Spreadsheet | ECHO [32] FRAC -Tetris Clone 3D ECHO [3] Games | ECHO [33] Return To Main Menu ECHO. | ECHO. PROMPT Type the number and[ENTER] | PROMPT Type the number and[ENTER] -------------+-------------+-------+------+--------------+-------------- This is 1.BAT|This is 2.BAT|This is 31.BAT|This is 32.BAT|This is 33.BAT @ECHO OFF | @ECHO OFF | @ECHO OFF | @ECHO OFF | @ECHO OFF CD\WP51 | CD\QPRO | CD\FUN\DOOM | CD\FUN | CD\ WP51.EXE | Q.EXE | DOOM.EXE | FRAC.EXE | CLS MENU.BAT | MENU.BAT | 3.BAT | 3.BAT | MENU.BAT 8. IS TSR LOADED IN MEMORY ? ~~~~~~~~~~~~~~~~~~~~~~~~~ Q: How can I test to make sure a TSR like ANSI.SYS is loaded? A: All memory-resident programs have to use memory and so an output from the MEM /C command will show them resident. The BATch below ('hook' needed) illustrates the technique. @echo off ----- if %1'==' goto syntax mem /c|find /i "%1">temp.tmp copy /b temp.tmp+,, >nul if exist temp.tmp echo Program: %1 was found in memory. if not exist temp.tmp echo Program: %1 is not loaded. if exist temp.tmp del temp.tmp goto end :syntax echo Usage: %0 [TSR to find in memory] echo Example: %0 mouse :end 9. JUMP BACK TO PRIOR DRIVE / DIRECTORY ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Q: When I run a program or command how can I get back to the directory that was current prior to command execution ? A: Several solutions are available as small utilities but below is a pure DOS solution.(using environment variables) (4DOS has built in variables to deal with this situation) ::Set Envars to current drive & current directory @echo @prompt SET OLDRV$Q$N$_SET OLDIR$Q$P >TEMP$TMP.BAT @command /c TEMP$TMP.BAT > TEMP$$MP.BAT @call TEMP$$MP>BAT >nul @del TEMP$?MP.BAT Once this BATch has been CALL'd or run: %OLDRV% will be evaluated to be the drive when BAT was run and %OLDIR% will be evaluated to be the prior directory. 10. FIND & REPLACE TEXT IN FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Q: Is there a piece of software that does string search and replacement from the command line ? A: EDLIN (included with MS-DOS) is ideal for this. EDLIN FILENAME.EXT nul another copy of COMMAND.COM if errorlevel 1 goto DOIT if environment space is less STRINGS esize= ENVSIZE than 300 bytes, setting the STRINGS esize= ADD %esize%, 300 new environment to 300 bytes %comspec% /e:%esize% /c %0 bigger than the current. goto END If space is already >or= 300 :DOIT it jumps to the DOIT label. echo We've got 300+ bytes space now! echo Environment Size: %esize% ; Environment Free: %efree% :END +----------------------------------------------- set efree= | With DOS you can check env. space usage with set esize= | SET>TEMP.TMP then DIR TEMP.TMP filesize=usage 14. PATH GREATER THAN 128 CHARACTERS ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Q: How can I make my Path bigger than 128 characters long ? A: To use more than DOS's command-line limit of 128 chars you need to set the PATH in the CONFIG.SYS file (MSDOSv6+) or you use an utility like STRINGS or P2E. As an alternative you may give sections of a directory tree a label as if they were a separate drive. This will cut the length of the path statement used for that subdir. SUBST K: C:\WINDOWS ( use in AE.BAT before the PATH line ) Using P2E: The following line in the Autoexec.Bat will set the path to the contents of the mypath.txt textfile. P2E Envar PATH NUL Will just wait for a keypress before continuing, without displaying the usual message: "Press any key to continue..." To hide error messages as well you'll need to use: CTTY NUL command that displays error you don't want to see CTTY CON Be Careful ! While CTTY NUL : the keyboard is ignored ! Q: Sometimes when I execute a batch file after running I'm left with more than one prompt (as if I'd hit Enter). A: This is because the batch file has trailing carriage- returns. Use a plain text (ASCII) editor to remove them. 19. Using ANSI (escape codes) in a BATch File ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Q: I know I need to use an ESC character (Alt+27) to use ANSI codes but whenever I try DOS or Editors give me a slash or clear the line ? A: Use DOS EDIT and press Ctrl+P (which puts you in 'literal' mode) and then press Esc and you'll see a small left pointing arrow; this is the Esc character. Some editors like QEDIT allow you to use the numeric keypad to enter the Alt+27 sequence as well (or instead). Specialist ANSI screens editors like TheDraw can save you a lot of time and are much easier to use ! To use ANSI in your PROMPT the $e code is an Esc character. If you use 4DOS use lines like the following instead; SCRPUT 6 34 BLINK BRIGHT RED ON BLUE W A R N I N G ! This will write 'W A R N I N G !' in blinking bright red on a blue background with the W at row 6, column 34. 20. Using REM or :: (double colons) in a BATch File ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Q: I heard double colons are better than REM statements, why? A: Even though a REM statement is a remark DOS will still read the whole line in case it's being re-directed. Double colons are seen as an invalid label by DOS when it starts to read the line so it skips on to the next line. The other way to speed up execution of a batch file that has lots of comments is to un-conditionally jump over the lines that are comments like this: @ECHO OFF GOTO START This is a comment line that'd slow down the BAT So is this line Ú------------------------- And so is this one as well | Be careful using double :START | colons with 4DOS ! --continue-with-batch-file-- |::LABEL is a valid label. 21. IS BATCH FILE RUNNING FROM WITHIN WINDOWS ? ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Q: How can I make sure Windows isn't running before I run a program in a batch file ? A: Either use a purpose-made COM program such as ISWINRUN.COM a DEBUG script for which is below, it will exit with 1 as ERRORLEVEL if Windows is running , OR use the FIND command to look at the output of the MEM /C command to search for Windows' 'hook' in memory. If Windows is running in 386Enh mode a 'hook' called win386 will be in memory when you are in a DOS window. Solutions are outlined below: ------------------------------------------------------------- nISWINRUN.COM e100 B8 80 46 CD 2F 3D 0 0 74 2 B0 1 34 1 B4 4C CD 21 RCX Ú---------------------------------------------------- 12 | MEM /C|FIND "win386">temp.$$$ W | COPY /B temp.$$$+,, Q | IF EXIST temp.$$$ ECHO This is a DOS Window ! 22. USING CTRL+Z IN A BATch ~~~~~~~~~~~~~~~~~~~~~~~ Q: How can you use Ctrl+Z in a batch file, whenever I put one in a BATch that's where execution of the BATch stops. ? A: This happens because Ctrl+Z is an End-Of-File Marker for DOS (EOF). Using EDLIN in a BATch often needs the Ctrl+Z to be entered into a script and one of the few ways to do this is from the command prompt. Type: SET ZED=(Ctrl+Z) Replacing the bracketed text with the real key combination. In BATch files thereafter you can use %ZED% where you are required to use a Control+Z key combination. As an example the little batch file below, will search README.TXT & replace every instance of 'Marvelous' with 'Wonderful'. ECHO 1,#RMarvelous%ZED%Wonderful>test.scr ECHO e>>test.scr EDLIN README.TXT FLAG.$$$ Here's an example of how you might use this 'feature': CD\ DIR /B /S|FIND /i "filename.ext">FLAG.$$$ COPY /B FLAG.$$$+,, IF EXIST FLAG.$$$ ECHO filename.ext was found IF NOT EXIST FLAG.$$$ ECHO filename.ext is not on drive. 24. BANISH 'Retry, Abort, Ignore' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Q: How do you intercept DOS's error message that you get if you try and print to a printer that's not ready OR when you try to read or write to/from an empty drive. A: The command line switch for command.com /F will make all such situations default to Fail. You can add the /F to your SHELL statement in CONFIG.SYS, or you can use it in a .BAT as below as a temporary fix for single BATch execution @echo off if %1.==. goto END echo %comspec% /f /c cprn$$$$.bat>temp$$$$.bat echo copy %1 prn: >cprn$$$$.bat call temp$$$$.bat if exist ????$$$$.bat del ????$$$$.bat >nul :END echo Usage: %0 [filename to print] 25. GETTING USER BATch INPUT ~~~~~~~~~~~~~~~~~~~~~~~~ Q: How can I get my BATch files to interact with the user ? A: Too many utilities have been written, to overcome this short-fall in the MS-DOS BATch language, to detail here so just some are shown below, with use examples: CHOICE (comes with DOS v6.xx) This use of choice nominates valid choices of A&B with a default to A after a 20 second wait and gives a prompt defined in the quotation marks ECHO *[A] Word Pro. ECHO [B] Database CHOICE /N /C:AB /T:A,20 "Enter Choice:" IF ERRORLEVEL 2 GOTO DATAB IF ERRORLEVEL 1 GOTO WORDP úúúúetcúúú INPUT (comes with the FAQ) INPUT sets the errorlevel to the ASCII value of the keypress so EL check is best in 'bracketed' manner shown ECHO Do you want to Quit (Y/N)? INPUT 30 IF ERRORLEVEL 89 IF NOT ERRORLEVEL 90 GOTO END IF ERRORLEVEL 121 IF NOT ERRORLEVEL 122 GOTO END 26. BATCH PROGRAMMING BOOKS ~~~~~~~~~~~~~~~~~~~~~~~ While the following books have been recommended by various echo users, any choice should always be based on your personal examination of the book, to ensure that it meets your needs. DOS Power Tools by Paul Somerson Supercharging MS-DOS by Van Wolverton MS-DOS Batch File Programming by Ronny Richardson Advanced MS-DOS Batch File Programming by Dan Gookin Concise Guide to MS-DOS Batch Files by Kris Jamsa ===================================================================== The above is a compilation of the information contained in the file: BATFAQ11.ZIP, presented in an ASCII text format, and 'updated' as deemed necessary, by the moderator, Bat Lang, on 5-03-96. We acknowledge the efforts of the many BATPOWER users who contributed to this collection, and particularly to our brethren in Zone: 3. This collection, as an entity, is the Copyright of the Fido Echo BATPOWER, and should receive credit as such. Any of the above, used piecemeal, is considered in the Public Domain. Any comments, additions or corrections, should be submitted to the moderator, addressed as below.* * Until 14 June 96, such comments should be made in the echo, using the Subj: BAT-FAQ. BATPOWER, Moderator NetMail at 1:382/1201 or E-mail: bat.lang@1201.ima.infomail.com