Link to home
Start Free TrialLog in
Avatar of smandell
smandell

asked on

Once a day batch file

Does anyone have a batch file or template for one that will only run the script once per day regardless of how many times the batch is called? I've read the way most users do this is by creating another file and then checking the date on the file to see if the script should run again. That seems like a good way to me I just don't know enough scripting to do it myself.

Any help would be appreciated.
Thanks,
Steve
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

echo off
set file=.\%date:~4,2%-%date:~7,2%-%date:~10,4%.txt
if exist %file% goto quit
echo n1>%date:~4,2%-%date:~7,2%-%date:~10,4%.txt

REM code goes HERE.
:quit
echo File already launched today.
pause
cleaned up batch:

echo off
set file=.\%date:~4,2%-%date:~7,2%-%date:~10,4%.txt
if exist %file% goto quit
echo n1>%file%

REM code goes HERE.
:quit
echo File already launched today.
set file=
pause
Avatar of AmazingTech
AmazingTech

Give this a try.
for /f %a in ('dir /b datefile.txt') do echo %~ta | find "%date:~-10%"
if not errorlevel 1 GOTO :EOF
 
{insert your code here}
{insert your code here}
{insert your code here}
 
::Update the modified date/time of the datefile.txt
echo Batch file ran>>datefile.txt

Open in new window

Without writing too much code to check file dates and comparing them to something dynamic, what you could do is have your batch file create a "flag" file somewhere on your disk. Then when the batch file runs, have it check for the existence of the flag file and only continue if it does not exist.

For example:
if not exist c:\flag.txt...

The last part of your batch file should be to then create that file so that if the batch is run again, it will just quit.

Then create a scheduled task that runs each night at midnight that deletes the file if it exists.
Avatar of smandell

ASKER

I started testing ged's code since it was the first to come in and it seems to be working well. What's the difference between ged's and AmazingTech's solution?

Do either of those scripts allow me to adjust a parameter in the code to specify how many days must go by before the script will execute. I realize that wasn't an original requirement but now i'm thinking of a few other uses.

I don't think dhoffman's suggestion will work for me because the script will be running on remote laptops with no guarantees as to when they will be on and available to run the scheduled task.

Thanks.
ged is creating a marker file with todays date. Tomorrow there will be yesterday's and current date. Date files will grow and grow. Probably should put in a clean up routine.

Mine at the end of the script it updates the 1 file datefile.txt after your code runs. When the batch runs again it checks the datefile.txt last modified date for today's date. Probably should change my last line to

echo Batch file ran %date%>datefile.txt

so the size doesn't grow.
Days gone by can be put in too but you'll need additional code and let me know your date format.

echo %date%
I see, good idea.
My date format: Thu 02/12/2009
Thanks!
SOLUTION
Avatar of AmazingTech
AmazingTech

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Apologies for coming in on this one so late however, I couldn't help mentioning my own preferre method which does use the creation of a file.

Personally, I would prefer to make proper use of Windows' registry file. For example, name the following code RUNSCRIPT.BAT

Next, create a (string value) entry in the RUN section of the registry file such as:

   HKLM\Software\Microsoft\Windows\CurrentvVersion\Run\MyApplication

and set it's value pointing to the full path of this batch file such as:

   "C:\Batch\RunScript.bat"

Now everytime the Windows is re-started this batch file will run.

This abtch file will only run your script file once a day whether you run this batch file from the command line, from within another script or from the registry file itself.

This batch file uses a key in the application data section of the registry file to determine whther or not your application has run during the current day.



@echo off
set MyAppName=MyApplication
 
reg query hkcu\software\%myapp% /v LastRun > NUL 2>&1
if not %ERRORLEVEL%==0 reg add hkcu\software\%MyAppName% /v LastRun /t reg_expand_sz /d 00/00/0000 > NUL
 
for /f "skip=4 tokens=3" %%r in ('reg query hkcu\software\%MyAppName% /v LastRun') do set lastrun=%%r
 
