how can i copy folders, subfolders and their files? - coldfusion

I have one folder called misc which contains few sub folders and each sub folders has sub sub folders and files in it and I want to copy the sub folders, sub sub folders and its files in a called default folder.
I have tried this getting empty folder. And also I am wondering why Coldfusion doesn't have copy attribute in cfdirectory tag.
<cfset CurrentDirectory=GetTemplatePath()>
<cfset CurrentDirectory=ListDeleteAt(CurrentDirectory,ListLen(CurrentDirectory,"/\"),"/\")>
<cfset NewDirectory="#CurrentDirectory#\default">
<cfif NOT directoryExists(NewDirectory)>
<cfdirectory action="create" directory="#NewDirectory#">
</cfif>
<cfset strPath = ExpandPath("c:/wwwroot/test/misc") />
<cfdirectory action="list" directory="#strPath#" name="exDir">
<cfif exDir.type EQ 'File' >
<cffile action="copy" source="#strPath#" destination="#NewDirectory#" mode="777">
</cfif>
<cfif exDir.type EQ 'Dir'>
<cfdirectory action="create" directory="#NewDirectory#" mode="777">
</cfif>
<cfdump var = "#strPath#">
<cfdump var="#exDir#">
can anyone please let me know why I am getting empty folder ? any help would be appreciated!

directoryCopy() on cflib should do the job, or use cfzip

Related

Custom CFInclude for file customization

Our code base has quite a bit of the following example as we allow a lot of our base pages to be customized to our customers' individual needs.
<cfif fileExists("/custom/someFile.cfm")>
<cfinclude template="/custom/someFile.cfm" />
<cfelse>
<cfinclude template="someFile.cfm" />
</cfif>
I wanted to create a custom CF tag to boilerplate this as a simple <cf_custominclude template="someFile.cfm" />, however I ran into the fact that custom tags are effectively blackboxes, so they aren't pulling in local variables that exist prior to the start of the tag, and I can't reference any variable that was created as a result of the tag from importing the file.
E.G.
<!--- This is able to use someVar --->
<!--- Pulls in some variable named "steve" --->
<cfinclude template="someFile.cfm" />
<cfdump var="#steve#" /> <!--- This is valid, however... --->
<!--- someVar is undefined for this --->
<!--- Pulls in steve2 --->
<cf_custominclude template="someFile.cfm" />
<cfdump var="#steve2#" /> <!--- This isn't valid as steve2 is undefined. --->
Is there a means around this, or should I utilize some other language feature to accomplish my goal?
Well, I question doing this at all but I know we all get handed code at times we have to deal with and the struggle it is to get people to refactor.
This should do what you are wanting. One important thing to note is that you will need to ensure your custom tag has a closing or it won't work! Just use the simplified closing, so like you had it above:
<cf_custominclude template="someFile.cfm" />
This should do the trick, called it has you had it : custominclude.cfm
<!--- executes at start of tag --->
<cfif thisTag.executionMode eq 'Start'>
<!--- store a list of keys we don't want to copy, prior to including template --->
<cfset thisTag.currentKeys = structKeyList(variables)>
<!--- control var to see if we even should bother copying scopes --->
<cfset thisTag.includedTemplate = false>
<!--- standard include here --->
<cfif fileExists(expandPath(attributes.template))>
<cfinclude template="#attributes.template#">
<!--- set control var / flag to copy scopes at close of tag --->
<cfset thisTag.includedTemplate = true>
</cfif>
</cfif>
<!--- executes at closing of tag --->
<cfif thisTag.executionMode eq 'End'>
<!--- if control var / flag set to copy scopes --->
<cfif thisTag.includedTemplate>
<!--- only copy vars created in the included page --->
<cfloop list="#structKeyList(variables)#" index="var">
<cfif not listFindNoCase(thisTag.currentKeys, var)>
<!--- copy from include into caller scope --->
<cfset caller[var] = variables[var]>
</cfif>
</cfloop>
</cfif>
</cfif>
I tested it and it works fine, should work fine being nested as well. Good luck!
<!--- Pulls in steve2 var from include --->
<cf_custominclude template="someFile.cfm" />
<cfdump var="#steve2#" /> <!--- works! --->

How to output the list of files from a directory that has multiple folders?

I would like to ouput the name of the folder and then the files in each folder,by grouping them.
folder1/folder2 are subfolders to test folder.
folder1 =>
-test.doc
-test2.doc
-test33.doc
folder2 =>
-test3.doc
-test4.doc
<cfdirectory directory="C:\wwwroot\test" recurse="yes" sort="type asc" type="all" filter="*" name="myList2">
<cfoutput query="myList2" group="type" >
#name#<br>
<cfoutput>
#name#<br>
</cfoutput>
</cfoutput>
You're close. Change the cfoutput to group by "directory". Within the nested cfoutput, only display files.
<cfoutput query="myList2" group="Directory">
<strong>#Directory#</strong><br>
<cfoutput>
<cfif type eq "file">
#name#<br>
</cfif>
</cfoutput>
</cfoutput>

Search Folders in ColdFusion 9

I am trying to create a search method to attach files on my outbound emails. I need to search inside a folder and find all the files that begin with a certain character and then attach them to the email. I just need a head start on how you could create such a search method so any pointers or links to references will be appreciated.
This is What I have so far but it does not seem to work correct when I use a path instead of GetBaseTemplatePath()
<cfscript>
attributes.attachments = 2011093475839213;
</cfscript>
<cfset Directory = "E:\sites\Example.com\FileFolder\#attributes.attachments#">
<cfset CurrentDirectory=Directory>
<cfset CurrentDirectory=ListDeleteAt(CurrentDirectory,ListLen(CurrentDirectory,"/\"),"/\")>
<cfoutput>
<b>Current Directory:</b> #CurrentDirectory#
<br />
</cfoutput>
<cfdirectory action="list" directory="#CurrentDirectory#" name="result">
<cfdump var="#result#">
When I change the code slightly by
<cfset CurrentDirectory=GetBaseTemplatePath()>
My code works and I get the list of all the files in my current directory. Do I have a mistake on my path that I cannot see?
This is my CFMAIL part which I do have an issue with.
When I dump my #result# query I get all the files inside the folder. Then I get this error:
The resource 2011093475839213.pdf was not found.
The root cause was: ''.
I do receive an email despite the error, just not the attached files.
<!--- Email Test --->
<CFMAIL FROM="user1#example.com" TO="user2#example.com" SUBJECT="Test" type="HTML">
<P> This is the attachment test</P>
<p> For this test to be successful, we need to receive some file attachments with this email</p>
<cfloop query="result">
<cfmailparam file="#result.name#" disposition="attachment">
</cfloop>
</cfmail>
<!--- Email Test Ends --->
The cfdirectory tag will allow you to search through folder(s) with a specific pattern. Using the query it returns, you can loop over it and attach all the files you need to the email.
http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7f99.html
Something like this:
<cfdirectory action="list" directory="#myDirectory#" name="myDir">
<cfmail subject="My Subject" to="yourAddress" from="myAddress">
My Message
<cfsilent>
<cfloop query="myDir">
<cfif Left(myDir.name,1) eq "Z">
<cfmailparam file="#myDirectory & myDir.name#">
</cfif>
</cfloop>
</cfsilent>
</cfmail>

Coldfusion - cannot access templ folder when creating thumb

I am getting a problem with the following line:
<cfimage action="read" name="myImage" source="#ExpandPath("../../banner/#upload.clientfile#")#" />
I suspect it is because I am using a shared host (CF9) and do not have access to the folder. The error I get is "unable to create temporary file". My temp directory is home/kloxo/temp/wwwroot-tmp. Can I specify another temp folder or do I have to get my hosting company to sort this?
<cfapplication sessionmanagement="true">
<cfoutput>#GetTempDirectory()#</cfoutput>
<cfif IsDefined ("FORM")>
<cfif structKeyExists(form, "uploadfile")>
<cfset destination = expandPath("../../banner")>
<cfif not directoryExists(destination)>
<cfdirectory action="create" directory="#destination#">
</cfif>
<cffile action="upload" filefield="uploadfile" destination="#destination#" nameConflict="makeUnique" result="upload">
<cfdump var="#upload.clientfile#">
<cfimage action="read" name="myImage" source="#ExpandPath("../../banner/#upload.clientfile#")#" />
</cfif>
It appears to be a configuration issue. Contact your host. Here is a page with more information:
http://forums.adobe.com/message/3060530

What is the safest way in ColdFusion to delete a folder and it's contents?

I am finishing creating a file upload utility for our site, and if the upload is an invalid format (per our specs not worth going over here) I would want to delete the folder the zip file was unzipped to, and all it's contents.
So far I have used a method of creating a dynamic batch file like this:
<!--- check if folder exists before starting to delete --->
<cfif directoryexists("#file_path_course#")>
<!--- this can be passed in a varaible or whatever --->
<cfset tDirectory = "#file_path_course#">
<!--- This is what we will put in the bat file --->
<cfset tString ="RMDIR /S /Q " & tDirectory>
<!--- generate a .BAT file for later execution --->
<cffile action="WRITE" file="#file_path_course#\delete.bat" output="#tString#">
<!--- Now execute the file to delete everything (Folder and all sub-folders and files)--->
<cfexecute name="#file_path_course#\delete.bat" timeout="60"></cfexecute>
<!--- check if bat file exists --->
<cfif fileexists("#file_path_course#\delete.bat")>
<!--- now delete the bat file --->
<cffile action="DELETE" file="#file_path_course#\delete.bat">
</cfif>
<!--- delete course folder --->
<cfdirectory action="delete" directory="#file_path_course#" recurse="yes">
<cfset course_files_deleted = "Yes">
</cfif>
But I am admittedly concerned about the allowed usage of the cfexecute tag.
There is another option, which uses the cfdirectory recurse delete option, which will do all I ask, but I want to be very sure it's not going to delete the folders/files outside the folder I point it to.
There is a 3rd way, which involves a cfdirectory and looping around it, but I also like the idea of using less lines of code to do a simple operation.
Which option do you trust the most?
I am running IIS7, Coldfusion 8.
Why not just use cfdirectory? You said you were worried that it would delete stuff "outside" the folder you specified. It won't. Simple as that. If it did, then the tag would be broken. :)
Instead of writing a batch file and then executing it, I let ColdFusion do all the work.
<cfset targetDirectory = "C:\Websites\site\thisFolder" />
<cfif directoryExists(targetDirectory)>
<cfdirectory action="list" directory="#targetDirectory#" listInfo="" name="theseFiles" recurse="true" type="file" />
<cfif theseFiles.recordcount gt 0>
<cfloop query="theseFiles">
<cffile action="delete" file="#targetDirectory#/#theseFiles.name#" />
</cfloop>
</cfif>
<cfdirectory action="delete" directory="#uploadDirectory#/#allFolders.name#" />
</cfif>
what i would do is upload the file to a temp directory outside of the webroot. you can use gettempdirectory() to accomplish this which uses your system's temp directory (c:\windows\temp for windows)
then you can unzip the file into a subdirectory off of the temp directory and perform some security checks against the unzipped files and make sure everything is ok, all the while not opening up your site to any attacks. if everything pans out, you can then move the files to their final resting place. if not, just use cfdirectory (as cfjedimaster pointed out) to remove the subdirectory and all the files.