?
Solved

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

Posted on 2004-08-04
5
Medium Priority
?
3,212 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

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…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

770 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