Creating a directory on remote FTP using powershell

I can put the file on a remote FTP with a modified version ...

$File = "D:\Dev\somefilename.zip" $ftp = "ftp://username: password@example.com /pub/incoming/somefilename.zip" "ftp url: $ftp" $webclient = New-Object System.Net.WebClient $uri = New-Object System.Uri($ftp) "Uploading $File..." $webclient.UploadFile($uri, $File) 

I ran into a problem that I am trying to upload a file to a directory that does not exist, put does not work. Therefore, I need to create the target directory first. GET-MEMBER does not seem to show any methods that I can call to create a directory, only file operations.

+4
source share
1 answer

I am using the Create-FtpDirectory

 function Create-FtpDirectory { param( [Parameter(Mandatory=$true)] [string] $sourceuri, [Parameter(Mandatory=$true)] [string] $username, [Parameter(Mandatory=$true)] [string] $password ) if ($sourceUri -match '\\$|\\\w+$') { throw 'sourceuri should end with a file name' } $ftprequest = [System.Net.FtpWebRequest]::Create($sourceuri); $ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::MakeDirectory $ftprequest.UseBinary = $true $ftprequest.Credentials = New-Object System.Net.NetworkCredential($username,$password) $response = $ftprequest.GetResponse(); Write-Host Upload File Complete, status $response.StatusDescription $response.Close(); } 

Taken from Ftp.psm1 , where you can also find other functions for FTP.

To others: it’s a pity that they did not follow the well-known template of the noun verb .;)

+6
source

All Articles