Is there a way to subtract 2 days from this batch file?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Maverick
Posts: 3
Joined: 27 Mar 2023 09:49

Is there a way to subtract 2 days from this batch file?

#1 Post by Maverick » 23 Jun 2023 11:55

Code: Select all

@echo
cd \
D:
cd "D:\Misc\Downloads"
For /f "tokens=1-4 delims=/ " %%d in ("%date%") DO REN "flyer.pdf" "Store weekly ad %%g%%e%%f.pdf"
%windir%\explorer.exe /n,/e,"D:\Misc\Downloads\"
:END
Every Sunday morning, my local grocery store releases their weekly sales ad onto their website. I would load up their website into my browser, click on the download button for the weekly ad (which came in a PDF format). Then I would go that specific folder and manually rename the file to:
"Store weekly ad 20230604.pdf".

The date used for the filename would always be on that very same day that I downloaded it. I am not a DOS expert by any means. I did some googling and found a script very similar to what I needed so this process could be automated. It was working just fine until about 4 or 5 weeks ago. (See the script above).

For whatever unknown godly reason, they have changed things around on their website. The download link for the PDF is not posted right away. Now I have to wait a couple of days for the link to appear. Usually appears by Tuesday morning. Is there a way to subtract 2 days from the batch script above?

Just to clarify:

They release a new sales ad every Sunday. If I continue to use the dos batch file above, I am forced to manually adjust the filename so it reflects the new release date of Sundays 2 days prior to the actual download. Hope I am explaining this clearly.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Is there a way to subtract 2 days from this batch file?

#2 Post by Aacini » 23 Jun 2023 18:40

I think you could solve your problem via the code posted at Generating consecutive dates.

You could also slightly modify the code in order to precisely decrement two days in a very simple way...

Antonio

Maverick
Posts: 3
Joined: 27 Mar 2023 09:49

Re: Is there a way to subtract 2 days from this batch file?

#3 Post by Maverick » 23 Jun 2023 22:08

Aacini wrote:
23 Jun 2023 18:40
You could also slightly modify the code in order to precisely decrement two days in a very simple way...

Antonio
That's exactly what I am here asking for help.

DOSuser
Posts: 4
Joined: 25 Sep 2023 17:35

Re: Is there a way to subtract 2 days from this batch file?

#4 Post by DOSuser » 01 Oct 2023 07:27

Maverick wrote:
23 Jun 2023 11:55

Code: Select all

@echo
cd \
D:
cd "D:\Misc\Downloads"
For /f "tokens=1-4 delims=/ " %%d in ("%date%") DO REN "flyer.pdf" "Store weekly ad %%g%%e%%f.pdf"
%windir%\explorer.exe /n,/e,"D:\Misc\Downloads\"
:END
Every Sunday morning, my local grocery store releases their weekly sales ad onto their website. I would load up their website into my browser, click on the download button for the weekly ad (which came in a PDF format). Then I would go that specific folder and manually rename the file to:
"Store weekly ad 20230604.pdf".

The date used for the filename would always be on that very same day that I downloaded it. I am not a DOS expert by any means. I did some googling and found a script very similar to what I needed so this process could be automated. It was working just fine until about 4 or 5 weeks ago. (See the script above).

For whatever unknown godly reason, they have changed things around on their website. The download link for the PDF is not posted right away. Now I have to wait a couple of days for the link to appear. Usually appears by Tuesday morning. Is there a way to subtract 2 days from the batch script above?

Just to clarify:

They release a new sales ad every Sunday. If I continue to use the dos batch file above, I am forced to manually adjust the filename so it reflects the new release date of Sundays 2 days prior to the actual download. Hope I am explaining this clearly.
under dostime functions on this site copy the jdate batch program to jdate,bat in the same folder as your current batch file them copy jdate2date to jdate2date.bat in the same folder then enter the code
call jdate JD
call jdate2date %JD%-2 y m d
echo %y%%m%%d%
REN "flyer.pdf" "Store weekly ad %y%%m%%d%.pdf"

mataha
Posts: 35
Joined: 27 Apr 2023 12:34

Re: Is there a way to subtract 2 days from this batch file?

#5 Post by mataha » 03 Oct 2023 19:03

Though incomplete, you might want to use $dayadd macro from here like so:

Code: Select all

set /a "date.day=2, date.month=3, date.year=2000"
%$putout% "%date.day%.%date.month%.%date.year%"
%$dayadd% date -2
%$putout% "%date.day%.%date.month%.%date.year%"

Code: Select all

2.3.2000
29.2.2000

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Is there a way to subtract 2 days from this batch file?

#6 Post by miskox » 08 Oct 2023 09:52

I have these two date functions (I really don't remember where I found them - I did not write them). But one does not work as intented. I used one to add 7 days to calculate dates (for wednesdays) but at one time there were errors:

date_calc_error.cmd

Code: Select all

@echo off
set start_dd=21
set start_mm=12
set start_yy=2010

:GO_BACK
echo WScript.Echo Weekday^("%start_yy%-%start_mm%-%start_dd%",1^)>weekday.vbs
cscript /nologo weekday.vbs>dow.tmp
set /p dow=<dow.tmp
if not "%dow%"=="3" set dow=ERROR
echo dd=%start_dd% mm=%start_mm% yyyy=%start_yy% dow=%dow%
call :ADDDAYS %start_dd% %start_mm% %start_yy% 7

set start_dd=%dd%
set start_mm=%mm%
set start_yy=%yy%

goto :GO_BACK


:ADDDAYS
set dd=%1
set mm=%2
set yy=%3
set koliko=%4
		)
