• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3273
  • Last Modified:

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

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
TrueBlue
Asked:
TrueBlue
  • 3
  • 2
1 Solution
 
SteveGTRCommented:
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
 
TrueBlueAuthor Commented:
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
 
SteveGTRCommented:
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
 
TrueBlueAuthor Commented:
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
 
SteveGTRCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now