Using CFSchedule to write to a log file - coldfusion

I am having a hard time figuring out how to use cfschedule and hope someone can help get me unstuck.
I can schedule a task fine. The Admin portal indicates that the task has run, but I don't see that anything has happened.
<cfschedule
action="update"
task="update_printer_status"
operation="HTTPRequest"
startDate="03/26/2018"
startTime="12:00 PM"
url="http://localhost:8000/source/update_status.cfm"
interval="1800" />
For testing purposes, I am just trying to get it to save the date/time it runs into a log file. But the log file is empty. Here's my log file.
<!--- Read in existing contents of file --->
<cffile
action="read"
file = "c:\temp\update_log.txt"
variable = "file_contents"
>
<!--- Set additional content --->
<cfset br = chr(13) & chr(10) />
<cfset date="#DateFormat(Now())#, #TimeFormat(Now())#">
<!--- Write string back to log file --->
<cfset update_string="#file_contents##br#Update initiated: #date#">
<cfscript>
FileWrite("update_string", "#update_string#");
ustr = FileRead("update_string");
FileWrite("c:\temp\update_log.txt", "#ustr#");
</cfscript>
Can anyone tell me what I'm doing wrong here?

My issue is that I had saved my module in a protected directly. Since the context is not logged, the administrator was not seeing it.
To resolve, I just moved my file to a public directory.

Related

ColdFusion Write File

