1

I have a batch script that downloads files from my ftp.

It has a section that takes today's date to pull the right files from the server:

FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CDATE=%%B
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
FOR /F "TOKENS=1,2 DELIMS=/ eol=/" %%A IN ('echo %CDATE%') DO SET dd=%%B
FOR /F "TOKENS=2,3 DELIMS=/ " %%A IN ('echo %CDATE%') DO SET yyyy=%%B
SET yy=%yyyy:~-2%
SET filename=%yy%%mm%%dd%

I run this script every week.

Now I'd like to add a section to the script that would automatically delete files that were downloaded 2 weeks ago.

How do I generate a new variable oldfilename that would be the formatted today minus 14 days?

Mofi
  • 46,139
  • 17
  • 80
  • 143
Uno Mein Ame
  • 1,060
  • 5
  • 16
  • 29
  • Search for _"yesterday's date"_ here on SO to find suggestions for date arithmetics; most of them are not pure batch file solutions as it does not support date functions... – aschipfl Apr 10 '16 at 11:26
  • 1
    See results of a search with [\[batch-file\] yesterday date](http://stackoverflow.com/search?q=%5Bbatch-file%5D+yesterday+date) or [Batch file to delete files older than N days](http://stackoverflow.com/questions/51054/). – Mofi Apr 10 '16 at 11:44

1 Answers1

0

Thank you for those who pointed me in the right direction.

Here's the code that I ended up with:

@echo off
set day=-14
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)
for /f %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
del "%temp%\%~n0.vbs"
set "YYYY=%result:~0,4%"
set "MM=%result:~4,2%"
set "DD=%result:~6,2%"
SET yy=%yyyy:~-2%
set "oldfilename=%yy%%mm%%dd%"
Uno Mein Ame
  • 1,060
  • 5
  • 16
  • 29