How to add yesterday's date to a filename if/when a job is run after midnight.

A

ajmister

Hi

I have a job that is dependant on the successful completion of another
script. Therefore the job could run before or after midnight.

If the job runs before midnight my adddate script address today's date
(YYYYMMDD) to the filename. I am having trouble with the adddate script when
the job runs after midnight. If the job runs after midnight I would like the
script to add yesterday's date to the filename but it is adding current days
date.

Does anyone have suggestions.

Ajmister.
 
F

foxidrive

I have a job that is dependant on the successful completion of another
script. Therefore the job could run before or after midnight.

If the job runs before midnight my adddate script address today's date
(YYYYMMDD) to the filename. I am having trouble with the adddate script when
the job runs after midnight. If the job runs after midnight I would like the
script to add yesterday's date to the filename but it is adding current days
date.

Does anyone have suggestions.


Date foward & backward

@echo off
:: from code by Phil Robyn
setlocal
if [%1]==[] (
echo to get todays date use
echo call "%~n0" today 0
echo.
echo to get yesterdays date use
echo call "%~n0" today -1
echo.
echo to get the date 25 days ago:
echo call "%~n0" today -25
echo.
echo to get the date 1250 days in the future
echo call "%~n0" today +1250
goto :EOF)

set date1=%1
set qty=%2
if /i "%date1%" EQU "TODAY" (
set date1=now
) else (
set date1="%date1%"
)
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%qty%,%date1%)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^&_
echo>>"%temp%\%~n0.vbs" right(100+month(s),2)^&_
echo>>"%temp%\%~n0.vbs" right(100+day(s),2)
for /f %%a in (
'cscript //nologo "%temp%\%~n0.vbs"') do set result=%%a
del "%temp%\%~n0.vbs"
endlocal& set day=%result:~0,4%-%result:~4,2%-%result:~6,2%
echo %%day%% is set to "%day%" (without the quotes)
 
B

billious

ajmister said:
Hi

I have a job that is dependant on the successful completion of another
script. Therefore the job could run before or after midnight.

If the job runs before midnight my adddate script address today's date
(YYYYMMDD) to the filename. I am having trouble with the adddate script
when the job runs after midnight. If the job runs after midnight I would
like the script to add yesterday's date to the filename but it is adding
current days date.

Does anyone have suggestions.

Ajmister.

Since you haven't shown us your adddate script, so adjusting it may be a
little difficult.

How about generating the filename that you require within the first script
which I assume is scheduled to start before midnight? Then pass the filename
or pre-midnight date to the second script, either as a parameter or via a
file?
 
T

Todd Vargo

ajmister said:
Hi

I have a job that is dependant on the successful completion of another
script. Therefore the job could run before or after midnight.

If the job runs before midnight my adddate script address today's date
(YYYYMMDD) to the filename. I am having trouble with the adddate script when
the job runs after midnight. If the job runs after midnight I would like the
script to add yesterday's date to the filename but it is adding current days
date.

Does anyone have suggestions.

Post your adddate script and tell us at what time you want to stop back
dating.
 
A

ajmister

Hi
Here is my add date script.

echo off
for /F "tokens=1,2,3" %%r in ('date /t') do (
for /f "tokens=1,2,3 delims=/" %%c in ("%%s") do set filedte=%%e%%c%%d
)
echo %filedte%

ren coverage.txt coverage.txt_%filedte%

If the script runs after midnight I need my script to pick previous day's
date instead of current day's date. My script runs anytime between 10 pm and
6 am the next day.

Thank you.

Aj
 
F

foxidrive

Here is my add date script.

echo off
for /F "tokens=1,2,3" %%r in ('date /t') do (
for /f "tokens=1,2,3 delims=/" %%c in ("%%s") do set filedte=%%e%%c%%d
)
echo %filedte%

ren coverage.txt coverage.txt_%filedte%

If the script runs after midnight I need my script to pick previous day's
date instead of current day's date. My script runs anytime between 10 pm and
6 am the next day.


