Append number to file names

Advertisement

xeric
Joined:
Posts:
3
Location:
USA

Append number to file names

I have a script that runs once per day to download files from a remote server. Because of the way they name the files, the names are sometimes too long. So currently I have it renaming the files and appending the date to the end (today variable is set in batch script independent of WinSCP functionality), but if there are multiple files I'm not sure how to handle it. There will be between 1 and 5 files at a time, so I was wondering if there is a way I can just add a number to the filename such as 1, 2, 3, etc.

It's currently in a batch script, and the WinSCP command is listed below. I am also open to other solutions if anyone has an idea. Thanks in advance.

"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
  /command ^
    "open sftp://redacted/ -hostkey=""redacted=""" ^
    "cd /Home/bhpa19658/from_bh" ^
    "get * -transfer=ascii -filemask=EVT_*.csv>1D R:\Files\EVT_%today%.csv" ^
    "exit" >> R:\Logs\EventsLog_%today%.txt

Reply with quote

Advertisement

xeric
Joined:
Posts:
3
Location:
USA

Re: Append number to file names

martin wrote:

You cannot append an incrementan number. But you can append current milliseconds using %TIMESTAMP% syntax. And you can use it even for the date:
https://winscp.net/eng/docs/scripting#timestamp

"get * -transfer=ascii -filemask=EVT_*.csv>1D R:\Files\EVT_%%TIMESTAMP#yyyy-mm-dd-hh-nn-ss-zzz%%.csv" ^
I thought this would work, but it still just overwrites the file with the same name. So if there are 2 files, it will download and use the same timestamp filename for both files. Any other ideas for how I can differentiate the names so they don't get overwritten?

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,442
Location:
Prague, Czechia

Re: Append number to file names

Right, I didn't realize that. Sorry.

Instead, you can use WinSCP .NET assembly from e.g. a PowerShell script. That will allow you do do whatever you want.

You can start with this example:
https://winscp.net/eng/docs/library_example_recursive_download_custom_error_handling#powershell

There just put this code
$number++
$localFilePath += $number
before
$transferResult = $session.GetFiles($remoteFilePath, $localFilePath)
(untested, but should work – this time).

Reply with quote

Advertisement

You can post new topics in this forum