I'm reading a simple file of logset.txt which only contains
Login:,Pass:
File is stored on webroot.
Reading it, and if no data after :. I want user to set their login data and store it.
So they would enter on a form their login and password.
So enter Bob and Turkey
And then I want it to Overwrite the logset.txt file as: Login:Bob,Pass:Turkey
I am trying this with no luck.
<cfset nf = "Login:Bob,Pass:Turkey">
<CFFILE ACTION="Write"
FILE="logset.txt"
nameConflict="overwrite"
OUTPUT="#nf#">
Is this a path issue? I have tried "/logset.txt" and "\logset.txt"
Ignore.
Path Problem This works.
<cfset fpath = expandpath('\')>
<CFFILE ACTION="Write"
FILE="#FPath#logset.txt"
nameConflict="overwrite"
OUTPUT="#nf#">
Now anyone got an idea of how to write to a mobile device local?

CFFILE creating a file with APPEND or WRITE

I'm having an issue with a coldfusion11 website, under certain conditions my attempt to create and send a report file is failing, apparently due to permission issues. I can't recreate the issue on in my test/dev environment so I need to understand what is happening for a live fix, I can't just start arbitrarily changing code on production. The current code creates the file using an APPEND action like this:
<cfset f_dir = EXCEL_PATH >
<cfset f_name = CreateUUID() & ".csv">
<cffile action="APPEND" file="#f_dir##f_name#" output="My Report "
addnewline="Yes">
<cffile action="APPEND" file="#f_dir##f_name#"
output="Title,#attributes.title#" addnewline="Yes">
Elsewhere in the code are similar functions that use WRITE as the first cffile action, like this:
<cfset f_dir = EXCEL_PATH >
<cfset f_name = CreateUUID() & ".csv">
<cffile action="WRITE" file="#f_dir##f_name#" output="My Report "
addnewline="Yes">
<cffile action="APPEND" file="#f_dir##f_name#"
output="Title,#attributes.title#" addnewline="Yes">
The code that uses WRITE first is not failing, this leads to my question:
Is there a difference between creating a file with action="APPEND" and action="WRITE" in coldfusion 11?
Ron - Write is "create" - it makes a new file. Append is used to add data to an existing file on the disk. They are different actions.
Try modifying your code as follows:
<cflock name="#f_name#">
<cfif NOT fileexists(f_dir & f_name)>
... do your WRITE action>
</cfif>
</cflock>
<cflock name="#f_name#">
.... do your APPEND action - you can be sure your file exists at this point.
</cflock>
Note, I usually use a named lock to serialize these two actions. Sometimes your code trips over file handles not quite released. Not typical but if your disk is ever thrashing it can happen.

Excluding items from a list in coldfusion by type

Is there a way to exclude certain items by filetype in a list in Coldfusion?
Background: I just integrated a compression tool into an existing application and ran into the problem of the person's prior code would automatically grab the file from the upload destination on the server and push it to the Network Attached Storage. The aim now is to stop their NAS migration code from moving all files to the NAS, only those which are not PDF's. What I want to do is loop through their variable that stores the names of the files uploaded, and exclude the pdf's from the list then pass the list onto the NAS code, so all non pdf's are moved and all pdf's uploaded remain on the server. Working with their code is a challenge as no one commented or documented anything and I've been trying several approaches.
<cffile action="upload" destination= "c:\uploads\" result="myfiles" nameconflict="makeunique" >
<cfset fileSys = CreateObject('component','cfc.FileManagement')>
<cfif Len(get.realec_transactionid)>
<cfset internalOnly=1 >
</cfif>
**This line below is what I want to loop through and exclude file names
with pdf extensions **
<cfset uploadedfilenames='#myfiles.clientFile#' >
<CFSET a_insert_time = #TimeFormat(Now(), "HH:mm:ss")#>
<CFSET a_insert_date = #DateFormat(Now(), "mm-dd-yyyy")#>
**This line calls their method from another cfc that has all the file
migration methods.**
<cfset new_file_name = #fileSys.MoveFromUploads(uploadedfilenames)#>
**Once it moves the file to the NAS, it inserts the file info into the
DB table here**
<cfquery name="addFile" datasource="#request.dsn#">
INSERT INTO upload_many (title_id, fileDate, filetime, fileupload)
VALUES('#get.title_id#', '#dateTimeStamp#', '#a_insert_time#', '#new_file_name#')
</cfquery>
<cfelse>
<cffile action="upload" destination= #ExpandPath("./uploaded_files/zip.txt")# nameconflict="overwrite" >
</cfif>
Update 6/18
Trying the recommended code helps with the issue of sorting out filetypes when tested outside of the application, but anytime its integrated into the application to operate on the variable uploadedfilenames the rest of the application fails and the multi-file upload module just throws a status 500 error and no errors are reported in the CF logs. I've found that simply trying to run a cfloop on another variable not related to anything in the code still causes it to error.
As per my understanding, you want to filter-out file names with a specific file type/extension (ex: pdf) from the main list uploadedfilenames. This is one of the easiest ways:
<cfset lFileNames = "C:\myfiles\proj\icon-img-12.png,C:\myfiles\proj\sample-file.ppt,C:\myfiles\proj\fin-doc1.docx,C:\myfiles\proj\fin-doc2.pdf,C:\myfiles\proj\invoice-temp.docx,C:\myfiles\proj\invoice-final.pdf" />
<cfset lResultList = "" />
<cfset fileExtToExclude = "pdf" />
<cfloop list="#lFileNames#" index="fileItem" delimiters=",">
<cfif ListLast(ListLast(fileItem,'\'),'.') NEQ fileExtToExclude>
<cfset lResultList = ListAppend(lResultList,"#fileItem#") />
</cfif>
</cfloop>
Using only List Function provided by ColdFusion this is easily done, you can test and try the code here. I would recommend you to wrap this code around a function for easy handling. Another way to do it would be to use some complex regular expression on the list (if you're looking for a more general solution, outside the context of ColdFusion).
Now, applying the solution to your problem:
<cfset uploadedfilenames='#myfiles.clientFile#' >
<cfset lResultList = "" />
<cfset fileExtToExclude = "pdf" />
<cfloop list="#uploadedfilenames#" index="fileItem" delimiters=",">
<cfif ListLast(ListLast(fileItem,'\'),'.') NEQ fileExtToExclude>
<cfset lResultList = ListAppend(lResultList,fileItem) />
</cfif>
</cfloop>
<cfset uploadedfilenames = lResultList />
<!--- rest of your code continues --->
The result list lResultList is copied to the original variable uploadedfilenames.
I hope I'm not misunderstanding the question, but why don't you just wrap all of that in an if-statement that reads the full file name? Whether the files are coming one by one or through a delimited list, it should be easy to work around.
<cfif !listContains(ListName, '.pdf')>
OR
<cfif FileName does not contain '.pdf'>
then
all the code you posted

How to deliver large file with Coldfusion 8?

I am using Coldfusion 8 and I am trying to serve a file of 15mo with cf_content. The problem is that the download freezes randomly. At the moment, I only tried locally, therefore the network is not the problem. I have tried with smaller files and freezes happen less often. I have no idea of the root of the problem. Here is my coldfusion code:
<cfheader name="Content-Disposition" value="attachment; filename=test.zip">
<cfcontent type="application/zip" file="C:\Test.zip" deletefile="no">
I tried to download the file with Chrome, IE and with a piece of java code to download the file (freeze on the read method after some iteration).
Do you have any idea of how I can easily stream a file using Coldfusion? Maybe it is possible using a Java Custom tags but how to write bytes to page as the custom tag write method of the Response object only allows to write a String?
I did this for a client. I am gathering a number of documents and zipping them for download. Rather than stream them, I save the zip file on the server:
<cfzip action="zip" file="#expandpath('/data/briefcase/')##session.order_id#.zip" source="#expandpath('/data/briefcase/')##session.order_id#" overwrite="yes" storepath="no">
Then I provide the user a link to download the file. That way, if it fails, they can always try again.
I then wrote a scheduled task that runs every day and delete any zip files more than 24 hours old.
<cfdirectory action="list" directory="#expandpath('/data/briefcase/')#" name="filelist" >
<cfquery name="filter_file" dbtype="query" >
SELECT * from filelist WHERE datelastmodified < #dateadd("h", -48, now())# AND type = 'File'
</cfquery>
<cfquery name="filter_dir" dbtype="query" >
SELECT * from filelist WHERE datelastmodified < #dateadd("h", -48, now())# AND type = 'Dir'
</cfquery>
<cfset path = expandpath('/data/briefcase/')>
<cfoutput query="filter_file">
<cfif fileexists('#directory#/#name#')>
<cffile action="delete" file="#directory#/#name#" >
</cfif>
</cfoutput>
<cfoutput query="filter_dir">
<cfif directoryexists('#directory#/#name#')>
<cfdirectory action="delete" directory="#directory#/#name#" recurse="true" >
</cfif>
</cfoutput>
See if helps to prepend your code with:
<cfheader name="Content-Length" value="#GetFileInfo('C:\Test.zip').size#">
That tells the browser how much data to expect.

Coldfusion GetHttpRequestData()?

Does anyone have an example of how Coldfusion's GetHttpRequestData() works? I'm looking to use this func to save data from the AJAX Upload script: http://valums.com/ajax-upload/
The script works in FireFox but not Safari, Chrome, etc...
Ideas?
What error do you get?
Maybe these links will help:
http://www.coldfusionjedi.com/index.cfm/2007/7/1/Undocumented-change-to-GetHTTPRequestData-in-ColdFusion-8
http://www.bennadel.com/blog/1602-GetHTTPRequestData-Breaks-The-SOAP-Request-Response-Cycle-In-ColdFusion.htm
You might also want to read this recent thread about that script. As valums suggested, you should be able to extract the binary data from getHttpRequestData().content (when needed).
In my very limited tests, it seemed to work okay with IE8/FF/Chrome/Opera. However, I had no luck with Safari (windows). It seemed like the request data was getting mangled (or possibly misinterpreted by CF?). So the final content-type header reported by CF was incorrect, causing an http 500 error. Granted, I did not test this extensively.
Here is my quick and dirty test script (lame by design...)
<cfset uploadError = "" />
<cfif structKeyExists(FORM, "qqFile")>
<!--- upload as normal --->
<cffile action="upload" filefield="qqFile" destination="c:/temp" />
<cfelseif structKeyExists(URL, "qqFile")>
<!--- save raw content. DON'T do this on a prod server! --->
<!--- add security checks, etc... --->
<cfset FileWrite( "c:/temp/"& url.qqFile, getHttpRequestData().content) />
<cfelse>
<!--- something is missing ...--->
<cfset uploadError = "no file detected" />
</cfif>
<!--- return status old fashioned way (for compatibility) --->
<cfif not len(uploadError)>
{"success": true}
<cfelse>
<cfoutput>{error": "#uploadError#"}</cfoutput>
</cfif>
You want to look into using cffile with action="upload" to upload the file: http://cfdocs.org/cffile
GetHttpRequestData() is intended for decoding protocols like SOAP, XML-RPC, and some of the more complex REST-ful protocols. HTTP file uploads are normally done as POSTs using the multipart/form-data MIME type. Looking at http://www.cfquickdocs.com/it doesn't appear that GetHttpRequestData() has any special support for multipart data, which means you'd have to split and decode the parts yourself. Not my idea of fun, and completely unnecessary if you're just doing file uploading.
<cffile action="upload"> or <cffile action="uploadAll"> (new for CF9) should be quite sufficient for processing file uploads, even for those done via an AJAX upload script.