Topic "powershell session.FileExists($remotePath) foreach"

Author Message
unimog

Guest


So I've tested the generic code of $session.FileExists($remotePath) which works great but as soon as I put the statement within a foreach loop it keeps telling me file doesn't exists. Anyone have experience or a solution to the problem I'm having?

Code:

try
{
    # Load WinSCP .NET assembly
    Add-Type -Path "C:\winscp\WinSCPnet.dll"
 
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions
    $sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
    $sessionOptions.HostName = "server01"
    $sessionOptions.UserName = "user"
    $sessionOptions.Password = "user1"
    $sessionOptions.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)
        $programFileNameList = Get-Content D:\program\programFileName.lst

        #Program List Error Check
        foreach( $programFileName in $programFileNameList )
        {
           
            Write-Host ("name: $programFileName")
            $remotePath = "/home/user/utility/Program/$programFileName"
            #$remotePath = "/home/user/utility/Program/program2.csv"
            Write-Host ("searching: $remotePath")
            if($session.FileExists($remotePath))
            {
                Write-Host ("File {0} exists" -f $remotePath)
            }

            else
            {
                Write-Host ("File {0} does not exist" -f $remotePath)
            }
        }
       

             
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch [Exception]
{
    Write-Host $_.Exception.Message
    exit 1
}
Advertisements
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24512
Location: Prague, Czechia
Please attach a full session log file showing the problem (using the latest version of WinSCP).

To generate 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
unimog

Guest


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

To generate 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.


file attached
psftp.log (8.89 KB) Private file

Description: (none)

martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24512
Location: Prague, Czechia
There's a space at the end of every file path.

Code:
                                                                                  v
> 2015-01-06 11:14:13.026 Script: stat -- "/xjp/utility/CapEx/Program/program2.csv "
                                                                                  ^   

_________________
Martin Prikryl
Advertisements

You can post new topics in this forum






Search Site

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!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License