Copy folders through FTP in Windows

2019-02-20 07:55发布

问题:

I have a Windows 2012 server where I am trying to copy a folder through FTP. The folder contains multiple folders inside it and the size is around 12 GB. What command can be used to copy the whole tree structure including all the folders and files inside it.

  • I cannot zip this folder.
  • Also I have tried using mget* but it copies all files from all the folders but doesn't created folder structure.
  • I am unable to use TAR command as the prompt shows "invalid command".

回答1:

Windows command-line FTP client, the ftp.exe, does not support recursive directory transfers.


You have to use a 3rd party FTP client for that.

For example with WinSCP FTP client, you can use a batch-file like:

winscp.com /command ^
    "open ftp://user:password@example.com/" ^
    "get /folder/* c:\target\" ^
    "exit"

It will automatically download all files and subfolders in the /folder.

For details, see WinSCP guide to automating file transfers from FTP server. There's also a guide for converting Windows ftp.exe script to WinSCP.

(I'm the author of WinSCP)



回答2:

The target dir is a zip file. You can copy the full zip file into the ftp server using below code.

//Taking source and target directory path
string sourceDir = FilePath + "Files\\" + dsCustomer.Tables[0].Rows[i][2].ToString() + "\\ConfigurationFile\\" + dsSystems.Tables[0].Rows[j][0].ToString() + "\\XmlFile";

string targetDir = FilePath + "Files\\Customers\\" + CustomerName + "\\" + SystemName + "\\";                                                                                       
foreach (var srcPath in Directory.GetFiles(sourceDir))
{
    //Taking file name which is going to copy from the sourcefile                                              
    string result = System.IO.Path.GetFileName(srcPath);

    //If that filename exists in the target path
    if (File.Exists(targetDir + result))
    {
        //Copy file with a different name(appending "Con_" infront of the original filename)
        System.IO.File.Copy(srcPath, targetDir + "Con_" + result);
    }
    //If not existing filename
    else
    {
        //Just copy. Replace bit is false here. So there is no overwiting.
        File.Copy(srcPath, srcPath.Replace(sourceDir, targetDir), false);
    }

}



标签: windows ftp