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 Pathanyfile .
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 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 (provided the directory name has no extension!).
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 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:
:: 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)
9. Suppress a command’s output.
Works with most commands.
COPY file1 file2 >nul
10. Suppress a batch file’s output.
11. Temporarily increase environment while running a batch file.
Specify a maximum of 32000 bytes with the /E switch.
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. Timed delay w/o pipe.
14. Timed delay with Ctrl+C and Ctrl+Break disabled.
TYPE nul |CHOICE.COM /N /CY /TY,5
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.
CHOICE.COM /N /CY /TY >nul
17. Create a 0 byte file.
TYPE nul >filename
18. Exit a batch at a designated 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
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, Laura’s Home Page.
CTTY NUL |GOTO |CTTY CON |CALL ECHO ON
21. 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
22. Suppress screen messages, including error messages.
ECHO this is a test
23. Selectively display messages when screen output is suppressed.
ECHO This message will not be displayed
ECHO This message will be displayed >con
24. Delete all non-system, hidden or read-only files without confirmation.
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 Windows not 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’
:: 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 “”
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
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 all files because a dot following a file with an extension is ignored.
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.
XCOPY c: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.
:: returns: Invalid parameter – name
:: returns: Invalid parameter – thin
37. Determine if a file is a 0 byte file without deleting it.
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
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 0 byte 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.
:: EOF character created in edit by Ctrl+P+Z
39. Get input from file into variable.
Where result1.dat contains command output or other text. This example places the current directory into a variable.
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
:: %optional% DEL result1.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.
results in a file named 01 which contains:
ECHO set var=>01/31/2000
results in a file named 01 which contains:
42. Remove a leading / (slash) character from a string.
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”
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 of Laura Fairhead, Laura’s Home Page.
46. Get current directory into a variable.
This trick courtesy of Laura Fairhead, Laura’s Home Page.
@PROMPT SET cd$Q$P
% do not delete this line %
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 SET date=%%3>%temp%.%%gdate1%%.bat
DIR/A-D/-W/L/-P %temp% | FIND “%%gdate1%%” >%temp%.%%gdate2%%.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.
:: 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 for parameter 1.
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.
:: 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
IF not “%1″==”” FOR %%f in (%1) do %0 %%f ReCuRs
IF “%1″==”” FOR %%f in (*.*) do %0 %%f ReCuRs
IF not “%fspec%”==”” ECHO. 1st %fspec% file in directory is %1
IF “%fspec%”==”” ECHO. 1st file in directory is %1
50. Keep a connection 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. —
Sumber : ccpb@kaskus