How To The Write `directoryCopy( )` Function In ColdFusion?
Description:-
This is used to copy the contents of a directory to a destination directory on disk or in memory if you have the required permissions to run this function.
directoryCopy( source, destination, recurse, filter );
Attributes:-
source:- This is a required attribute, and the name of the source directory from where contents are going to be copied, This will be an absolute on-disk or in-memory location. Alternatively, you can specify the IP address as in the bellow example:directoryRename( "//192.168.0.1/new_disc/test", "" );
destination:- This is a required attribute, and the name of the destination directory to where contents are going to be copied, This will be an absolute on-disk or in-memory location. Alternatively, you can specify the IP address as in the bellow example:directoryRename( "//192.168.0.1/new_disc/test1", "" );
recurse:- This is a non-required attribute, and If it is set true then it copies the subdirectories, otherwise only the files in the source directory. The default value is false.
filter:- This is a non-required attribute, File extension filter applied to the listed files, for example, *.cfm. Filter to be used to filter the data copied: – A string that uses “*” as a wildcard, for example, “*.cfm” – a UDF (User defined Function) using the following pattern “functio_name(String path): boolean”, the function is run for every single file, if the function returns true, then the file is will be added to the list otherwise it will be omitted.
Example:-
In this example, we will copy d:\files\documents this directory contents to the d:\files\tests directory only. also, check directoryList( ) this function verifies if the copy process is complete or not.
<cfscript>
writeOutput( "Before Copy:<br>" );
writeOutput( directoryList( "d:\files\tests", false, "name" ) );
directoryCopy( "d:\files\documents", "d:\files\tests", recurse, filter );
writeOutput( "<br>" );
writeOutput( "After Copy:<br>" );
writeOutput( directoryList( "d:\files\tests", false, "name" ) );
</cfscript>
Result:-
Before Copy:
array
After Copy:
array
1 new.jpg
2 icon.gif
3 test.txt
In our d:\files\tests directory we copied some files from the d:\files\documents directory, which you can see in the result.
