Solved

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

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Command-line Script 15 72
DOS string replace and remove character at end 3 68
DOS Database Program in Windows 10 5 113
bat script to open stickynote with text 3 100
The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

786 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