DOS Batch file soluntions


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 fileswithout 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 batch file. 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 parentdirectory will always be in the Path.

8.   Change Drive and Directory easily.
There can be many reasons for changing drive and directory in a batch file, 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 batch file:

@ECHO off
:: cdd.bat (Change Drive and Directory)
:: Where %1 is a full path and directory without backslash.
:: Example usage: 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 (wait/sleep) which can not be interfered with from the keyboard.
:: Substitute 5 with the desired number of seconds, up to 99.
TYPE nul |CHOICE.COM /N /CY /TY,5 >nul

13. Timeddelay w/o pipe.
CHOICE.COM/N/CY/TY,5<CLOCK$>nul
CHOICE.COM/N/CY/TY,5<aux>nul

14. Timed delay with Ctrl+C and Ctrl+Break disabled.
@ECHO off
CTTY nul
TYPE nul |CHOICE.COM /N /CY /TY,5
CTTY con

15.  Produce a constant alarm (beeping).
CHOICE.COM /N < nul
Continues until Ctrl+C or Ctrl+Break is used.

16.  Break out of an endless loop.
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 with Ctrl+C or Ctrl+Break.
:start
SIREN.COM >nul
CHOICE.COM /N /CY /TY >nul
GOTO start

17.  Create a 0 byte file.
TYPE nul >filename

18.  Exit a batch at adesignated place.
This method is used for testing and outputs a "Label not found" message which can be useful as confirmation the batch has quit at the designated place and also as a reminder if you forget to remove the bogus GOTO. Makes no disk writes.
GOTO bogus (non-existent label)

19. Exit a batch w/o error message using a 0 byte file.
This method writes a temporary 0 byte file which remains on the drive.
TYPE nul>%temp%.\batexit.bat
%temp%.\batexit

20. Exit a batch file w/o error message or orphaned file.
This method does not leave a 0 byte file behind, but uses pipes so it writes to %temp% or current. This trick courtesy of Laura Fairhead, http://lf.8k.com/.
CTTY NUL |GOTO |CTTY CON |CALL ECHO ON

21.  Use FOR to process GOTO's.
FOR does not see the colon as a delimiter, butGOTO does. Therefore, they can be used together.
IF "%1"=="" FOR %%v IN (ECHO GOTO:end) DO %%v  parameter required

22.  Suppress screen messages, including error messages.
:: test.bat
@ECHO off
CTTY nul
ECHO   this is a test
DIR/AD \..\
CTTY con

23.  Selectively display messages when screen output is suppressed.
:: test2.bat
@ECHO off
CTTY nul
ECHO   This message will not be displayed
ECHO   This message will be displayed >con
CTTY con

24.  Delete all non-system, hidden or read-only files without confirmation.
DEL c:\dirname\*?.*

25.  Use the pipe to place separate commands on one line.
Commands can sometimes be combined on one line.
SET |FIND.EXE "windir" |IF errorlevel=1 ECHO  Windowsnot running
:: Here are six commands on one line:
D:|CD\|DIR/AD/-P TEMP|FIND "TEMP "|IF not errorlevel=1 CD TEMP

26.  Delete all files in a directory, regardless of attributes.
This example uses the pipe to place separate 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

