Upload one file with date stamp to SFTP
Hi everyone, I am new to WinSCP and I don't have a clue about scripting or programming. I have been reading how to automate a process I do each morning but I am not getting it. So each morning I have to upload this file that is a .csv into the SFTP but the file name has a date stamp of the date before file_name_20161205 but today is 12/6/2016. After that file has been uploaded it would get verified and the new version would be dropped into another folder in the same SFTP in which we get an email that means it is ready for pick up. I would like to have this script scheduled thru my windows pc unless anyone knows of another way.
I am having a hard time with the script... I am using WinSCP version 5.9.3
Now from what I understand I can use this code %TIMESTAMP-1D#yyyymmdd% and that will subtract one date out of the current date, how do I do it for powershell. However I have no clue how I can tell the script to put this file with the name file_name_20161205 to be on the sftp and how does it know where it is going to grab the file from.
I am having a hard time with the script... I am using WinSCP version 5.9.3
Now from what I understand I can use this code %TIMESTAMP-1D#yyyymmdd% and that will subtract one date out of the current date, how do I do it for powershell. However I have no clue how I can tell the script to put this file with the name file_name_20161205 to be on the sftp and how does it know where it is going to grab the file from.
try { # Load WinSCP .NET assembly Add-Type -Path "WinSCPnet.dll" # Setup session options $sessionOptions = New-Object WinSCP.SessionOptions -Property @{ Protocol = [WinSCP.Protocol]::Sftp HostName = "example.com" UserName = "user" Password = "mypassword" SshHostKeyFingerprint = "ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" } $session = New-Object WinSCP.Session try { # Connect $session.Open($sessionOptions) $stamp = Get-Date -Format "yyyyMMdd" #how do I do -1 date? "-1dyyyMMdd"? $fileName = "file_name_$stamp.csv" $remotePath = "/home/user/sysbatch/$fileName" $localPath = "T:\prb\$fileName" #I only need for this script to upload one file, how do I make sure it will only upload that one file and not all of them. # Upload files $transferOptions = New-Object WinSCP.TransferOptions $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary $transferResult = $session.PutFiles("$localPath", "$remotePath", $False, $transferOptions) # Throw on any error $transferResult.Check() # Print results foreach ($transfer in $transferResult.Transfers) { Write-Host ("Upload of {0} succeeded" -f $transfer.FileName) } } finally { # Disconnect, clean up $session.Dispose() } exit 0 } catch [Exception] { Write-Host ("Error: {0}" -f $_.Exception.Message) exit 1 }