Make transfer with script

Advertisement

Xaviez
Joined:
Posts:
2

Make transfer with script

Hello,
I hope someone can help me with my issue, I have always loved WinSCP and used it, thanks so much for a great program. I've just started to make some scripts and I stumble across a issue now.

I need a script that moves from a FTP to a SMB share. The file that comes to FTP changes name everyday since it takes actual day in the ending of the file.
I managed to get a script function well but that was when file was same name all the time. How do I create a script that takes this file and moves it to a SMB then renames it.

The rename is always the same that should land on SMB share.
The file I transfer from FTP is always the same name in the beginning 6 digits.

This is "my" generated script without the rename and when the file was same name all the time:
$PSNativeCommandArgumentPassing = "Legacy"
 
& "C:\Program Files (x86)\WinSCP\WinSCP.com" `
  /log="C:\Program Files (x86)\WinSCP\script\Log\WinSCP.log" /ini=nul `
  /command `
    "open sftp://XXXXXXXXXXXX" `
    "cd /Folder/foldername" `
    "lcd \\SMB-share" `
    "get 12345678" `
    "exit"
 
$winscpResult = $LastExitCode
if ($winscpResult -eq 0)
{
  Write-Host "Success"
}
else
{
  Write-Host "Error"
}
 
exit $winscpResult

Reply with quote

Advertisement

Xaviez
Joined:
Posts:
2

So I edited the script with following:
 "get -delete *.xml" `

And it works for now since its only 1 file with XML but will be an issue if there is more files.
I tried some with the filemask command but no success yet.

The rename script I used a ps1 script with task scheduler:
$folder_path = "SMB share"
$old_name_prefix = "XXXX"
$new_name_prefix = "XXX"
$today_date = Get-Date -Format "yyyyMMdd"
 
Set-Location -Path $folder_path
 
Get-ChildItem -Path . -Filter "$old_name_prefix*" | ForEach-Object {
    $new_filename = "$new_name_prefix" + "." + $today_date + $_.Extension
    Rename-Item -Path $_.FullName -NewName $new_filename
    Write-Output "File '$($_.Name)' has been renamed to '$new_filename'"
    return
}
 
Write-Output "No file found starting with '$old_name_prefix'"

Reply with quote

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

When there are more files, based on what do you want to pick the correct one?

To "rename" file, just specify the target name in the get command:
 "get -delete *.xml newname" `

Reply with quote

Guest

There will be some more files but this specific file that I transfer will always have same name the first 5 digits atleast

Reply with quote

Advertisement

You can post new topics in this forum