Difference between revisions of "Copy a directory"

From CodeCodex

(Ruby)
(Implementations)
 
Line 31: Line 31:
 
</pre>
 
</pre>
  
 +
=== Tcl ===
 +
<pre class="tcl">
 +
file copy -force dir1 dir2
 +
</pre>
 
[[Category:Java]]
 
[[Category:Java]]
 
[[Category:Ruby]]
 
[[Category:Ruby]]
 +
[[Category:Tcl]]
 
[[Category:I/O]]
 
[[Category:I/O]]

Latest revision as of 17:44, 24 February 2011

Implementations[edit]

Java[edit]

    // Copies all files under srcDir to dstDir.
    // If dstDir does not exist, it will be created.
    public void copyDirectory(File srcDir, File dstDir) throws IOException {
        if (srcDir.isDirectory()) {
            if (!dstDir.exists()) {
                dstDir.mkdir();
            }
    
            String[] children = srcDir.list();
            for (int i=0; i<children.length; i++) {
                copyDirectory(new File(srcDir, children[i]),
                                     new File(dstDir, children[i]));
            }
        } else {
            // This method is implemented in e1071 Copying a File
            copyFile(srcDir, dstDir);
        }
    }

Ruby[edit]

Copies a file system entry src to dest. If src is a directory, this method copies its contents recursively.

require 'fileutils'

FileUtils.copy_entry src, dest

Tcl[edit]

file copy -force dir1 dir2