To figure out which day it is use the time and see if it is before 10, and run
an appropriate routine.

Use %day% which will be populated with the result EG: 2007-11-28


@echo off
for /f "delims=:" %%a in ("%time%") do if %%a LSS 10 (
set qty=-1
call :getdate
) else (
set qty=0
call :getdate
)
echo %%day%% is set to "%day%"
goto :EOF
:getdate
set date1=now
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%qty%,%date1%)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^&_
echo>>"%temp%\%~n0.vbs" right(100+month(s),2)^&_
echo>>"%temp%\%~n0.vbs" right(100+day(s),2)
for /f %%a in (
'cscript //nologo "%temp%\%~n0.vbs"') do set result=%%a
del "%temp%\%~n0.vbs"
set day=%result:~0,4%-%result:~4,2%-%result:~6,2%



If you are unable to use WSH then here is a thread that could help you:

http://groups.google.com.au/group/alt.msdos.batch.nt/msg/cd883858bfaf0fa5
 
B

billious

ajmister said:
Hi
Here is my add date script.

echo off
for /F "tokens=1,2,3" %%r in ('date /t') do (
for /f "tokens=1,2,3 delims=/" %%c in ("%%s") do set filedte=%%e%%c%%d
)
echo %filedte%

ren coverage.txt coverage.txt_%filedte%

If the script runs after midnight I need my script to pick previous day's
date instead of current day's date. My script runs anytime between 10 pm
and 6 am the next day.

Thank you.

Aj

Ah - the dentist scenario...

The format of the "date/t" command varies depending on the setting for the
user under whose account the job is run.

We can conclude that the date format you are using is month/daynumber/year
from this data.

We now have "THE script" but you're not specific as to whether THE script to
which you refer is THIS script or THIS script's CALLER. It seems odd that a
script could be run at a random time between 10pm and 6am, but you offer no
description of why this might be.

So - next step is to determine what the format of TIME/t might be for your
machine. It also conveniently depends on user settings. Mine is in 24-hour
format, but yours may be in 12-hour format, may or may not have leading
zeroes on the hour, and may also have a separator other than ":" (although
that seems unlikely)

Once we know your TIME format, we can construct a scheme to subtract 1 day
if the time is after midnight, so examples of your TIME format would be
useful.

I'll not approach the method for performing the maths yet - the trick is to
recognise the 1st of the month and leap years. Done many times before - and
posted in Timo Salmi's FAQ in alt,msdos.batch.nt, but the key for this
mysterious application is the time - the date-manipulation is the easy
part....

For your existing script though,

for /F "tokens=2,3,4 delims=/ " %%c in ('date /t') do set filedte=%%e%%c%%d
echo %filedte%

would seem to be a worthwhile simplification. Note the SPACE afer the / in
the DELIMS list which makes delimiters "/" OR [space]
 
T

Todd Vargo

foxidrive said:
To figure out which day it is use the time and see if it is before 10, and run
an appropriate routine.

Actually, the backdate routine only needs to detect if the time is between
midnight and 6am.

The following batch returns the desired date in YYYYMMDD format.

@echo off
echo >"%temp%\%~n0.vbs" s=Now
echo>>"%temp%\%~n0.vbs" If Hour(s) ^> 6 Then s=s-1
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^&_
echo>>"%temp%\%~n0.vbs" right(100+month(s),2)^&_
echo>>"%temp%\%~n0.vbs" right(100+day(s),2)
for /f %%a in (
'cscript //nologo "%temp%\%~n0.vbs"') do set day=%%a
del "%temp%\%~n0.vbs"
echo %%day%% is set to "%day%"
 
F

foxidrive

Actually, the backdate routine only needs to detect if the time is between
midnight and 6am.

The following batch returns the desired date in YYYYMMDD format.

@echo off
echo >"%temp%\%~n0.vbs" s=Now
echo>>"%temp%\%~n0.vbs" If Hour(s) ^> 6 Then s=s-1

did you mean < 6 ??
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top