Issues with CFFTP and recursive directories

570 Views Asked by At

I have searched the archives and I cannot find the answer to this question. Basically, what I am doing is FTP'ing several folders and files to a remote server. The code I have now will create the root folder, and transfer the 11 files that are in the root as well as create all of the folders under the root. What is not happening is the files for each folder are not being transferred. In addition, the 11 files in the root folder get resent each time a folder is being created. I am attaching my code with comments.

    <!--- Open an FTP connection --->
        <cfftp action = "open"
            connection = "MyConnection"
            username = "#getSiteList.TMS_USERNAME#"
            password = "#getSiteList.TMS_PASSWORD#"
            server = "#getSiteList.TMS_FTPADDRESS#"
            secure = "no"
            stopOnError = "yes">

        <!--- If an FTP connection has been made --->
        <cfif cfftp.Succeeded EQ "YES">

        <!--- FTP folder contents --->
            <cfset folderName = ListLast(#request.FileURL#, "\\")>
            <cfset local_path = "#request.FileURL#">
            <cfset remote_path = "#folderName##getSiteList.TMS_DIRECTORY#">

            <!--- Loop through the number of folders --->

          <cfloop index="i" from="1" to="#NumberOfPages#"> 

            <cfscript>
                KeywordStart=getPageBreaks.TMP_PAGESTART[i];
                if (i eq NumberOfPages)
                {
                    KeywordEnd=getAllKeywords.RecordCount;
                }
                else
                {
                    KeywordEnd=getPageBreaks.TMP_PAGESTART[i+1]-1;
                }
                PageKeywordStart=Trim(getAllKeywords.TML_KEYWORD[KeywordStart]);
                PageKeywordRange=PageKeywordStart & " to " & Trim(getAllKeywords.TML_KEYWORD[KeywordEnd]);
                KeyFolder=MakeFolderTitle(PageKeywordStart);
                request.thisFolder=KeyFolder;
                create_remote_folder=KeyFolder;
            </cfscript>


        <!--- Add slash to end of local path if necessary --->
        <cfif Right(local_path, 1) NEQ "\">
            <cfset local_path = local_path & "\">
        </cfif>

        <!--- Add slash to end of remote path if necessary --->
        <cfif Right(remote_path, 1) NEQ "/">
            <cfset remote_path = remote_path & "/">
        </cfif>

        <!--- Ensure that the root folder is there. --->
        <cfftp action="existsDir"
             connection="MyConnection"
             directory="#remote_path#/#folderName#"
             stopOnError = "no">

        <!--- If it doesn't exist create it --->
        <cfif cfftp.succeeded NEQ "YES">

            <!--- Create directory --->

            <cfftp action="createDir"
                 connection="MyConnection"
                 directory="#folderName#"
                 stopOnError = "no">
          </cfif> 

        <!--- Get the local directory contents --->

         <cfdirectory directory="#local_path#" name="local_dir_contents" action="list">


        <!--- Check if create_remote_folder exists --->
        <cfftp action="existsDir"
             connection="MyConnection"
             directory="#remote_path##create_remote_folder#"
             stopOnError = "Yes">
              <cfif cfftp.Succeeded NEQ "YES">
                 <cfoutput>
                    The following error occured: #cfftp.ErrorCode# : #cfftp.ErrorText#
                 </cfoutput>
                 </cfif>

        <!--- If it doesn't exist create it --->
        <cfif cfftp.succeeded NEQ "YES">
            <!--- Create directory --->

            <cfftp action="createDir"
                 connection="MyConnection"
                 directory="#remote_path##create_remote_folder#"
                 stopOnError = "Yes">
                  <cfif cfftp.Succeeded NEQ "YES">
                 <cfoutput>
                    The following error occured: #cfftp.ErrorCode# : #cfftp.ErrorText#
                 </cfoutput>
                 </cfif>
           </cfif>

        <!--- Loop through contents and put the file on the SFTP server --->
        <cfloop query="local_dir_contents" startrow="1" endrow="20">

            <cfif local_dir_contents.type EQ "File">
                <!--- Put the file on the server --->
                <cfftp action="putFile"
                     connection="MyConnection"
                     localFile="#local_path##local_dir_contents.name#"
                     remoteFile="#remote_path##local_dir_contents.name#"
                     stoponerror="yes">
                      <cfif cfftp.Succeeded NEQ "YES">
                 <cfoutput>
                    The following error occured: #cfftp.ErrorCode# : #cfftp.ErrorText#
                 </cfoutput>
                 </cfif>
            <cfelse>

                <!--- Recursively call THIS function with the new directory --->
                <cfset folderName = ListLast(#request.FileURL#, "\\")>
                <cfset local_path = "#request.FileURL#">
                <cfset remote_path = "#folderName##getSiteList.TMS_DIRECTORY#">
                <cfscript>
                    KeywordStart=getPageBreaks.TMP_PAGESTART[i];
                    if (i eq NumberOfPages)
                    {
                        KeywordEnd=getAllKeywords.RecordCount;
                    }
                    else
                    {
                        KeywordEnd=getPageBreaks.TMP_PAGESTART[i+1]-1;
                    }
                    PageKeywordStart=Trim(getAllKeywords.TML_KEYWORD[KeywordStart]);
                    PageKeywordRange=PageKeywordStart & " to " & Trim(getAllKeywords.TML_KEYWORD[KeywordEnd]);
                    KeyFolder=MakeFolderTitle(PageKeywordStart);
                    request.thisFolder=KeyFolder;
                    create_remote_folder=KeyFolder;
                </cfscript>

                <cfftp action="createDir"
                 connection="MyConnection"
                 directory="#remote_path##create_remote_folder#"
                 stopOnError = "Yes">

                 <cfif cfftp.Succeeded NEQ "YES">
                 <cfoutput>
                    The following error occured: #cfftp.ErrorCode# : #cfftp.ErrorText#
                 </cfoutput>
                 </cfif>
            </cfif>
    </cfloop>   
   </cfloop> 
    <cfftp action="close" connection="MyConnection">

</cfif>
1

There are 1 best solutions below

0
On

The following options worked for me

cfftp(connection="cn",action="RemoveDir",recurse=true,passive="true",name="cfftp",directory="[dir location]");