Issue with Script execution and upload of a file to FTP site

Advertisement

Gaffer007
Guest

Issue with Script execution and upload of a file to FTP site

Hi

We are running a script below to upload a CSV file from a root directory to an FTP site, the root has a new child folder created each day ( folder name is date stamp ) and the CSV name is date stamp in the child folder. When we run the below script i can see the variable populated with the latest CSV file, but the upload to FTP side of the script does not upload the file and we dont get an error, it just doesn't upload. I can manually upload using Winscp so it isnt F/W, permissions etc, can you help please?? our script is below, Thanks in Advance.

-------------------------------

try
{
# Load WinSCP .NET assembly
#Add-Type -Path "WinSCPnet.dll"
Add-Type -Path "e:\Program Files\WinSCP\WinSCPnet.dll"

# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::ftp
HostName = "ftp.********.com"
PortNumber = 21
UserName = "********"
Password = "*********"
}

$session = New-Object WinSCP.Session

try
{
# Connect
$session.Open($sessionOptions)

$localPath = "F:\upload\filestoupload\"
$remotePath = "/in/"

# Select the most recent file.
# The !$_.PsIsContainer test excludes subdirectories.
# With PowerShell 3.0, you can replace this with -File switch of Get-ChildItem.
$latest =
Get-ChildItem -Path $localPath -recurse -Filter *.csv |
Where-Object {!$_.PsIsContainer} |
Sort-Object LastWriteTime -Descending |
Select-Object -First 1

# Any file at all?
if ($latest -eq $Null)
{
Write-Host "No file found"
exit 1
}

# Upload the selected file
$session.PutFiles($session.EscapeFileMask($localPath + $latest.Name), $remotePath).Check()
}
finally
{
# Disconnect, clean up
$session.Dispose()
}

exit 0
}
catch [Exception]
{
Write-Host ("Error: {0}" -f $_.Exception.Message)
exit 1
}

# exit

# Execute the script using a command like:
# "C:\Program Files\WinSCP\WinSCP.exe" /log="C:\writable\path\to\log\WinSCP.log" /ini=nul /script="C:\path\to\script\script.txt"

Reply with quote

Advertisement

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

Re: Issue with Script execution and upload of a file to FTP site

Please attach a full session log file showing the problem (using the latest version of WinSCP).

To generate the session log file, set Session.SessionLogPath. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you can mark the attachment as private.

Reply with quote

Gaffer007
Guest

Session Syntax

Hi

Sorry with my limited scripting knowledge im struggling to output a session log file. I have checked the syntax of the command but cant see how to get it to produce a Log output when im adding in my script.

Can you help with adding the command / syntax to enable a log to be outputted?

Thanks in Advance.

Andy

Reply with quote

Gaffer007
Guest

WinSCP session log

Hi

I managed to work out the error of my ways generating the log file. I have attached the log file.

Hope you can help

Regards

Reply with quote

Advertisement

martin
Site Admin
martin avatar

Re: Issue with Script execution and upload of a file to FTP site

I cannot see how you cannot get any error. The script clearly fails.

WinSCP gets path F:\Symphony\iExchange\Repository\NLPG Hub\4205_20170124_01.csv (what I do not see how it corresponds to your code, which uses F:\upload\filestoupload\). And WinSCP fails to find that file. Is the path correct?

Reply with quote

Advertisement

You can post new topics in this forum