<!--- Read data from two datasource tables. ---> 
<cfquery name="GetTable" datasource="#Application.DataSrc#">
      SELECT TABLE_SCHEMA  FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA like '%agency%' group by  TABLE_SCHEMA
</cfquery>
<cfset database=arraynew(1)>

<cfloop query="GetTable">
<cfset database[CurrentRow]= TABLE_SCHEMA>
</cfloop>

<cfloop from=1 to=#ArrayLen(database)# index=ind>
   <cfif ArrayLen(database) gt 1>
      <cfset Request.prefix_db_agency = #database[ind]# />
   </cfif>
   <cfquery name="GetpCMS4857"  datasource="#Application.DataSrc#">
      SELECT Order_ID, Assmt_ID, Status, Form_Type, Insured_HIC, Date_Admit, Certification_From, Certification_To, Pt_Agy_ID, Agy_CCN, Pt_First, Pt_Last, Pt_Street, Pt_City, Pt_State, Pt_Zip, Pt_Phone, Agy_Name, Agy_Street, Agy_City, Agy_State, Agy_Zip, Agy_Phone, Agy_NPI, Pt_DOB, Pt_Gender, Medications, ICD10, Description, ICD_Date, DME, Safety, Diet, Allergies, Functional_Limitations, Functional_Limits_Extra, Activities_Permitted, Activities_Permitted_Extra, Mental_Status, Mental_Status_Extra, Prognosis, Careplan_SN, Careplan_PT, Careplan_OT, Careplan_ST, Careplan_MSW, Careplan_HHA, Careplan_other, Goals_SN, Goals_PT, Goals_OT, Goals_ST, Goals_MSW, Goals_HHA, Goals_other, Rehabilitation_Potential, Discharge_Plan, Clinician_Signature, Render_Phys_ID, Date_Sent, Date_Received, MD_Homebound, MD_Clinical_Condition, MD_Digital_Signature, Export, filename, Date_Create, Created_by, Date_Change, Change_by  
      FROM  #Request.prefix_db_agency#.pCMS4857
      Where Export = 0 
      AND Status =  0 
   </cfquery>
  
  <cfscript> 
      //Use an absolute path for the files. ---> 
      // theDir=GetDirectoryFromPath(GetCurrentTemplatePath()); 
      theDir = "/var/www/cron/UploadFiles/";
      thefilename = #Request.prefix_db_agency#&".xls"; 
      theFile= theDir & thefilename; 
      //Create two empty ColdFusion spreadsheet objects. ---> 
      theSheet = SpreadsheetNew("pCMS4857"); 
      SpreadsheetAddRow(theSheet, 
      " Order_ID, Assmt_ID, Status, Form_Type, Insured_HIC, Date_Admit, Certification_From, Certification_To, Pt_Agy_ID, Agy_CCN, Pt_First, Pt_Last, Pt_Street, Pt_City, Pt_State, Pt_Zip, Pt_Phone, Agy_Name, Agy_Street, Agy_City, Agy_State, Agy_Zip, Agy_Phone, Agy_NPI, Pt_DOB, Pt_Gender, Medications, ICD10, Description, ICD_Date, DME, Safety, Diet, Allergies, Functional_Limitations, Functional_Limits_Extra, Activities_Permitted, Activities_Permitted_Extra, Mental_Status, Mental_Status_Extra, Prognosis, Careplan_SN, Careplan_PT, Careplan_OT, Careplan_ST, Careplan_MSW, Careplan_HHA, Careplan_other, Goals_SN, Goals_PT, Goals_OT, Goals_ST, Goals_MSW, Goals_HHA, Goals_other, Rehabilitation_Potential, Discharge_Plan, Clinician_Signature, Render_Phys_ID, Date_Sent, Date_Received, MD_Homebound, MD_Clinical_Condition, MD_Digital_Signature, Export, filename, Date_Create, Created_by, Date_Change, Change_by"); 
      SpreadsheetAddRows(theSheet,GetpCMS4857); 
  </cfscript> 
  
<!--- Write the two sheets to a single file ---> 
<cfspreadsheet action="write" filename="#theFile#" name="theSheet"  sheetname="pCMS4857" overwrite=true > 

<cfscript>
  FileSetAccessMode(#theFile#, "777");
</cfscript>

<!--- Read all or part of the file into a spreadsheet object, CSV string, HTML string, and query. 
<cfspreadsheet action="read" src="#theFile#" sheetname="pCMS4857" name="spreadsheetData"> 
<cfspreadsheet action="read" src="#theFile#" sheet=1 rows="3,4" format="csv" name="csvData"> 
<cfspreadsheet action="read" src="#theFile#" format="html" rows="5-10" name="htmlData"> 
  
<h3>First sheet row 3 read as a CSV variable</h3> 
<cfdump var="#csvData#"> 
  
<h3>Second sheet rows 5-10 read as an HTML variable</h3> 
<cfdump var="#htmlData#"> ---> 
   
            <cfset objFTPProperties = {
              Server = "34.198.162.172",
              Port = "22",
              Username = "Myhomecarebiz",
              Password = "Rl97HtU24CUy",
             fingerprint = "92:29:86:af:6d:e3:01:25:9c:20:41:f3:99:2a:72:1a",
              Secure = true
              } />

              <cfftp 
              action="open"
              connection="objConnection"
              attributeCollection="#objFTPProperties#"
              />
             <p>Successful: <cfoutput>#cfftp.succeeded#</cfoutput>
              <cfdump var="#objConnection#">
                       
            <cfftp action="putfile"  localfile="#theFile#" remotefile="/files/#thefilename#" connection="objConnection" />
            <cfftp action="close" connection="objConnection" />


</cfloop>
