Rob van der Woude's Scripting Pages

Use Date and Time

to prefix strings

There's one neat trick that involves the DATE or TIME command.
Sometimes, when redirecting standard output to temporary batch files, you need to process the first word in the redirected line.
The trick I have shown in several examples - using the first word as the name of another temporary batch file - will not work here, since we do not know that first word.
Now, as long as we know for sure that this first word cannot and will not be interpreted as a valid date or time string, we can redirect the string to DATE or TIME. Use FIND to filter out every line but the one containing the string "Enter the new date:".
Both DATE and TIME will display the string that was redirected to them, preceded by "Enter the new date:".
Do test this one thoroughly, though. Whether it will work or not depends on both the redirected strings and the DOS version/operating system.

Note: Though this technique will work in Windows NT 4 and later (CMD.EXE), I recommmend using FOR /F instead.
This prefix technique is a workaround at best for functionality missing in COMMAND.COM.

The old version 1.03 of KillNTPr.bat is an sample batch file I created using this trick. Use it to kill any process by its executable file name (the recent version 2.10 doesn't use temporary files, it uses FOR /F instead).
KillNTPr.bat uses TLIST.EXE and KILL.EXE from the Windows NT 4 Resource Kit. By modifying the batch file you could use SysInternals' PSLIST.EXE and PSKILL.EXE, both from their PsTools set. Windows XP users might prefer to use XP's native TASKLIST.EXE and TASKKILL.EXE commands.

Be careful with this command!

Modified.bat is another example of this technique. This one works in any DOS like environment (including OS/2 after replacing .BAT with .CMD everywhere in the batch file):

@ECHO OFF
REM * MODIFIED.BAT, Version 1.01
REM * Check if specified file was created or modified today
REM * Written by Rob van der Woude
REM * http://www.robvanderwoude.com

REM * Needs write access to current directory; or add fully qualified
REM * path to files CURRENT.BAT and its language dependent versions,
REM * and make sure its path is specified in the PATH variable.

REM * File name should be specified
IF "%1"=="" GOTO Syntax
IF NOT EXIST %1 GOTO Syntax

REM * Send DIR output for specified file to primary temporary
REM * batch file to get the file's creation or modification date
DIR %1 | FIND /I "%1" > %TEMP%.\~ISMODIF.TMP
ECHO.>> %TEMP%.\~ISMODIF.TMP
TYPE %TEMP%.\~ISMODIF.TMP | TIME | FIND /I "%1" > %TEMP%.\~ISMODIF.BAT
REM * Create secondary temporary batch files to be called by primary
ECHO SET CHKDATE=%%4>ENTER.BAT
REM * For Dutch DOS versions
ECHO SET CHKDATE=%%5>VOER.BAT
ECHO SET CHKDATE=%%6>TYP.BAT
CALL %TEMP%.\~ISMODIF.BAT

REM * Send DIR output for temporary batch file to itself to get today's date
DIR %TEMP%.\~ISMODIF.BAT | FIND /I "~ISMODIF.BAT" > %TEMP%.\~ISMODIF.TMP
ECHO.>> %TEMP%.\~ISMODIF.TMP
TYPE  %TEMP%.\~ISMODIF.TMP | TIME | FIND /I "~ISMODIF.BAT" > %TEMP%.\~ISMODIF.BAT
REM * Create secondary temporary batch files to be called by primary
ECHO SET NOWDATE=%%4>ENTER.BAT
REM * For Dutch DOS versions
ECHO SET NOWDATE=%%5>VOER.BAT
ECHO SET NOWDATE=%%6>TYP.BAT
CALL %TEMP%.\~ISMODIF.BAT

REM * Compare dates and display result
IF "%NOWDATE%"=="%CHKDATE%" ECHO %1 was created or modified today (%NOWDATE%)

REM * Clean up the mess
DEL %TEMP%.\~ISMODIF.BAT
DEL %TEMP%.\~ISMODIF.TMP
DEL ENTER.BAT
DEL VOER.BAT
DEL TYP.BAT
SET CHKDATE=
SET NOWDATE=
GOTO End

:Syntax
ECHO MODIFIED,  Version 1.01
ECHO Check if specified file was created or modified today
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com
ECHO.
ECHO.
ECHO Usage:  %0  filename

:End
 
Click to view source Click to view source Click to download ZIPped sources
DOS & Windows
(including NT)
OS/2 & NT ZIPped
 

See the Redirection page for a more detailed explanation of redirection to temporary batch files.


page last modified: 2016-09-19; loaded in 0.0052 seconds