Post a reply

Before posting, please read how to report bug or request support effectively.

Bug reports without an attached log file are usually useless.

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

Muffinz

Re: Check if any file exist and return a value - Powershell

martin wrote:

Something like [Linq.Enumerable]::Any($session.EnumerateRemoteFiles("/transfer/ut", $Null, [WinSCP.EnumerationOptions]::None))


Ah, thank you :)

I used this solution for now

$Contents = $session.EnumerateRemoteFiles($remotePath, "*.*",[WinSCP.EnumerationOptions]::AllDirectories)

$Contents | select Name | Out-File
martin

Re: Check if any file exist and return a value - Powershell

Something like [Linq.Enumerable]::Any($session.EnumerateRemoteFiles("/transfer/ut", $Null, [WinSCP.EnumerationOptions]::None))
savoym

Re: Check if any file exist and return a value - Powershell

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 :)

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.
Muffinz

Re: Check if any file exist and return a value - Powershell

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 :)

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

Re: Check if any file exist and return a value - Powershell

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:
    # 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:
Files            

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

or

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

or

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


   

    # 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

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

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:
    # 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:
Files            

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

or

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

or

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


   

    # Connect
    $session.Open($sessionOptions)
   

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


Thank you!