Post a reply

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

martin

Re: Upload file if exists (filename changes daily)

You cannot use Session.FileExists to test for an existence of a local file.
Use Test-Path cmdlet instead.
See https://winscp.net/eng/docs/script_checking_file_existence#local
Though it makes no sense anyway. You do not want to test if a file exist, you want to test if Get-ChildItem returned any file. So just do:

if ($localFile) {
   ...
}
laya_warren

Upload file if exists (filename changes daily)

Hi All

Im using powershell to upload a file if it exists, its a daily file with the date appended to the filename:
# Logging
$session.SessionLogPath = $log
# Connect
Write-Host "Connecting to DCK FTP..."
$session.Open($sessionOptions)
 
# Retrieve filename from local path
$localFile = Get-ChildItem $localPath -Filter $task*.zip | % { $_.FullName }
 
if ($session.FileExists($localFile)) {
    # Upload
    $session.PutFiles($localFile, $remotePath).Check()
    exit 0
}
else {
    Write-Host "Local Test file does not exist"
    exit 1
}


here is a snippit from the log:


> 2020-06-11 16:43:45.658 Script: stat -- "\\layafsp3\InspireFtp\Testing110620.zip"
. 2020-06-11 16:43:45.658 Listing file "\\layafsp3\InspireFtp\Testing110620.zip".
> 2020-06-11 16:43:45.658 Type: SSH_FXP_LSTAT, Size: 73, Number: 263
< 2020-06-11 16:43:45.674 Type: SSH_FXP_STATUS, Size: 32, Number: 263
< 2020-06-11 16:43:45.674 Status code: 2, Message: 263, Server: No such file., Language: en
< 2020-06-11 16:43:45.674 Script: Can't get attributes of file '\\layafsp3\InspireFtp\Testing110620.zip'.
< 2020-06-11 16:43:45.674 No such file or directory.
< 2020-06-11 16:43:45.674 Error code: 2
< 2020-06-11 16:43:45.674 Error message from server (en): No such file.
. 2020-06-11 16:43:45.674 Script: Failed
> 2020-06-11 16:43:45.721 Script: exit
. 2020-06-11 16:43:45.721 Script: Exit code: 1
. 2020-06-11 16:43:45.721 Closing connection.
. 2020-06-11 16:43:45.721 Sending special code: 12
. 2020-06-11 16:43:45.721 Sent EOF message


how do I get it to get the attributes of the file?

Thanks in advance