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

Author Message
Gaffer007

Guest

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
{

# 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
}

$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"
martin
[View user's profile]
Joined: 2002-12-10
Posts: 26109
Location: Prague, Czechia

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.
_________________
Martin Prikryl
Gaffer007

Guest

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?

Andy
Gaffer007

Guest

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
Gaffer007

Guest

Description: (none)

martin
[View user's profile]
Joined: 2002-12-10
Posts: 26109
Location: Prague, Czechia
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?

You can post new topics in this forum

## What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!