/* Author: Regan Heath This code is public domain */ import std.file; import std.stdio; bool issep(char c) {return (c == '\\' || c == '/');} version(Windows) const char[] SEP = "\\"; version(linux) const char[] SEP = "/"; char[] makePath(char[] path, char[] file) { if (!issep(path[$-1])) { if (!issep(file[0])) return path ~ SEP ~ file; } else { if (issep(file[0])) file = file[1..$]; } return path ~ file; } void copyDir(char[] src, char[] dst, bool overwrite = false, bool recurse = false) { if (!isdir(src)) throw new FileException("Source does not exist"); if (!exists(dst)) mkdir(dst); else if (!overwrite) throw new FileException("Destination exists and overwrite not specified"); bool processEntry(char[] name) { if (isdir(makePath(src,name))) { if (!recurse) return true; //skip copyDir(makePath(src,name), makePath(dst,name), overwrite, true); return true; } copy(makePath(src,name), makePath(dst,name)); return true; } listdir(src, &processEntry); } void main(char[][] argv) { if (argv.length < 5) throw new Exception("Usage: copyDir src dst overwrite recurse"); copyDir(argv[1],argv[2],argv[3]=="true",argv[4]=="true"); }