{"id":7900,"date":"2012-09-21T19:43:31","date_gmt":"2012-09-21T14:13:31","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=7900"},"modified":"2016-11-29T16:44:36","modified_gmt":"2016-11-29T11:14:36","slug":"copy-filesfolders-from-one-location-to-another-in-groovy","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/copy-filesfolders-from-one-location-to-another-in-groovy\/","title":{"rendered":"Copy Files\/Folders from one location to another in Groovy"},"content":{"rendered":"<p>It is normally difficult to copy file\/folders from one location to another. In my recent <a title=\"Grails Development\" href=\"http:\/\/www.tothenew.com\/grails-application-development\" target=\"_blank\">Grails Development<\/a>\u00a0project, i successfully achieved this. I had to copy files\/folders from one location to another on file system. I searched about it and came across a cool method to copy files or directories from one location to another.<\/p>\n<p>We can simply copy files, directories, exclude particular files as we have good control in it. Below is the code which explains how to use it.<\/p>\n<p><strong>Copy directory to another directory :-<\/strong><\/p>\n<p>[java]<\/p>\n<p>        String sourceDir = SOURCE_DIR_PATH<br \/>\n        String destinationDir = DESTINATION_DIR_PATH<br \/>\n        new AntBuilder().copy(todir: destinationDir) {<br \/>\n            fileset(dir: sourceDir)<br \/>\n        }<\/p>\n<p>[\/java]<\/p>\n<p>This method will copy the source directory to the destination directory with all the internal data in the same structure.<\/p>\n<p>Suppose if we want to exclude some particular file(s), then we can easily provide that file name or the common pattern of those files as mentioned in the example.<\/p>\n<p><strong>Copy directory with excluding some files :-<\/strong><\/p>\n<p>[java]<br \/>\n      String sourceDir = SOURCE_DIR_PATH<br \/>\n      String destinationDir = DESTINATION_DIR_PATH<\/p>\n<p>      new AntBuilder().copy(todir: destinationDir) {<br \/>\n           fileset(dir : sourceDir) {<br \/>\n                 exclude(name:&quot;*.java&quot;)<br \/>\n      }<br \/>\n   }<\/p>\n<p>[\/java]<\/p>\n<p>The above code will not copy the files which have an extension <strong>.java<\/strong><\/p>\n<p>Now if i want to copy any particular file, then there is other method to copy it as given below.<\/p>\n<p><strong>Copy files from one directory to another :-<\/strong><\/p>\n<p>[java]<\/p>\n<p>        String sourceFilePath = SOURCE_FILE_PATH<br \/>\n         String destinationFilePath = DESTINATION_FILE_PATH<br \/>\n         (new AntBuilder()).copy(file: sourceFilePath, tofile: destinationFilePath)<\/p>\n<p>[\/java]<\/p>\n<p>This method will copy the provided file to the destination location.<\/p>\n<p>This worked for me. Hope it helps.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It is normally difficult to copy file\/folders from one location to another. In my recent Grails Development\u00a0project, i successfully achieved this. I had to copy files\/folders from one location to another on file system. I searched about it and came across a cool method to copy files or directories from one location to another. We [&hellip;]<\/p>\n","protected":false},"author":19,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":277},"categories":[7,3917,1994],"tags":[4840,9],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/7900"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/19"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=7900"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/7900\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=7900"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=7900"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=7900"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}