Page 1 of 1

Increase & replace nub in Bat script Automatic When executed

Posted: 06 Mar 2013 07:30
by vsr1258
Hi,

Sub: Increase & replace numbers in Bat script Automatically When ever Batch file executed

Need your kind help on below scenario.

> Created One Bat File Named as "Sample.bat"

> Let us Assume, The Bat file is executed on 2013/02/28

> The script in the Bat File as in below

> Script Execution Started By clicking the "Sample.Bat"

Start D:\Sample.exe -Start Date 2013/01/01 -End Date 2013/02/28

> Execution completed

> Then My requirement Is:
> After execution it automatically Dates should be replaced as "-Start Date 2013/03/7 -End Date 2013/03/14 " and remaining code is should be same, As shown in below.
Start D:\Sample.exe -Start Date 2013/03/7 -End Date 2013/03/14
-----------------------------------------------------------------
2nd Time executing Bat File.

> Script Execution Started By clicking the "Sample.Bat"

Start D:\Sample.exe -Start Date 2013/03/7 -End Date 2013/03/14

> Execution completed

> Then My requirement Is:
> After execution it automatically Dates should be replaced as "-Start Date 2013/03/14 -End Date 2013/03/21 " and remaining code is should be same, As shown in below.
Start D:\Sample.exe -Start Date 2013/03/14 -End Date 2013/03/21
----------------------------------------------------------------------------------
3rd Time executing Bat File.

> Script Execution Started By clicking the "Sample.Bat"

Start D:\Sample.exe -Start Date 2013/03/7 -End Date 2013/03/14

> Execution completed

> Then My requirement Is:
> After execution it automatically Dates should be replaced as "-Start Date 2013/03/21 -End Date 2013/03/28 " and remaining code is should be same, As shown in below.
Start D:\Sample.exe -Start Date 2013/03/21 -End Date 2013/03/28

--------------------------------------------
Note: The Dates should increased to next 7 days

Any Logic to implement this.

Greatly Appreciated

Thanks in advance

Regards,
ASR

Re: Increase & replace nub in Bat script Automatic When exec

Posted: 06 Mar 2013 07:48
by foxidrive
Do you want to run this batch file say 10 times in a day? Or are you scheduling this to run every thursday?

Re: Increase & replace nub in Bat script Automatic When exec

Posted: 06 Mar 2013 08:07
by vsr1258
Thanks for quick reply.

I want to schedule to run the batch file (Sample.bat) on every week.

Using schedule task option i can schedule to run the batch file.

My Requirement is:
After execution the batch file, then the numbers (Dates) should automatically replaced with new numbers(Dates).

Kindly look into my previous given details

Once again Thanks a lot!

Re: Increase & replace nub in Bat script Automatic When exec

Posted: 06 Mar 2013 09:15
by foxidrive
This might work for you.

On line 6 remove the echo command to make it run and the pause on line 7.

This is what it shows when I run it today (it is 7th March here)

Start "" "D:\Sample.exe" -Start Date 2013/03/01 -End Date 2013/03/07


Code: Select all

@echo off
call :getdate today 0
set end=%day%
call :getdate today -6
set start=%day%

echo Start "" "D:\Sample.exe" -Start Date %start% -End Date %end%
pause

goto :EOF
:getdate
:: Date foward & backward
@echo off
:: from code by Phil Robyn
setlocal
if [%1]==[] (
  echo to get todays date use
  echo call "%~n0" today 0
  echo.
  echo to get yesterdays date use
  echo call "%~n0" today -1
  echo.
  echo to get the date 25 days ago:
  echo call "%~n0" today -25
  echo.
  echo to get the date 1250 days in the future
  echo call "%~n0" today +1250
  goto :EOF)

set date1=%1
set qty=%2
if /i "%date1%" EQU "TODAY" (
 set date1=now
) else (
 set date1="%date1%"
)
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%qty%,%date1%)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^&_
echo>>"%temp%\%~n0.vbs"         right(100+month(s),2)^&_
echo>>"%temp%\%~n0.vbs"         right(100+day(s),2)
for /f %%a in (
  'cscript //nologo "%temp%\%~n0.vbs"') do set result=%%a
