1.11M Members

Create a new folder on an FTP site

 
0
 

Hello,

I’m trying to make a powershell script that will copy files to and FTP server, but the destination folder needs to be generated each time the copy starts. In lieu of having a ton of issues trying to copy the source folder onto a remote FTP site using powershell, I thought of maybe just adding a piece to my shell script which will pre-create a folder with a specific name before starting the file copy. This way, the files will have a place to go besides the root share on the FTP server. I'm able to connect to the FTP site and manually use the makedir command, but articulating that into a script is proving troublesome.

Does anyone have an example of a simple powershell script that will create a folder on an FTP directory?

 
0
 

Better option is to use some FTP tool [cuteFTP].

$newFolder = "ftp://servername/newfolder/"
$ftpuname = "username"
$ftppassword = "password"
try
{
    $makeDirectory = [System.Net.WebRequest]::Create($newFolder);
    $makeDirectory.Credentials = New-Object System.Net.NetworkCredential($ftpuname,$ftppassword);
    $makeDirectory.Method = [System.Net.WebRequestMethods+FTP]::MakeDirectory;
    $makeDirectory.GetResponse();
    #folder created successfully
}catch [Net.WebException]
{
    try {

        #if there was an error returned, check if folder already existed on server
        $checkDirectory = [System.Net.WebRequest]::Create($newFolder);
        $checkDirectory.Credentials = New-Object System.Net.NetworkCredential($ftpuname,$ftppassword);
        $checkDirectory.Method = [System.Net.WebRequestMethods+FTP]::PrintWorkingDirectory;
        $response = $checkDirectory.GetResponse();
        #folder already exists!
        }
    catch [Net.WebException] {                
        #if the folder didn't exist, then it's probably a file perms issue, incorrect credentials, dodgy server name etc
    }    
}
  • Om
 
0
 

I am new in this of write script and I only speak spanish. It is more easy to write that code into a batch file (.cmd) and then translate it to a powershell script. I have many examples of work with ftp:
make directories, check instructions and to execute them etc.
How I can share the code??
I have only to write it here??

 
0
 

If you press the code button you should be able to insert your code into a code block in your message.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article