Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy a directory and files

Status
Not open for further replies.

bobbyr

Programmer
Nov 30, 2000
66
US
Anybody know of a way to copy a directory and all of its subdirectories and files to a new location?

I know how to create the new directory:
<cfdirectory action=&quot;CREATE&quot; directory=&quot;C:\NewDir&quot;>

Now, the problem I'm having is how to copy C:\Downloads\ into the C:\NewDir\ directory. C:\Downloads contains subdirectories and files, which needs to be copied as well. I've tried a few different combinations of <cffile> but nothing seems to work. <cfdirectory> doesn't have a &quot;copy&quot; action, from what I can tell..



Thanks in advance!

Bobby R
 
I think you will need to do a listing on your Download directory using CFDIRECTORY ACTION=&quot;List&quot;. Name the query MyDirList for example.
Execute the following code:

<cfset thisDir = &quot;C:\downloads\&quot;>
<cfset targetDir = &quot;C:\NewDir\&quot;>

<cfdirectory action=&quot;LIST&quot;
directory=&quot;#thisDir#&quot;
name=&quot;MyDirList&quot;>
<!--- Start on row 3, the first two will be . and .. --->
<cfoutput query=&quot;MyDirList&quot; startrow=&quot;3&quot;>
<cfif Type IS &quot;File&quot;>
<cffile action=&quot;MOVE&quot;
source=&quot;#thisDir##name#&quot;
destination=&quot;#targetDir#&quot;>
</cfif>
</cfoutput>

Remember that this won't do a recursive copy, it will only move files in the C:\downloads directory, not in its subdirectories.
Good luck!


<webguru>iqof188</webguru>
 
Thanks for this addition cfhub :), recursion can be fun indeed, but very bad for performance too! Anyways, good luck Bobby.

<webguru>iqof188</webguru>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top