del "%temp%\%~n0.vbs"
endlocal& set day=%result:~0,4%/%result:~4,2%/%result:~6,2%
:: echo %%day%% is set to "%day%" (without the quotes)

Re: Increase & replace nub in Bat script Automatic When exec

Posted: 06 Mar 2013 21:45
by vsr1258
Thanks a lot! for the clear details regarding my requirement

But, the below given syntax only accepted.
D:\Sample.exe" -Start Date YYYY/DD/MM -End Date YYYY/DD/MM

Exp: Start D:\Sample.exe -Start Date 2013/03/7 -End Date 2013/03/14
Start D:\Sample.exe -Start Date 2013/03/14 -End Date 2013/03/21
Start D:\Sample.exe -Start Date 2013/03/21 -End Date 2013/03/28

Requirement:
1. Once execution of Bat file then the dates should automatically increased to 7 and replaced, like shown below.
Exp: Start D:\Sample.exe -Start Date 2013/03/7 -End Date 2013/03/14 ----> 1st execution of Bat file
Start D:\Sample.exe -Start Date 2013/03/14 -End Date 2013/03/21----> 2nd execution of Bat file
Start D:\Sample.exe -Start Date 2013/03/21 -End Date 2013/03/28----> 3rd execution of Bat file
.......... Etc...................

Please help me!

Re: Increase & replace nub in Bat script Automatic When exec

Posted: 06 Mar 2013 23:40
by foxidrive
It will work if you schedule the batch file every 7 days.

Today it shows:

Start "" "D:\Sample.exe" -Start Date 2013/03/01 -End Date 2013/03/07

and in 7 days it will show

Start "" "D:\Sample.exe" -Start Date 2013/03/08 -End Date 2013/03/014

Re: Increase & replace nub in Bat script Automatic When exec

Posted: 07 Mar 2013 11:18
by Aacini
The Batch file below use an auxiliary text file to store the next Start and End dates. The first time you run this program the auxiliary file does not exists, so it create the initial dates from 2013/01/01 up to today (as you mentioned in your explanation). All the next times that you run the program the auxiliary file exists, so the dates are the previous run ones incremented in 7 days.

Code: Select all

@echo off
setlocal EnableDelayedExpansion

rem Initialize the number of days per month
set month=0
for %%a in (31 28 31 30 31 30 31 31 30 31 30 31) do (
   set /A month+=1
   set DaysPerMonth[!month!]=%%a
)

rem If the auxiliary file does not exist
if not exist nextDates.txt (
   rem Set dates from 2013/01/01 up to today
   set StartDate=2013/01/01
   for /F "tokens=1-3 delims=/" %%a in ("%date%") do (
      rem Assumed date format: MM/DD/YYYY. Interchange %%a and %%b for DD/MM/YYYY
      set EndDate=%%c/%%a/%%b
   )
) else (
   rem Read next dates from the auxiliary file
   < nextDates.txt (
   set /P StartDate=
   set /P EndDate=
   )
)

rem Execute the desired process
Start D:\Sample.exe -Start Date %StartDate% -End Date %EndDate%

rem Advance the dates 7 days
set StartDate=%EndDate%
call :DateAdd EndDate= %StartDate% +7

rem And store they in the auxiliary file
echo %StartDate%> nextDates.txt
echo %EndDate%>>  nextDates.txt
goto :EOF


:DateAdd result= YYYY/MM/DD +days
for /F "tokens=1-3 delims=/" %%a in ("%2") do (
   set /A YYYY=%%a, MM=1%%b %% 100, DD=1%%c %% 100 %3
)
if %DD% gtr !DaysPerMonth[%MM%]! (
   set /A DD-=DaysPerMonth[%MM%], MM+=1
   if !MM! gtr 12 (
      set /A MM=1, YYYY+=1
   )
)
if %DD% lss 10 set DD=0%DD%
if %MM% lss 10 set MM=0%MM%
set %1=%YYYY%/%MM%/%DD%
exit /B


Antonio

PS - Yes, I know that previous DateAdd subroutine is NOT a general use one, but it works for this problem.