Link to home
Start Free TrialLog in
Avatar of plegg
plegg

asked on

CFLOOP doesn't seem to work - why?

I'm trying to FTP a file that the user has selected to a number of directories on the FTP server.  It only sends the file to the first account.  Can anyone help here?

Here's the code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<CFPARAM name="attributes.Action"             default="Form">
<CFPARAM name="attributes.FTPSrvr"             default="ftp.hud.gov">
<CFPARAM name="attributes.Timeout"               default="1500">

<CFSWITCH expression="#attributes.Action#">

     <CFCASE value="Put">

          <CFFILE ACTION="Upload"
              FILEFIELD="ReleaseNotes"
              DESTINATION="e:\samsweb\ftproot\"
              NAMECONFLICT="overwrite"
               >

          <CFSET localfile = "e:\samsweb\ftproot\#file.serverfile#">
                   
          <CFSET FTPaccounts = StructNew()>
         
          <CFSET val = StructInsert(FTPAccounts, "one"     , "pass1" )>
          <CFSET val = StructInsert(FTPAccounts, "two"     , "pass2" )>
          <CFSET val = StructInsert(FTPaccounts, "three"     , "pass3" )>
          <CFSET val = StructInsert(FTPAccounts, "four"     , "pass4" )>
         
          <CFLOOP collection=#FTPaccounts# item="FTPUser">
               
               <CFFTP
                    connection=HUDFTP
                    timeout="#attributes.Timeout#"
                    server="#attributes.FTPSrvr#"
                    username=#FTPUser#
                    password=#StructFind(FTPaccounts, FTPUser)#
                    action="Open"
                    stoponerror="No"
                 >
               
               <!--- Can Only do all this "stuff" if connection succeeded --->
               <CFIF CFFTP.succeeded is "Yes">
                    <CFFTP
                         connection=HUDFTP
                         timeout="#attributes.Timeout#"
                         action="PutFile"
                         localfile=#localfile#
                         remotefile="#file.serverfile#"
                         transferMode="binary"
                         stoponerror="Yes"
                         >

                    <CFFTP connection=HUDFTP action="Close" stoponerror="No">
                         
               </CFIF>
     
          </CFLOOP>

          <html>
          <head>
               <title>SAMS Release Notes</title>
          </head>
         
          <body>
         
          <table width=100%>
               <tr>
                    <td align="center"><b>The Release Notes file <cfoutput>#file.serverfile#</cfoutput> was successfuly transferred to the HUD FTP site!</b></td>
               </tr>
          </table>
         
          </body>
          </html>    
                                                                 
     </CFCASE>

     <CFCASE value="Form">
     
          <html>
          <head>
               <title>SAMS Release Notes</title>
          </head>
         
          <body>
         
          <form name="relnotes" method="post" action="releasenotes.cfm" enctype="multipart/form-data">
          <input type="hidden" name="attributes.Action" value="Put">
               <table width=100%>
                    <tr>
                         <td align="right">Select the Release Notes :</td>
                         <td align="left"><input type="file" name="ReleaseNotes" size="50"></td>
                    </tr>
                    <tr>
                         <td colspan=2 align="center"><input type="Submit" name="Submit" value="Submit"></td>
                    </tr>
               </table>
          </form>
         
          </body>
          </html>    
     </CFCASE>

</CFSWITCH>                    

Avatar of substand
substand

I can't find any problems with the code, but coldfusion is wierd sometimes.  Have you tried another loop type?

Try:

<CFSET FTPaccounts = StructNew()>
<cfset accntlist="user1,user2,user3,user4">
<cfset passlist="p1,p2,p3,p4">
<cfset counter=0>
<cfloop list="#accntlist#" index="accnt">
<cfset counter=counter+1>
       
              <CFFTP
                   connection=HUDFTP
                   timeout="#attributes.Timeout#"
                   server="#attributes.FTPSrvr#"
                   username=#accnt#
                   password=#listgetat(passlist, counter)#
                   action="Open"
                   stoponerror="No"
                >
             
              <!--- Can Only do all this "stuff" if connection succeeded --->
              <CFIF CFFTP.succeeded is "Yes">
                   <CFFTP
                        connection=HUDFTP
                        timeout="#attributes.Timeout#"
                        action="PutFile"
                        localfile=#localfile#
                        remotefile="#file.serverfile#"
                        transferMode="binary"
                        stoponerror="Yes"
                        >

                   <CFFTP connection=HUDFTP action="Close" stoponerror="No">
                       
              </CFIF>
   
         </CFLOOP>


-----------------------------------------

i know its not the best way to do it, but sometimes doing it a different way helps.

hi chk on this

u have a similar variable name for
1. account username = "FTPUser"
2. the loop index = "FTPUser"

try changing the names, so it knows what to pick up when !

that shld do it - i guess !!!

let me know

K'Rgds
Anand
Avatar of plegg

ASKER

substand, I tried that but the same thing happened.  The file was only FTPd to the first account.  Any other ideas?
Avatar of plegg

ASKER

anandkp, don't they need to be the same so that correct value is looped over?
Avatar of plegg

ASKER

Hey, I figured it out.  Because the name of the connection was the same, the loop used the connection information (username, password) from the first one for all of them.  I changed the connection name, making it dynamic.

I found this on Macromedia's ColdFusion Forums.

This is right straight from the documentation in Studio.

" After you establish a connection with cfftp, you can reuse it to perform additional FTP operations. To do this, you use the connection attribute to define and name an FTP connection object that stores information about the connection. Any additional FTP operations that use the same connection name automatically make use of the information stored in the connection object. This facility helps save connection time and improves file transfer operation performance.

Thnaks for your help anyway.
wow, thats wierd.  i figured it would change the connection details as the loop changed.  ie, reopen the connection since you closed it.  

thats funny.
ok - so the change is the name worked for u ...

well the username & the loop index needs to be different - cos the username wld be different than the loopindex -that wld hvae values as 1,2,3,4 ... now i dont think the usernames wld also be as 1,2,3,4 ...

ne-ways

K'Rgds
Anand
Avatar of plegg

ASKER

anandkp, what are you talking about?  It doesn't matter that the account username and the collection index are the same.  I changed the name of the FTP connection to be #Replace(FTPUser,"-","") because it needs to change every time through the loop (see my last post).  I used replace because some of the accounts have a dash and CF doesn't accept a dash in the connection name - another thing they forgot to mention in their documentation.
No comment has been added lately, so it's time to clean up this question.
I will leave the following recommendation in the Cleanup topic area:

PAQ/Refund

Please leave any comments here within the next four days.

mrichmon
EE Cleanup Volunteer
ASKER CERTIFIED SOLUTION
Avatar of ee_ai_construct
ee_ai_construct
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial