MS Dos handbook
1. Alternative ways to get directory listing. DIR DIR * DIR . DIR. DIR : DIR: DIR , DIR, The last two also list system and hidden files. 2. One or more . (dots) can represent a directory. Sometimes it's easier to use dots to represent directories. For example, if you'd like to move a file from another directory to the current, instead of writing the path to the current directory (which move.exe requires), use a dot: MOVE.EXE Path\anyfile . This can be demonstrated with the DIR command. One dot represents the current directory: DIR . Two dots represent the parent directory: DIR .. Three dots represent the directory above that: DIR ... And so on. 3. Directory listing of extentionless files. DIR/A *. will list all files without extensions. 4. Add or remove file extensions (except system or hidden files). REN *. *.txt will add .txt extension to files without extensions. REN *.txt *. will remove .txt extension on files that have it. 5. Rename multiple files (except system or hidden files). REN *.bat *.txt will rename all .bat files to .txt files. REN *.txt 1*.txt will rename all .txt files so their first character is 1. 6. Use %temp%.\ (case is irrelevant) to represent the temp variable. The temp variable can be set with or without a backslash, ie. c:\temp or c:\temp\, which can be problematic when using the variable in a batfile. Using %temp%.\ will refer correctly to the temp directory whether it has been set with a backslash or not. This method can be used with any variable whose value is set to a directory. 7. Use ..\ as the last directory in your Path statement. No matter where you are, the parent directory will always be in the Path. 8. Change Drive and Directory easily. There can be many reasons for changing drive and directory in a batfile, and this can be difficult if we only have a path and directory to go on. This can be accomplished with startling ease when one knows the secret, however. A drive can be made current just by entering a full path and directory at the prompt, provided a backslash is added at the end, ie. D:\AnyDir\, which entered at the prompt will make the D: drive current, if it is a valid drive. Oddly enough, the directory can be bogus and this will work nonetheless. If our directory is indeed valid, we can after changing drives, change to the directory by adding CD in front and repeating it without the backslash so we have, D:\AnyDir\ followed by CD D:\AnyDir Here's how it looks in a batfile: @ECHO off :: cdd.bat (Change Drive and Directory) :: Where %1 is a full path and directory without backslash. :: Examples: CDD D:\ValidDir or CDD D: (for root) %1\ CD %1 9. Suppress a command's output. Works with most commands. COPY file1 file2 >nul 10. Suppress a batch file's output. COMMAND/CTEST.BAT >nul 11. Temporarily increase environment while running a batch file. Specify a maximum of 32000 bytes with the /E switch. COMMAND/E:24000/CTEST.BAT 12. A timed delay which can not be interfered with from the keyboard. REM |CHOICE.COM /TY,5 >nul Substitute 5 with the desired number of seconds, up to 99. 13. Produce a constant alarm (beeping). CHOICE.COM /N < nul Continues until Ctrl+C or Ctrl+Break are used. 14. Break out of an endless loop. CHOICE.COM /N /TY >nul In the example, siren.com emits a siren sound for about 3.83 seconds each time it's executed. If the choice command is not used, it's impossible to break out using Ctrl+C or Ctrl+Break. :start SIREN.COM >nul CHOICE.COM /N /TY >nul GOTO start 15. An easy way to exit a batch file. TYPE nul >%temp%.\batexit.bat %temp%.\BATEXIT 16. Use FOR to process GOTO's. FOR does not see the colon as a delimiter, but GOTO does. Therefore, they can be used together. IF "%1"=="" FOR %%v IN (ECHO GOTO:end) DO %%v parameter required 17. Suppress screen messages, including error messages. :: test.bat @ECHO off CTTY nul ECHO this is a test DIR/AD \..\ CTTY con 18. Selectively display messages when screen output is suppressed. :: test.bat @ECHO off CTTY nul ECHO This message will not be displayed ECHO This message will be displayed >con CTTY con 19. Use the pipe to place seperate commands on one line. Commands can sometimes be combined on one line. See also number 19. SET |FIND.EXE "windir" |IF errorlevel=1 ECHO Windows not running :: Here are six commands on one line: D:|CD\|DIR/A-D/B|FIND.EXE "TEMP.TMP"|IF errorlevel=1 C: 20. Delete all files in a directory, regardless of attributes. As in number 18, this example uses the pipe to place seperate commands on one line. Be careful with this one, don't do anything rash. ATTRIB.EXE -R -A -S -H DirName\*.* |ECHO Y |DEL DirName\*.* >nul 21. Multiple pipes, and an ansi trick. Ansi.sys required; Esc represents the escape character, created in edit by 'Ctrl+P Esc' :: howmany.bat @ECHO off :: displays howmany files and bytes in current and sub-directories DIR/A-D/W/S |FIND "file(s)" |SORT/R |FIND/N "file(s)" |FIND "[1]" IF not errorlevel=1 ECHO Esc[1ATotal: 22. Place comments on a command line. ATTRIB.EXE, %1 %removes file attributes - broken in MS-DOS 7.x% 23. Echo pipes and redirection characters to the screen or to another file. ECHO @PROMPT a+b $g c+d$_ > %temp%.\spchar1.bat COMMAND/E:2048/C %temp%.\spchar1.bat |FIND "+" >%temp%.\results.txt DEL %temp%.\spchar1.bat 24. Echo pipes and redirection characters with a single command. This command is taken from an actual batch file. COMMAND/E:2048/CFOR %%v IN (1 2) DO PROMPT ECHO %%3 $B FIND "-" $Gnul$_ |FIND /V "PROMPT" >>%bnam%.bat 25. Limit DIR output to only the file or directory you specify. In cases where you are looking for an extensionless file or directory, only the name you specify will be in the DIR output, even though there may exist one or more files with the same basename and an extension. It can be used on all files because a dot following a file with an extension is ignored. DIR/A-D FileName. DIR/AD DirName. 26. Delete a file with copy COPY nul filename 27. Xcopy one file to another without being asked if it's a file or directory. ECHO f |XCOPY.EXE file1 file2
This handbook explains the basic functions of Ms Dos . Click here for some samples scripts .
www.freescripts.4t.com