Batch Programing

Embed Size (px)

Citation preview

  • 8/8/2019 Batch Programing

    1/8

    Batfile Tricks & Tips

    Batfiles: The DOS batch file programming handbook & tutorial 1997-2003 by Laurence Soucy

    1. Alternative ways to get directory listing.2. One or more . (dots) can represent a directory.

    3. Directory listing of extentionless files.

    4. Add or remove file extensions (except system or hidden files).5. Rename multiple files (except system or hidden files).

    6. Use %temp%.\ (case is irrelevant) to represent the temp variable.

    7. Use ..\ as the last directory in your Path statement.

    8. Change Drive and Directory easily.9. Suppress a command's output.

    10. Suppress a batch file's output.

    11. Temporarily increase environment while running a batch file.12. A timed delay (wait/sleep) which can not be interfered with from the keyboard.

    13. Timed delay w/o pipe.

    14. Timed delay with Ctrl+C and Ctrl+Break disabled.15. Produce a constant alarm (beeping).

    16. Break out of an endless loop.

    17. Create a 0 byte file.18. Exit a batch at a designated place.

    19. Exit a batch w/o error message using a 0 byte file.20. Exit a batch file w/o error message or orphaned file.

    21. Use FOR to process GOTO's.22. Suppress screen messages, including error messages.

    23. Selectively display messages when screen output is suppressed.

    24. Delete all non-system, hidden or read-only files without confirmation25. Use the pipe to place separate commands on one line.

    26. Del all files in a directory, regardless of attributes.

    27. Multiple pipes, and an ansi trick.28. Place comments on a command line.

    29. Echo pipes and redirection characters to the screen or to another file.

    30. Echo pipes and redirection characters with a single command.31. Limit DIR output to only the file or directory you specify.32. Delete a file with copy.

    33. Xcopy one file to another without being asked if it's a file or directory.

    34. Xcopy a file to another directory w/o pipe.35. Xcopy a file to another directory w/o pipe.

    36. Use mode.com to truncate a string.

    37. Determine if a file is a 0 byte file without deleting it.

    http://home7.inet.tele.dk/batfiles/main/tricktip.htm#1%231http://home7.inet.tele.dk/batfiles/main/tricktip.htm#1%231http://home7.inet.tele.dk/batfiles/main/tricktip.htm#2%232http://home7.inet.tele.dk/batfiles/main/tricktip.htm#2%232http://home7.inet.tele.dk/batfiles/main/tricktip.htm#2%232http://home7.inet.tele.dk/batfiles/main/tricktip.htm#2%232http://home7.inet.tele.dk/batfiles/main/tricktip.htm#3%233http://home7.inet.tele.dk/batfiles/main/tricktip.htm#3%233http://home7.inet.tele.dk/batfiles/main/tricktip.htm#4%234http://home7.inet.tele.dk/batfiles/main/tricktip.htm#4%234http://home7.inet.tele.dk/batfiles/main/tricktip.htm#5%235http://home7.inet.tele.dk/batfiles/main/tricktip.htm#5%235http://home7.inet.tele.dk/batfiles/main/tricktip.htm#6%236http://home7.inet.tele.dk/batfiles/main/tricktip.htm#6%236http://home7.inet.tele.dk/batfiles/main/tricktip.htm#7%237http://home7.inet.tele.dk/batfiles/main/tricktip.htm#7%237http://home7.inet.tele.dk/batfiles/main/tricktip.htm#7%237http://home7.inet.tele.dk/batfiles/main/tricktip.htm#7%237http://home7.inet.tele.dk/batfiles/main/tricktip.htm#8%238http://home7.inet.tele.dk/batfiles/main/tricktip.htm#8%238http://home7.inet.tele.dk/batfiles/main/tricktip.htm#9%239http://home7.inet.tele.dk/batfiles/main/tricktip.htm#9%239http://home7.inet.tele.dk/batfiles/main/tricktip.htm#10%2310http://home7.inet.tele.dk/batfiles/main/tricktip.htm#10%2310http://home7.inet.tele.dk/batfiles/main/tricktip.htm#11%2311http://home7.inet.tele.dk/batfiles/main/tricktip.htm#11%2311http://home7.inet.tele.dk/batfiles/main/tricktip.htm#12%2312http://home7.inet.tele.dk/batfiles/main/tricktip.htm#12%2312http://home7.inet.tele.dk/batfiles/main/tricktip.htm#13%2313http://home7.inet.tele.dk/batfiles/main/tricktip.htm#13%2313http://home7.inet.tele.dk/batfiles/main/tricktip.htm#14%2314http://home7.inet.tele.dk/batfiles/main/tricktip.htm#14%2314http://home7.inet.tele.dk/batfiles/main/tricktip.htm#15%2315http://home7.inet.tele.dk/batfiles/main/tricktip.htm#15%2315http://home7.inet.tele.dk/batfiles/main/tricktip.htm#16%2316http://home7.inet.tele.dk/batfiles/main/tricktip.htm#16%2316http://home7.inet.tele.dk/batfiles/main/tricktip.htm#17%2317http://home7.inet.tele.dk/batfiles/main/tricktip.htm#17%2317http://home7.inet.tele.dk/batfiles/main/tricktip.htm#18%2318http://home7.inet.tele.dk/batfiles/main/tricktip.htm#18%2318http://home7.inet.tele.dk/batfiles/main/tricktip.htm#19%2319http://home7.inet.tele.dk/batfiles/main/tricktip.htm#19%2319http://home7.inet.tele.dk/batfiles/main/tricktip.htm#20%2320http://home7.inet.tele.dk/batfiles/main/tricktip.htm#20%2320http://home7.inet.tele.dk/batfiles/main/tricktip.htm#21%2321http://home7.inet.tele.dk/batfiles/main/tricktip.htm#21%2321http://home7.inet.tele.dk/batfiles/main/tricktip.htm#22%2322http://home7.inet.tele.dk/batfiles/main/tricktip.htm#22%2322http://home7.inet.tele.dk/batfiles/main/tricktip.htm#23%2323http://home7.inet.tele.dk/batfiles/main/tricktip.htm#23%2323http://home7.inet.tele.dk/batfiles/main/tricktip.htm#24%2324http://home7.inet.tele.dk/batfiles/main/tricktip.htm#25%2325http://home7.inet.tele.dk/batfiles/main/tricktip.htm#25%2325http://home7.inet.tele.dk/batfiles/main/tricktip.htm#26%2326http://home7.inet.tele.dk/batfiles/main/tricktip.htm#26%2326http://home7.inet.tele.dk/batfiles/main/tricktip.htm#27%2327http://home7.inet.tele.dk/batfiles/main/tricktip.htm#27%2327http://home7.inet.tele.dk/batfiles/main/tricktip.htm#28%2328http://home7.inet.tele.dk/batfiles/main/tricktip.htm#28%2328http://home7.inet.tele.dk/batfiles/main/tricktip.htm#29%2329http://home7.inet.tele.dk/batfiles/main/tricktip.htm#29%2329http://home7.inet.tele.dk/batfiles/main/tricktip.htm#30%2330http://home7.inet.tele.dk/batfiles/main/tricktip.htm#30%2330http://home7.inet.tele.dk/batfiles/main/tricktip.htm#31%2331http://home7.inet.tele.dk/batfiles/main/tricktip.htm#31%2331http://home7.inet.tele.dk/batfiles/main/tricktip.htm#32%2332http://home7.inet.tele.dk/batfiles/main/tricktip.htm#32%2332http://home7.inet.tele.dk/batfiles/main/tricktip.htm#33%2333http://home7.inet.tele.dk/batfiles/main/tricktip.htm#33%2333http://home7.inet.tele.dk/batfiles/main/tricktip.htm#34%2334http://home7.inet.tele.dk/batfiles/main/tricktip.htm#34%2334http://home7.inet.tele.dk/batfiles/main/tricktip.htm#35%2335http://home7.inet.tele.dk/batfiles/main/tricktip.htm#35%2335http://home7.inet.tele.dk/batfiles/main/tricktip.htm#36%2336http://home7.inet.tele.dk/batfiles/main/tricktip.htm#36%2336http://home7.inet.tele.dk/batfiles/main/tricktip.htm#37%2337http://home7.inet.tele.dk/batfiles/main/tricktip.htm#37%2337http://home7.inet.tele.dk/batfiles/main/tricktip.htm#2%232http://home7.inet.tele.dk/batfiles/main/tricktip.htm#3%233http://home7.inet.tele.dk/batfiles/main/tricktip.htm#4%234http://home7.inet.tele.dk/batfiles/main/tricktip.htm#5%235http://home7.inet.tele.dk/batfiles/main/tricktip.htm#6%236http://home7.inet.tele.dk/batfiles/main/tricktip.htm#7%237http://home7.inet.tele.dk/batfiles/main/tricktip.htm#8%238http://home7.inet.tele.dk/batfiles/main/tricktip.htm#9%239http://home7.inet.tele.dk/batfiles/main/tricktip.htm#10%2310http://home7.inet.tele.dk/batfiles/main/tricktip.htm#11%2311http://home7.inet.tele.dk/batfiles/main/tricktip.htm#12%2312http://home7.inet.tele.dk/batfiles/main/tricktip.htm#13%2313http://home7.inet.tele.dk/batfiles/main/tricktip.htm#14%2314http://home7.inet.tele.dk/batfiles/main/tricktip.htm#15%2315http://home7.inet.tele.dk/batfiles/main/tricktip.htm#16%2316http://home7.inet.tele.dk/batfiles/main/tricktip.htm#17%2317http://home7.inet.tele.dk/batfiles/main/tricktip.htm#18%2318http://home7.inet.tele.dk/batfiles/main/tricktip.htm#19%2319http://home7.inet.tele.dk/batfiles/main/tricktip.htm#20%2320http://home7.inet.tele.dk/batfiles/main/tricktip.htm#21%2321http://home7.inet.tele.dk/batfiles/main/tricktip.htm#22%2322http://home7.inet.tele.dk/batfiles/main/tricktip.htm#23%2323http://home7.inet.tele.dk/batfiles/main/tricktip.htm#24%2324http://home7.inet.tele.dk/batfiles/main/tricktip.htm#25%2325http://home7.inet.tele.dk/batfiles/main/tricktip.htm#26%2326http://home7.inet.tele.dk/batfiles/main/tricktip.htm#27%2327http://home7.inet.tele.dk/batfiles/main/tricktip.htm#28%2328http://home7.inet.tele.dk/batfiles/main/tricktip.htm#29%2329http://home7.inet.tele.dk/batfiles/main/tricktip.htm#30%2330http://home7.inet.tele.dk/batfiles/main/tricktip.htm#31%2331http://home7.inet.tele.dk/batfiles/main/tricktip.htm#32%2332http://home7.inet.tele.dk/batfiles/main/tricktip.htm#33%2333http://home7.inet.tele.dk/batfiles/main/tricktip.htm#34%2334http://home7.inet.tele.dk/batfiles/main/tricktip.htm#35%2335http://home7.inet.tele.dk/batfiles/main/tricktip.htm#36%2336http://home7.inet.tele.dk/batfiles/main/tricktip.htm#37%2337http://home7.inet.tele.dk/batfiles/main/tricktip.htm#1%231
  • 8/8/2019 Batch Programing

    2/8

    38. Place a line without a CR/LF (Carriage Return/Line Feed) into a file.

    39. Get input from file into variable.

    40. Timestamp and/or datestamp (touch) a file with the current system date and time.41. Parse a string with the / (slash) character.

    42. Remove a leading / (slash) character from a string.

    43. A pause which requires Ctrl+Break instead ofany key.44. Display number of files in directory, bytes used and bytes free.

    45. Edit the %path% at the prompt.

    46. Get current directory into a variable.47. Get current date into a variable.

    48. Get latest (last) file into a variable.

    49. Get oldest (1st) file into a variable.

    50. Keep a connection alive.

    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 tomove 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 orc:\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 thedirectory name has no extension!).

    http://home7.inet.tele.dk/batfiles/main/tricktip.htm#38%2338http://home7.inet.tele.dk/batfiles/main/tricktip.htm#38%2338http://home7.inet.tele.dk/batfiles/main/tricktip.htm#39%2339http://home7.inet.tele.dk/batfiles/main/tricktip.htm#39%2339http://home7.inet.tele.dk/batfiles/main/tricktip.htm#40%2340http://home7.inet.tele.dk/batfiles/main/tricktip.htm#40%2340http://home7.inet.tele.dk/batfiles/main/tricktip.htm#41%2341http://home7.inet.tele.dk/batfiles/main/tricktip.htm#41%2341http://home7.inet.tele.dk/batfiles/main/tricktip.htm#42%2342http://home7.inet.tele.dk/batfiles/main/tricktip.htm#42%2342http://home7.inet.tele.dk/batfiles/main/tricktip.htm#43%2343http://home7.inet.tele.dk/batfiles/main/tricktip.htm#43%2343http://home7.inet.tele.dk/batfiles/main/tricktip.htm#43%2343http://home7.inet.tele.dk/batfiles/main/tricktip.htm#43%2343http://home7.inet.tele.dk/batfiles/main/tricktip.htm#44%2344http://home7.inet.tele.dk/batfiles/main/tricktip.htm#44%2344http://home7.inet.tele.dk/batfiles/main/tricktip.htm#45%2345http://home7.inet.tele.dk/batfiles/main/tricktip.htm#45%2345http://home7.inet.tele.dk/batfiles/main/tricktip.htm#46%2346http://home7.inet.tele.dk/batfiles/main/tricktip.htm#46%2346http://home7.inet.tele.dk/batfiles/main/tricktip.htm#47%2347http://home7.inet.tele.dk/batfiles/main/tricktip.htm#47%2347http://home7.inet.tele.dk/batfiles/main/tricktip.htm#48%2348http://home7.inet.tele.dk/batfiles/main/tricktip.htm#48%2348http://home7.inet.tele.dk/batfiles/main/tricktip.htm#49%2349http://home7.inet.tele.dk/batfiles/main/tricktip.htm#49%2349http://home7.inet.tele.dk/batfiles/main/tricktip.htm#50%2350http://home7.inet.tele.dk/batfiles/main/tricktip.htm#50%2350http://home7.inet.tele.dk/batfiles/main/tricktip.htm#38%2338http://home7.inet.tele.dk/batfiles/main/tricktip.htm#39%2339http://home7.inet.tele.dk/batfiles/main/tricktip.htm#40%2340http://home7.inet.tele.dk/batfiles/main/tricktip.htm#41%2341http://home7.inet.tele.dk/batfiles/main/tricktip.htm#42%2342http://home7.inet.tele.dk/batfiles/main/tricktip.htm#43%2343http://home7.inet.tele.dk/batfiles/main/tricktip.htm#44%2344http://home7.inet.tele.dk/batfiles/main/tricktip.htm#45%2345http://home7.inet.tele.dk/batfiles/main/tricktip.htm#46%2346http://home7.inet.tele.dk/batfiles/main/tricktip.htm#47%2347http://home7.inet.tele.dk/batfiles/main/tricktip.htm#48%2348http://home7.inet.tele.dk/batfiles/main/tricktip.htm#49%2349http://home7.inet.tele.dk/batfiles/main/tricktip.htm#50%2350
  • 8/8/2019 Batch Programing

    3/8

    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 withstartling 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 byadding CD in front and repeating it without the backslash so we have, D:\AnyDir\

    followed by CD D:\AnyDirHere'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. Timed delay w/o pipe.CHOICE.COM/N/CY/TY,5nulCHOICE.COM/N/CY/TY,5nul

    14. Timed delay with Ctrl+C and Ctrl+Break disabled.@ECHO off

    CTTY nulTYPE nul |CHOICE.COM /N /CY /TY,5CTTY con

    15. Produce a constant alarm (beeping).CHOICE.COM /N < nul

    Continues until Ctrl+C orCtrl+Break is used.

  • 8/8/2019 Batch Programing

    4/8

    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.:startSIREN.COM >nulCHOICE.COM /N /CY /TY >nulGOTO start

    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%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 FORto process GOTO's.

    FOR does not see the colon as a delimiter, but GOTO does. Therefore, they can be usedtogether.IF "%1"=="" FOR %%v IN (ECHO GOTO:end) DO %%v parameter required

    22. Suppress screen messages, including error messages.:: test.bat@ECHO offCTTY nulECHO this is a testDIR/AD \..\CTTY con

    23. Selectively display messages when screen output is suppressed.:: test2.bat@ECHO offCTTY nulECHO This message will not be displayedECHO This message will be displayed >conCTTY con

  • 8/8/2019 Batch Programing

    5/8

    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 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'::howmany.bat@ECHO off:: display how many files and bytes in current and sub-directoriesDIR/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.batCOMMAND/E:2048/C %temp%.\spchar1.bat |FIND "+" >%temp%.\results.txtDEL %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 youspecify will be in the DIR output, even though there may exist one or more files with the

    same basename andan extension. It can be used on all files 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 copyCOPY nul filename

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

  • 8/8/2019 Batch Programing

    6/8

    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.MODE name.ext:: returns: Invalid parameter - nameMODE think/:: returns: Invalid parameter - thin

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

    :: is0byte2.batCOPY %1 nul |FIND " 0 file" >nulIF not errorlevel=1 ECHO %1 is a 0 byte fileIF 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 nothave a CR/LF. See FAQ 57.:: setvar.inp@ECHO off:: EOFcharacter created in edit by Ctrl+P+ZSET %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 offCD > result1.dat ECHO. >> result1.datDATE < result1.dat | find "):" > result1.batECHO set curdir=%%4> enter.batFOR %%C IN (CALL DEL) DO %%C result1.batDEL enter.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.ECHO>01/31/2000

    results in a file named 01 which contains:

    http://home7.inet.tele.dk/batfiles/main/batfaq2.htm#57http://home7.inet.tele.dk/batfiles/main/batfaq2.htm#57
  • 8/8/2019 Batch Programing

    7/8

    /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 ofany key.

    AfterCtrl+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 of Laura Fairhead, http://lf.8k.com/.:: edpath.bat@ECHO offPATH>%temp%.\edpath1.batCALL%temp%.\setdir.bat@% do not delete this line %@ECHO offPROMPT %promp$%FOR %%c IN (CALL DEL) DO %%c %temp%.\setdir.batECHO. current directory=%cd%

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

  • 8/8/2019 Batch Programing

    8/8

    DEL %temp%.\%%gdate?%%.batECHO. 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 for parameter 1.SET lastfil=IF not "%1"=="" FOR %%f in (%1) do set lastfil=%%fIF "%1"=="" FOR %%f in (*.*) do set lastfil=%%fIF 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 recursSET fspec=%1IF not "%1"=="" FOR %%f in (%1) do %0 %%f ReCuRsIF "%1"=="" FOR %%f in (*.*) do %0 %%f ReCuRs:recursSET 1stfile=%1IF 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, itacts 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