if not "%lastrun%"=="%date%" (
   reg add hkcu\software\%MyAppName% /v LastRun /t reg_expand_sz /d %date% /f > NUL
   echo start script
)

Open in new window

Oh, I forgot to mention...

To use the batch file... changes:

   set MyAppName=MyApplication

to the name of your application and edit,,

   echo start script

to whatever is required to run you script



echo off
set file=.\%date:~4,2%-%date:~7,2%-%date:~10,4%.txt
if exist %file% goto quit
REM Delete old text files per suggestions
del *.txt
echo n1>%file%

Also t0t0 . . . depending on the system, there is a chance that the registry will be locked down.

REM code goes HERE.
:quit
echo File already launched today.
set file=
pause
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I do like some of the other examples shown on here, but I'll add a comment to the author regarding not being sure when the machines are online.

Scheduled tasks CAN be configured so that they execute the next time the machine is on if the schedule was missed. So if it was to execute at midnight, but the machine was off, then next time the machine is turned on, it will still execute.
I am fully aware that this is the DOS topic area, but I would tend to agree with dhoffman on this.  Seeing as you only want the batch file to be run once a day, do you really need the batch file to test if it has already been run?  I suppose it all depends on what programs, processes, or user activities may make an unwanted call to run the batch file at any time.  Only you will know the likelihood of that possibility.

For what it's worth, I use a small program to do my "Once a Day" tasks.  It may do just what you need:
http://www.karenware.com/powertools/ptoad.asp

If not, then you have enough batch scripts here for any occasion, ranging from the simple and effective "flag file", to the startup program to check registry values.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Wow, this is the best response I've ever gotten on Experts-Exchange. Thanks!

I think I have enough scripts and suggestions here to last for a while. Just to clarify, for those suggesting alternatives to scripts like registry or scheduled tasks the reason I need a script is because I want it to run the first time a user logs onto the VPN remotely each day. The script will actually be on a remote server so I have one file to update for all vpn users. It's always been an issue for me that only users that come into the office run login scripts, this has all been about applying that to remote user as well.

I'm just going to test a few of these out then close the question.

Thanks again.
This technique uses ADS....

@echo off

(for /f %%a in (%~nx0:LastRun) do set LastRun=%%a) 2>>nul
if "%LastRun%"=="" set LastRun=00/00/0000
if "%LastRun%"=="%date%" (exit /b) else (echo %date%>%~nx0:LastRun)

rem =========== Continue to process rest of batch file ===========

echo Running...
Just to add a final variation to my code above and my earlier code using the registry file. This example is more down to earth and one which you will find easier to understand. It basically saves the LastRun date to an external file and reads it in each time the batch file runs, comparing it to the current date. If the current date is different, then it upfates the date i the file. The principle is the same - just another method. Please see code below.

@echo off

if not exist lastrun.txt echo 00/00/0000 > lastrun.txt
(for /f %%a in (lastrun.txt) do set LastRun=%%a) 2>>nul
if "%LastRun%"=="%date%" (exit /b) else (echo %date%>lastrun.txt)

rem =========== Continue to process rest of batch file ===========

echo Processing....
smandell

Could you please look at the last few comments and decide if you are satisfied with the solutions given and close this question.

Thank you.
 
I would rather not be included in the points split because my comment did not actually answer the question as asked.  I merely posed the rhetorical question suggesting that the asker consider whether or not a batch file was really necessary for the task, and offered an alternative non-DOS suggestion.
Sorry for abandoning the question. I'll close it out. Amazing Tech and T0t0's responses were most helpful.
GED was actually the first to respond with a solution that worked for me but the others contributed useful scripts as well. Thanks all.
Thanks for that scathing attack Lee ;-)

"... most of the comments I've seen from you over these many years have been so lengthy and detailed that they could favorably be compared to a Reader's Digest version of Leo Tolstoy's War and Peace".

You know, that's one book I have never had the time to read.  I've always been too busy here .... typing!  :-)