Solved

batch file trying to get date minus a certain amount of days to work

Posted on 2004-08-04
5
3,171 Views
Last Modified: 2010-08-05
Hi!

I am trying to use the below listed statement to provide a runtime command similiar to:

OCR.EXE %DATE:~4%-4 %DATE:~4% %Account% ALARMLOG%Cnt%.TXT>OCROUT.TXT

OCR.EXE 08/01/2004 08/04/2004 ...

Everything works except the %DATE:~4% -4

Any ideas?
0
Comment
Question by:TrueBlue
  • 3
  • 2
5 Comments
 
LVL 30

Expert Comment

by:SteveGTR
ID: 11719855
Try this:

@echo off
setlocal

for /f "tokens=2-4 delims=/ " %%f in ('date /t') do (
  set mm=%%f
  set dd=%%g
  set yyyy=%%h
)

REM Substract your 4 days here
set /A dd=1%dd% - 104
set /A mm=1%mm% - 100

if /I %dd% GTR 0 goto DONE

set /A mm=%mm% - 1

if /I %mm% GTR 0 goto ADJUSTDAY

set /A mm=12
set /A yyyy=%yyyy% - 1

:ADJUSTDAY

if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
if %mm%==12 goto SET31

:SET31

set /A dd=31 + %dd%

goto DONE

:SET30

set /A dd=30 + %dd%

goto DONE

:LEAPCHK

set /A tt=%yyyy% %% 4

if not %tt%==0 goto SET28

set /A tt=%yyyy% %% 100

if not %tt%==0 goto SET29

set /A tt=%yyyy% %% 400

if %tt%==0 goto SET29

:SET28

set /A dd=28 + %dd%

goto DONE

:SET29

set /A dd=29 + %dd%

:DONE

if /i %dd% LSS 10 set dd=0%dd%
if /i %mm% LSS 10 set mm=0%mm%

OCREXE %mm%/%dd%/%yyyy% %DATE:~4% %Account% ALARMLOG%Cnt%.TXT>OCROUT.TXT

Good Luck,
Steve
0
 

Author Comment

by:TrueBlue
ID: 11720101
Hi Steve:

I am getting the following error from this code:

Wed 08/04/2004 15:52:33.12 123456 9009

I only need the date in this format mm/dd/yyyy.

I do not need the day of week nor the time.

Any ideas?
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 11720148
Error? I'm not sure I understand the context of the error. Is that what is in OCROUT.TXT?

Try to echo the command out to see if it gives you what you requested:

echo OCREXE %mm%/%dd%/%yyyy% %DATE:~4% %Account% ALARMLOG%Cnt%.TXT

You'll have to incorporate your %Account% and %Cnt% processing into my code.

0
 

Author Comment

by:TrueBlue
ID: 11720362
Hi Steve!

I figured out where the error was coming from...

I cut and pasted your code into my batch file.
You missed the period between OCR and EXE.

The problem I have now is only the last account is left in the alarmlog file after the process terminates without the new code
and a start date I get 6 accounts in the log file.

:: *** If AccountList is specified, this will be used. If it's set to an empty value ("set AccountList="),
:: *** the range defined in StartAccount to EndAccount will be used.
set StartAccount=%1
set EndAccount=%2

if not "%AccountList%"=="" goto ManualList
for /l %%a in (%StartAccount%,1,%EndAccount%) do call :process %%a
goto leave

:ManualList
for %%a in (%AccountList%) do call :process %%a
goto leave

:: **********************************************************************
:: *** Subroutine process
:: *** First argument: Account number
:process
set Account=%1
echo Processing account %Account% ...

:: ************** Steve's code begins here ****************************
@echo off
setlocal

for /f "tokens=2-4 delims=/ " %%f in ('date /t') do (
  set mm=%%f
  set dd=%%g
  set yyyy=%%h
)

REM Substract your 4 days here
set /A dd=1%dd% - 104
set /A mm=1%mm% - 100

if /I %dd% GTR 0 goto DONE

set /A mm=%mm% - 1

if /I %mm% GTR 0 goto ADJUSTDAY

set /A mm=12
set /A yyyy=%yyyy% - 1

:ADJUSTDAY

if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
if %mm%==12 goto SET31

:SET31

set /A dd=31 + %dd%

goto DONE

:SET30

set /A dd=30 + %dd%

goto DONE

:LEAPCHK

set /A tt=%yyyy% %% 4

if not %tt%==0 goto SET28

set /A tt=%yyyy% %% 100

if not %tt%==0 goto SET29

set /A tt=%yyyy% %% 400

if %tt%==0 goto SET29

:SET28

set /A dd=28 + %dd%

goto DONE

:SET29

set /A dd=29 + %dd%

:DONE

if /i %dd% LSS 10 set dd=0%dd%
if /i %mm% LSS 10 set mm=0%mm%

OCR.EXE %mm%/%dd%/%yyyy% %DATE:~4% %Account% ALARMLOG%Cnt%.TXT>OCROUT.TXT

:**************** Steve's code ends here *******************

if errorlevel 1 echo %Date% %Time% %Account% %Errorlevel% >>FAILLOG.TXT
if errorlevel 1 copy FAILLOG.TXT+OCROUT.TXT
del OCROUT.TXT

set /a Cnt+=1
goto :eof
:: **********************************************************************
:leave

Any ideas?
0
 
LVL 30

Accepted Solution

by:
SteveGTR earned 500 total points
ID: 11720548
Remove the following from my code:

@echo off
setlocal

The setlocal is causing the Cnt to go out of scope when the routine is finished.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now