NetCmdlets: FTP Test-RemotePath


Checking to see if a path exists on an FTP server is a very simple operation when using NetCmdlets. The below script shows how to check for the existence of a remote path.

param( [string] $server,
       [string] $user,
       [string] $password,
       [string] $path ) 
    
if ($server -eq $null) {
  $server = Read-Host "FTP Server [localhost]";
  if ($server -eq $null) { 
	$server = "localhost"
  }
}      
    
$results = (Get-FTP -Server $server -User $user -Password $password -List "$($path)*")
return ($results -ne $null)
	
trap{
  Write-Host "Exception occurred (please find more information below), script execution was terminated."
  break
}

If you need to check if a directory exists before attempting an upload you can simply do:

PS> .\Test-RemotePath.ps1 -Server myhost -User test -Password test -Path Folder1/SubFolder

We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@nsoftware.com.