Prepend timestamp for wildcard upload deleting characters from filename

Advertisement

onetwotreee
Joined:
Posts:
1

Prepend timestamp for wildcard upload deleting characters from filename

Hello all,

I'm uploading all files in a directory to a remote SFTP site and if I prepend a timestamp the timestamp will remove characters from the filename e.g.

The command I'm using:
put *.* %TIMESTAMP#yyyymmdd%*.*
What I'm expecting:
20220914_abc_defghij_klmn.csv
The result:
20220914_ij_klmn.csv
How can I fix this?

Reply with quote

Advertisement

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

Re: Prepend timestamp for wildcard upload deleting characters from filename

WinSCP operation masks do not support prepending (imo, it's actually how the masks work in general with all Windows/Linux command-line tools, so it should not be really surprising).

You would have to use more powerful scripting language. Like PowerShell with use of WinSCP .NET assembly.
https://winscp.net/eng/docs/library_powershell

Something like this (untested):
$remotePath = "/remote/path"
$timestamp = (Get-Date -Format "yyyyMMdd")
$dirInfo = $session.ListDirectory($remotePath)
foreach ($file in $dirInfo.Files)
{
    $newName = $timestamp + "_" + $file.Name
    $newPath = [WinSCP.RemotePath]::Combine($remotePath, $newName)
    $session.MoveFile($file.FullName, $newName)
}

Reply with quote

Advertisement

You can post new topics in this forum