Topic "SOLVED - Check if any file exist and return a value - Powershell"

Author Message
Muffinz
[View user's profile]

Joined: 2016-12-15
Posts: 7
Location: Sweden
Hi,

How can I check if any file exist on a remote FTP-server with powershell and so the return value is False or True?
I need that value since I use Invoke-Command in an other script to call for this script and if it isn't possible I can change the script a bit so it write to a logfile and I'll have another script checking that log file.

See the "# Check File" line

This returns the value False even if there is a file on the remote location:
Code:

    # Connect
    $session.Open($sessionOptions)
   

    # Remote Path
    $remotePath = "/transfer/ut/*"
   
    # Check File
    $Exsist = $session.FileExists($remotePath)
     
    # Disconnect, clean up
    $session.Dispose()


And this return me with the filename/filenames looking like this:
Code:

Files           
-----           
{.., file.txt, .}

or

Files           
-----           
{.., ., file.txt}

or

Files 
----- 
{.., .}



Code:
   
    # Connect
    $session.Open($sessionOptions)
   

    # Remote Path
    $remotePath = "/transfer/ut/"
   
    # Check File
    $Exsist = $session.ListDirectory($remotePath)
     
    # Disconnect, clean up
    $session.Dispose()


Thank you!

Last edited by Muffinz on 2017-02-01 10:33; edited 1 time in total
Advertisements
savoym
[View user's profile]

Joined: 2016-09-27
Posts: 5
Muffinz wrote:
Hi,

How can I check if any file exist on a remote FTP-server with powershell and so the return value is False or True?
I need that value since I use Invoke-Command in an other script to call for this script and if it isn't possible I can change the script a bit so it write to a logfile and I'll have another script checking that log file.

See the "# Check File" line

This returns the value False even if there is a file on the remote location:
Code:

    # Connect
    $session.Open($sessionOptions)
   

    # Remote Path
    $remotePath = "/transfer/ut/*"
   
    # Check File
    $Exsist = $session.FileExists($remotePath)
     
    # Disconnect, clean up
    $session.Dispose()


And this return me with the filename/filenames looking like this:
Code:

Files           
-----           
{.., file.txt, .}

or

Files           
-----           
{.., ., file.txt}

or

Files 
----- 
{.., .}



Code:
   
    # Connect
    $session.Open($sessionOptions)
   

    # Remote Path
    $remotePath = "/transfer/ut/"
   
    # Check File
    $Exsist = $session.ListDirectory($remotePath)
     
    # Disconnect, clean up
    $session.Dispose()


Thank you!


You might try something like this:

if ($session.FileExists($remoteFile1)) {
Write-Output true |
Out-File -Append "C:\testTrue.txt"
} else {
Write-Output false |
Out-File -Append "C:\testFalse.txt"
}
Muffinz
[View user's profile]

Joined: 2016-12-15
Posts: 7
Location: Sweden
savoym wrote:

You might try something like this:

if ($session.FileExists($remoteFile1)) {
Write-Output true |
Out-File -Append "C:\testTrue.txt"
} else {
Write-Output false |
Out-File -Append "C:\testFalse.txt"
}


Hi,

Thank you for the reply Smile

The problem is when I use ".FileExists" I seems to be needed to specify a filename and I can't do that since all the files have different filenames.

Please correct me if I'm wrong or if I misunderstood something.
savoym
[View user's profile]

Joined: 2016-09-27
Posts: 5
Muffinz wrote:
savoym wrote:

You might try something like this:

if ($session.FileExists($remoteFile1)) {
Write-Output true |
Out-File -Append "C:\testTrue.txt"
} else {
Write-Output false |
Out-File -Append "C:\testFalse.txt"
}


Hi,

Thank you for the reply Smile

The problem is when I use ".FileExists" I seems to be needed to specify a filename and I can't do that since all the files have different filenames.

Please correct me if I'm wrong or if I misunderstood something.


Sorry, I misunderstood where I thought you only needed to check if the value was true or false after you checked if a file existed. You may not need to use the write-out to a file but just some code logic for whatever needs to happen based on the outcome.
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 26447
Location: Prague, Czechia
Something like [Linq.Enumerable]::Any($session.EnumerateRemoteFiles("/transfer/ut", $Null, [WinSCP.EnumerationOptions]::None))
Muffinz
[View user's profile]

Joined: 2016-12-15
Posts: 7
Location: Sweden
martin wrote:
Something like [Linq.Enumerable]::Any($session.EnumerateRemoteFiles("/transfer/ut", $Null, [WinSCP.EnumerationOptions]::None))


Ah, thank you Smile

I used this solution for now

Code:

$Contents = $session.EnumerateRemoteFiles($remotePath, "*.*",[WinSCP.EnumerationOptions]::AllDirectories)
$Contents | select Name | Out-File
Advertisements

You can post new topics in this forum






Search

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