Solved Subtracting Minutes from current Date in CMD-Batch

May 31, 2020 at 03:14:19
Specs: Windows XP
Dear Community,

I am a hobby weather forecaster and would like to download satellite imagery from a weather site.
For that purpose I use wget (windows version) which I call from a cmd batch job. The filenames contain timestamps in the form YYMMDDHHMM (note the 2-digit year) and images come in 15-min intervals.

The problem:
In order to download e.g. the last 6 hours of satellite imgagery, I need to calculate the corresponding filenames. For example, starting from 31. May 2020, 10.00hr, the filenames will be
- 2005311000,
- 2005310945,
- 2005310930,
- 2005310415 and
- 2005310400.

My Own Approach
I have written two scripts. The first one converts the current date/time into minutes and stores the result in an environment variable "acDatTime". The second script is meant to subtract x minutes from %actDatTime%, converts the reduced number back to a date/time and stores that reduced value in "prcDatTime". Somehow, however, I just don't manage to handle the leap years correctly.

Online research has revealed a number of code snippets, but these do either subtract whole days (not considering the hours), or subtract minutes (not considering the days). What I need is a combination of that.

My Request
Can anyone either share (or provide an URL pointing to) a corresponding code snippet?
I need
- a cmd-batch
- that calculates time stamps in the format YYMMDDHHMM
- for any amount of minutes before the actual date (amount will be passed to the script as parameter).

Many thanks
Jens Grillemeyer

message edited by jgr27

See More: Subtracting Minutes from current Date in CMD-Batch

Reply ↓  Report •

May 31, 2020 at 05:22:35
Dont know how to do that in batch, but would it be possible to call a powershell script?

Gets the current date:

Subtract minutes from current date:

Or subtract days:

To make the dates a variable use $VariableName, f.ex:
$AcDatTime = (Get-Date).ToString('yyMMddHHmm')

Found this post on spiceworks marked solved on how to pass variables from powershell to batch:

I overlooked that you wanted to combine different subtractions:

$Date = (Get-Date).AddYears(-1).AddMonths(-2).AddDays(-3).AddHours(-4).AddMinutes(-5).ToString('yyMMddHHmm')

You can add and substract as much as you want like shown above..

message edited by Kilavila

Reply ↓  Report •

May 31, 2020 at 14:24:03
I use wget

It's been awhile since I used WGET, but
doesn't the WGET command have a time stamping function/option so that you only download a file that does not already exist locally or if a file was modified more recently than the file on your computer?

Wouldn't this solve your problem of what file needed to be downloaded?
You only get the most recent file, regardless of file name.


Reply ↓  Report •

May 31, 2020 at 21:21:49
✔ Best Answer
Here's a vbscript attempt. As written, the value submitted would be in negative minutes from (your local system) current time/date. Output always normalized to 15-multiples of minutes. Another totally different approach might be to wget the "parent" page and extract the available links that match certain criteria, using 'find' or 'findstr'. If you provide the weather URL you're using, I can test that. Anyway...
'----- begin vbscript "weather.vbs" usage: cscript weather.vbs [-]minutes
'--- which in this case is always negative
for i=0 to 1
wscript.echo dd+left(hm,2)&p
'--------- end vbscript

To try: [cscript /nologo] weather.vbs -MINUTES
should display the intended result, "rounded" to the previous 15-minute timeframe. A submitted value of zero should always get the most recent.

message edited by nbrane

Reply ↓  Report •

Related Solutions

May 31, 2020 at 22:30:39
Dear Community,

many thanks for these quick responses.
While I am ultimately after a non-powershell approach, I might go down that route.
Trouble is that I might have to run that script on a computer I can't install anything on.
So I really want to do it with the on-board toolkit.

wget does indeed provide that check, but I still need to calculate the filename
before I can check wehther it is already downloaded.

Many thanks for this,
in fact I have initially used Excel to do the calculations for me (as the quick and dirty solution).

In that file I would enter the start date/time, and the sheet would then build the required wget commands.
Those in turn I simply copied and pasted into a cmd batch file. This, however, is a 2-step process that requires interaction. That's what I want to avoid.

I am not too familiar with vBscript... is such a script capable of donwloading files?
I.e. is there a function "download(URL)"?


Reply ↓  Report •

May 31, 2020 at 23:39:25
Ah. The vbscript is only intended to yield the target URL for the wget to execute, which is launched by a batch. F/e, here's a batch that might be similar:

set URL=
:: this gets the weathersat timestamp
for /f "tokens=*" %%a in ('cscript weather.vbs -0') do set xx=%%a
echo downloading attempt %xx%
echo wget -O %xx% %URL%/%xx%
wget -O %xx% %URL%/%xx%
::----- end batch
this should get the most recent satellite data. For full automation, consider task-scheduler.

Reply ↓  Report •

June 1, 2020 at 04:19:31
Got a link to the images?



Reply ↓  Report •

June 1, 2020 at 07:03:10
wget does indeed provide that check, but I still need to calculate the filename
before I can check wehther it is already downloaded.

That is what WGET does.
Here is the relevant section from the GNU WGET manual
Reissuing the command with ā€˜-Nā€™ will make Wget re-fetch only the files that have been modified since the last download.

So if you download file XX today
Tomorrow, if the time stamp has not changed, it will NOT download it again.
You will only get fresh files, regardless of name.

I am no expert on this but I would urge you do a bit more research on WGET,
you may not need all of this date/time math.


message edited by mmcconaghy

Reply ↓  Report •

June 1, 2020 at 21:59:08
Touche, Mike. I kind of felt there was some over-kill. @M2, that would be helpful.

message edited by nbrane

Reply ↓  Report •

Ask Question