Solved

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

Posted on 2004-08-04
5
3,183 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

679 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