Aug 17, 2008

Batch Programming Tricks --- 5

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

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

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

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

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 off
PATH>%temp%.\edpath1.bat
CALL<%temp%.\edpath1.bat

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%

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%

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=%%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%

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
::

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.

No comments: