0

For a dissertation Im trying to repeatedly download an XML through an API. It has to be done every hour or three hours for a long period of time.

I've found a few ways that I can download something once, for instance with PowerShell:

powershell.Exe -command "& {$client = new-object System.Net.WebClient;$client.DownloadFile('http://exampleapi.uk?key=examplekey','C:\location\APIcall.xml')}"

I got this from Periodically download a file from web in Windows 7

However, is it possible to set a dynamic filename so that every download gets a new name and files don't get overwritten? Ideally, they would get a timestamp in their name, but as long as no data is overwritten I'll be happy.

Note: Im a GIS student with some SQL and C++ experience but nothing advanced. I cant use answers that require hours on hours of study just to understand if it can be done, like libcurl or gnuwin, I just need to solve this and move on with using the data for a mapping project.

2

You can create a file path and filename before calling the DownloadFile() method easily using Join-Path.

Something like

powershell.exe -command "& {$fileOut = Join-Path -Path 'C:\location' -ChildPath ('APIcall_{0:yyyy-MM-dd HH_mm_ss}.xml' -f (Get-Date))
                            $client = new-object System.Net.WebClient
                            $client.DownloadFile('http://exampleapi.uk?key=examplekey', $fileOut)}" 

Here I use the -f Format operator to convert the DateTime object from Get-Date into a timestamp string {0:yyyy-MM-dd HH_mm_ss}. Of course, you can format the date differently, but make sure it does not contain invalid filename characters like :, / or \

1
  • Wow thats exactly it!!! Thank you very much, Ill tweak the stamp to suit my subsequent processing, but my data store is already filling up. Excellent! – Liron Chen May 5 at 18:42
0

Get-Date will return the current data & time as a DateTime type. There are a variety of pre-defined or custom formats you can apply.

Here's an example usingthe pre-deifned "s" (sortable) format using the -f format operator:

$BaseName = 'MyBaseName'
$ThisRunName = '{0}_{1:s}' -f $BaseName , ( Get-Date )
$ThisRunName

Output:

PS C:\> $BaseName = 'MyBaseName'
>> $ThisRunName = '{0}_{1:s}' -f $BaseName , ( Get-Date )
>> $ThisRunName
MyBaseName_2021-05-05T13:01:30
PS C:\>\

Low vision here -- didn't even notice the colons ( illegal in filenames ) in the Sortable format. Here's a better example using a custom format string. Dropped the seconds from the time as the O.P. did state every few hours:

$BaseName = 'MyBaseName'
$ThisRunName = '{0}_{1:yyMMdd_HHmm}' -f $BaseName , ( Get-Date )
$ThisRunName

Output:

PS C:\> $BaseName = 'MyBaseName'
>> $ThisRunName = '{0}_{1:yyMMdd_HHmm}' -f $BaseName , ( Get-Date )
>> $ThisRunName
MyBaseName_210505_2239
PS C:\>
2
  • 1
    But that won't do as filename because of the invalid characters in there.. – Theo May 5 at 18:29
  • Then you use a custom format. There was no formatted code, limitations, or preferred format given by the O.P. All you would need to do is change the format specifer. – Keith Miller May 6 at 2:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.