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 •

#1
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:
(Get-Date).ToString('yyMMddHHmm')

Subtract minutes from current date:
(Get-Date).AddMinutes(-15).ToString('yyMMddHHmm')

Or subtract days:
(Get-Date).AddDays(-1).ToString('yyMMddHHmm')

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: https://community.spiceworks.com/to...

Edit:
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 •

#2
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.

MIKE

http://www.skeptic.com/


Reply ↓  Report •

#3
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
v=wscript.arguments(0)
z=dateadd("n",v,now)
dd=formatdatetime(z,2)
hm=formatdatetime(z,4)
n=split(dd,"/")
for i=0 to 1
n(i)=right("0"+n(i),2)
next
dd=right(n(2),2)+n(0)+n(1)
hm=replace(hm,":","")
p=(right(hm,2)\15)*15
p=right(0&p,2)
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

#4
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.


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

@nbrane
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)"?


Grille


Reply ↓  Report •

#5
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=https://www.weathersat.com/xxx/yyy/
:: 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 •

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

=====================

M2


Reply ↓  Report •

#7
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.

MIKE

http://www.skeptic.com/

message edited by mmcconaghy


Reply ↓  Report •

#8
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