27.  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
:: display how many files and bytes in current and sub-directories
DIR/A-D/W/S/-P |FIND "file(s)" |SORT/R |FIND/N "file(s)" |FIND "[1]"
IF not errorlevel=1 ECHO Esc[1ATotal:

28.  Place comments on a command line.
ATTRIB.EXE, %1 %removes file attributes- broken in MS-DOS 7.x%

29.  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

30.  Echo pipes and redirection characters with a single command.
Use %v at the prompt and %%v in a batch file.
COMMAND/E:2048/CFOR %v IN (1 2) DO PROMPT a+b $G c+d $_ |FIND/V "PROMPT">results.txt

31.  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 allfiles because a dot following a file with an extension is ignored.
DIR/A-D/-P FileName.
DIR/AD/-P DirName.

32.  Delete a file with copy
COPY nul filename

33.  Xcopy one file to another without being asked if it's a file or directory.
ECHO F |XCOPY.EXE file1 file2

34. Xcopy a file to another directory w/o pipe.
XCOPYc:\example1.fil w:\temp\

35. Xcopy a file to another directory w/o pipe.
XCOPY c:\example1.fil w:\temp\*example1.fil

36.  Use mode.com to truncate a string.
:: truncate one character, or up to and including a delimiter.
MODE name.ext
:: returns: Invalid parameter - name
MODE think/
:: returns: Invalid parameter - thin

37.  Determine if a file is a 0 bytefile without deleting it.
:: is0byte.bat
@ECHO off
COPY>nul %1 nul |FIND "1 file(s) copied"
IF not errorlevel=1 ECHO  %1 is not a 0 byte file
IF     errorlevel=1 ECHO  %1 is a 0 byte file

:: is0byte2.bat
COPY %1 nul |FIND " 0 file" >nul
IF not errorlevel=1 ECHO  %1 is a 0 byte file
IF     errorlevel=1 ECHO  %1 is not a 0byte file

38.  Place a line without a CR/LF (Carriage Return/Line Feed) into a file.
After creating this input file which has an End Of File character as its last character, you can TYPE the input file into a new batch file which will not have a CR/LF. See FAQ 57.
:: setvar.inp
@ECHO off
:: EOF character created in edit by Ctrl+P+Z
SET %1=EOF

39.  Get input from file into variable.  {last modified Oct. 19, 2002}
Where result1.dat contains command output or other text. This example places the current directory into a variable.
:: result.bat
@ECHO off
CD > result1.dat ECHO. >> result1.dat
DATE < result1.dat | find "):" > result1.bat
ECHO set curdir=%%4> enter.bat
FOR %%C IN (CALL DEL) DO %%C result1.bat
DEL enter.bat
:: %optional% DELresult1.dat
:: %optional% ECHO. current directory is %curdir%

40.  Timestamp and/or datestamp (touch) a file with the current system date and time.
COPY/Y filename /B+,,

41.  Parse a string with the / (slash) character.
ECHO>01/31/2000
results in a file named 01 which contains:
/31/2000

ECHO set var=>01/31/2000
results in a file named 01 which contains:
set var=/31/2000

42.  Remove a leading / (slash) character from a string.
ECHO/31/2000
results in:
31/2000

43.  A pause which requires Ctrl+Break instead of any key.
After Ctrl+Break, there is the usual choice to continue or abort.
COMMAND nul /CECHO ;|CHOICE/C; /N

44.  Display number of files in directory, bytes used and bytes free.
DIR/W c:\bat |find "y"
Includes subdirectories:
DIR/W/S c:\bat |find "y" |more

45.  Edit the %path% at the prompt.
After running edpath.bat, use [F3], or [Up Arrow] (if doskey is installed) to edit the path. With this method, a temporary batch will remain in the %temp% directory. This trick courtesy ofLaura Fairhead, http://lf.8k.com/.
:: edpath.bat
@ECHO off
PATH>%temp%.\edpath1.bat
CALL<%temp%.\edpath1.bat

46.  Get current directory into a variable.
This trick courtesy of Laura Fairhead, http://lf.8k.com/.
:: curdir.bat
@SET cd=
@SET promp$=%prompt%
@PROMPT SET cd$Q$P
@CALL>%temp%.\setdir.bat
@
% do not delete this line %
@ECHO off
PROMPT %promp$%
FOR %%c IN (CALL DEL) DO%%c %temp%.\setdir.bat
ECHO. current directory=%cd%

47.  Get current date into a variable.
:: gdate.bat (GetDATE)
@ECHO off
SET gdate1=
ECHO SET date=%%3>%temp%.\%%gdate1%%.bat
DIR/A-D/-W/L/-P %temp% | FIND "%%gdate1%%" >%temp%.\%%gdate2%%.bat
SET gdate1=%temp%.\%%gdate1%%
CALL %temp%.\%%gdate2%%.bat
SET gdate1=
DEL %temp%.\%%gdate?%%.bat
ECHO. todays date is %date%

48.  Get latest (last) file into a variable.
I received a couple of emails about lastfile.bat. Here is a clarification. "Last non hidden/system file" refers to the last file according to physical placement on disk which is not necessarily the last file written to disk.
:: lastfile.bat
@ECHO off
:: Get last non hidden/system file in current directory into
:: variable (last file on disk, but not by date or last modified).
:: Accepts wildcard specification forparameter 1.
SET lastfil=
IF not "%1"=="" FOR %%f in (%1) do set lastfil=%%f
IF     "%1"=="" FOR %%f in (*.*) do set lastfil=%%f
IF not "%1"=="" ECHO. last %1 file in directory is %lastfil%
IF     "%1"=="" ECHO. last file in directory is %lastfil%

49.  Get oldest (1st) file into a variable.
:: 1stfile.bat
@ECHO off
:: Get first non hidden/system file in current directory into
::variable (1st file on disk, but not by date).
:: Accepts wildcard specification for parameter 1.
IF "%2"=="ReCuRs" GOTO recurs
SET fspec=%1
IF not "%1"=="" FOR %%f in (%1) do %0 %%f ReCuRs
IF "%1"=="" FOR %%f in (*.*) do %0 %%f ReCuRs
 
:recurs
SET 1stfile=%1
IF not "%fspec%"=="" ECHO. 1st %fspec% file in directory is %1
IF     "%fspec%"=="" ECHO. 1st file in directory is %1
::

50.  Keep aconnection alive.
netstat -e 15
Although it cannot be used as a delay function because it must be broken by a CTRL+C, it is not processor intensive and does not send any packets on the network. However, it acts as a great way to hold a connection open, such as when someone uses a Terminal Service connection, rather than a ping or dir loop. This does a check on net card statistics every 15 seconds, and runs in a loop automatically. -- Tip submitted by Mike McKee