if %dd% LSS 10 set dd=%dd:~1%
if %mm% LSS 10 set mm=%mm:~1%

set mo=%mm%
set yr=%yy%
set /a mo=%mo%+1
set /a a=%yr%/100
set /a b=2-%a%+%a%/4
set /a jd=%yr%*36525/100+%mo%*306001/10000+%dd%+%b%-694084

if %dd% LSS 10 set dd=0%dd%
if %mm% LSS 10 set mm=0%mm%

set /a jd=%jd%+%koliko%
set /a yy=%jd%*100/36525
set /a dd=%jd%-%yy%*36525/100
set /a mm=%dd%*10/306
set /a dd=%dd%-(%mm%*306+5)/10
if %dd%==0 (
	set dd=31
	set /a mm=%mm%-1
	if %mm%==0 set dd=29
		)
set /a mm=%mm%+3
if %mm% GTR 12 (
	set /a mm=%mm%-12
	set /a yy=%yy%+1
		)
set /a yy=%yy%+1900
if %dd% LSS 10 set dd=0%dd%
if %mm% LSS 10 set mm=0%mm%
goto :EOF
Output (dow=3=wednesday):

Code: Select all

dd=21 mm=12 yyyy=2010 dow=3
dd=28 mm=12 yyyy=2010 dow=3
dd=04 mm=01 yyyy=2011 dow=3
dd=09 mm=01 yyyy=2011 dow=ERROR
dd=14 mm=01 yyyy=2011 dow=ERROR
dd=19 mm=01 yyyy=2011 dow=ERROR
dd=24 mm=01 yyyy=2011 dow=ERROR
dd=29 mm=01 yyyy=2011 dow=ERROR
dd=03 mm=02 yyyy=2011 dow=ERROR
dd=07 mm=02 yyyy=2011 dow=ERROR
dd=11 mm=02 yyyy=2011 dow=ERROR
dd=15 mm=02 yyyy=2011 dow=3
dd=19 mm=02 yyyy=2011 dow=ERROR
dd=23 mm=02 yyyy=2011 dow=ERROR
dd=27 mm=02 yyyy=2011 dow=ERROR
dd=03 mm=03 yyyy=2011 dow=ERROR
This one works:

Code: Select all

@echo off
set start_dd=21
set start_mm=12
set start_yy=2010

:GO_BACK
echo WScript.Echo Weekday^("%start_yy%-%start_mm%-%start_dd%",1^)>weekday.vbs
cscript /nologo weekday.vbs>dow.tmp
set /p dow=<dow.tmp
if not "%dow%"=="3" set dow=ERROR
echo dd=%start_dd% mm=%start_mm% yyyy=%start_yy% dow=%dow%
call :ADDDAYS JD %start_yy% %start_mm% %start_dd%

set start_dd=%dd%
set start_mm=%mm%
set start_yy=%yyyy%

goto :GO_BACK


:ADDDAYS
set "yy=%~2"&set "mm=%~3"&set "dd=%~4"
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
if %yy% LSS 100 set /a yy+=2000 &rem Adds 2000 to two digit years
set /a JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)

set /a jd=jd+7

call :j2d %jd% yyyy mm dd
goto :EOF

:j2d
SETLOCAL ENABLEDELAYEDEXPANSION
set /a L= %~1+68569,     N= 4*L/146097, L= L-(146097*N+3)/4, I= 4000*(L+1)/1461001
set /a L= L-1461*I/4+31, J= 80*L/2447,  K= L-2447*J/80,      L= J/11
set /a J= J+2-12*L,      I= 100*(N-49)+I+L
set /a YYYY= I,  MM=100+J,  DD=100+K
set MM=%MM:~-2%
set DD=%DD:~-2%
( ENDLOCAL & REM RETURN VALUES
    IF "%~2" NEQ "" (SET %~2=%YYYY%) ELSE echo.%YYYY%
    IF "%~3" NEQ "" (SET %~3=%MM%) ELSE echo.%MM%
    IF "%~4" NEQ "" (SET %~4=%DD%) ELSE echo.%DD%
)
goto :EOF

Output (dow=3=wednesday):

Code: Select all

dd=21 mm=12 yyyy=2010 dow=3
dd=28 mm=12 yyyy=2010 dow=3
dd=04 mm=01 yyyy=2011 dow=3
dd=11 mm=01 yyyy=2011 dow=3
dd=18 mm=01 yyyy=2011 dow=3
dd=25 mm=01 yyyy=2011 dow=3
dd=01 mm=02 yyyy=2011 dow=3
dd=08 mm=02 yyyy=2011 dow=3
dd=15 mm=02 yyyy=2011 dow=3
dd=22 mm=02 yyyy=2011 dow=3
dd=01 mm=03 yyyy=2011 dow=3
dd=08 mm=03 yyyy=2011 dow=3
dd=15 mm=03 yyyy=2011 dow=3
dd=22 mm=03 yyyy=2011 dow=3
dd=29 mm=03 yyyy=2011 dow=3
dd=05 mm=04 yyyy=2011 dow=3
dd=12 mm=04 yyyy=2011 dow=3
dd=19 mm=04 yyyy=2011 dow=3
dd=26 mm=04 yyyy=2011 dow=3
dd=03 mm=05 yyyy=2011 dow=3
dd=10 mm=05 yyyy=2011 dow=3
dd=17 mm=05 yyyy=2011 dow=3
dd=24 mm=05 yyyy=2011 dow=3
dd=31 mm=05 yyyy=2011 dow=3
Hope this helps.
Saso

Post Reply