This page is edited from the 1cmdfaq.txt faq-file contained in
my tscmd.zip
command line interface (CLI) collection. That zipped file has much
additional material, including a number of detached .cmd script
files. It is recommended that you also get the zipped version as a
companion.
Please see "
The Description and
the Index page" for the conditions of usage and other such
information.
180} I want to exit my script if it already has been run today.
Although not quite the same, this has some resemblance to avoiding
multiple calls of a script with a lockfile as explained in
Item #12.
@echo off & setlocal enableextensions
rem The log file. Employ the current script file's name, but use .log as the extension.
set MyLogFile="%mytemp%\%~n0.log"
rem Get the last entry (the last line) of the log file.
set LastLine=
if exist "%MyLogFile%" (
for /f "delims=" %%a in ('type "%MyLogFile%"') do set LastLine=%%a
)
rem Compare the last line with today's date.
echo.%LastLine%|findstr /c:"%date%">nul
if %errorlevel% EQU 0 (
echo Exiting: "%~f0" has already been run today
echo %LastLine%
goto :EOF)
::
rem Whatever your script is otherwise designed to do.
echo Do whatever
::
rem Write into the log file the time of running this script.
echo %date% %time%>>"%MyLogFile%"
endlocal & goto :EOF
(If you have not set the
mytemp
environment variable, the root is indicated. For more study e.g.
the logic of AssignTemp in
Item #18.)