Tuesday, November 26, 2013

FTP made easier with CFFTP - Downloading zip file using cfftp

CFFTP is a powerful feature in coldfusion which allows file operations without hassles. To start with it might throw challenges but then it's not hard to master.

To get a file from a server using FTP. Adobe docs have very detailed explanation of this tag. You can find this here.

1. Open a connection.
                This is not a necessary option but then I'd choose this way.

                  <cfftp action="open" username="webuser" connection="ftpConnection"
                   password="secret" server="testftp.com" stoponerror="Yes" secure="true"
                   timeout="300"/>

                Timeout is optional, I'd rather specify it.

2. GetFile

                Let me give an example for GetFile operation.

                Get the current path using the GetCurrentDir action. This would help us to avoid errors like file not found.

                <cfftp connection=MyFTPConnection action="GetCurrentDir" stoponerror="Yes">


               <cfftp action="getFile" connection="ftpConnection"
                   localfile="/home/data/downloadedFile.zip"
                   remotefile="#cfftp.returnvalue#/gallery/images.zip"
                   transfermode="auto" failifexists="no" timeout="300" passive="true"/>

3. Close the connection

               <cfftp action="close" connection="MyFTPConnection" stoponerror="Yes"/>


Now the required operation can be performed on that. Let me know if any of you have any questions.




Monday, November 25, 2013

Add a Date/time Column with a default date to a table in MySQL

This is again a mundane task which any one of us might have been required to do. Please find the simple DDL.

If the requirement is to alter table Users with a pwdchangedate with a default date of today, here goes the DDL

ALTER TABLE Users
    ADD pwdchangedate
    DATETIME
    NOT NULL
    DEFAULT now();


Wednesday, November 13, 2013

Find the difference between 2 dates in days in coldfusion

This is a requirement which is faced in our day to day coding life.  The  DateDiff() function is used.

Let me give an example.
 
     There's a scenario in which the last login date/time is compared against today's date/time. If the difference is more than 7 days, we send an e-mail alert to the user saying the user has not done any activity in the site.

LastLoginDate: 2013/10/01 20:10:10

noOfDays = DateDiff("d",DateFormat(LastLoginDate, "mm/dd/yyyy"),DateFormat(now(), "mm/dd/yyyy"));

noOfDays = 12.

This one did the work for me :)