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

Advertisement

Muffinz
Joined:
Posts:
7
Location:
Sweden

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!
Last edited by Muffinz on 2017-02-01 10:33; edited 1 time in total

Reply with quote

Advertisement

savoym
Joined:
Posts:
5

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

Reply with quote

Muffinz
Joined:
Posts:
7
Location:
Sweden

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.

Reply with quote

savoym
Joined:
Posts:
5

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.

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
40,476
Location:
Prague, Czechia

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

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

Reply with quote

Advertisement

Muffinz
Joined:
Posts:
7
Location:
Sweden

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

Reply with quote

Advertisement

You can post new topics in this forum