Getting Started Automating ExcelĪre you new to VBA and not sure where to begin? Check out my quickstart guide to learning VBA. The code below is microsoft suggestion how-to-copy-directoriesĪnd it is shared by dear it just copies sub directories and files of source folder recursively and doesn't copy the source folder it self (like right click -> copy ).īut there is a tricky way below this answer : private static void Director圜opy(string sourceDirName, string destDirName, bool copySubDirs = true)ĭirector圜opy(subdir.Now that you’ve found some VBA code that could potentially solve your Excel automation problem, what do you do with it? If you don’t necessarily want to learn how to code VBA and are just looking for the fastest way to implement this code into your spreadsheet, I wrote an article (with video) that explains how to get the VBA code you’ve found running on your spreadsheet. String temppath = Path.Combine(destDirName, subdir.Name) ĭirector圜opy(subdir.FullName, temppath, copySubDirs) If copying subdirectories, copy them and their contents to new location. String temppath = Path.Combine(destDirName, file.Name) Get the files in the directory and copy them to the new location. If the destination directory doesn't exist, create it. "Source directory does not exist or could not be found: "ĭirectoryInfo dirs = dir.GetDirectories() Get the subdirectories for the specified directory.ĭirectoryInfo dir = new DirectoryInfo(sourceDirName) Private static void Director圜opy(string sourceDirName, string destDirName, bool copySubDirs) Copy from the current directory, include subdirectories. You can always use this, taken from Microsofts website. SourceFile.CopyTo(Path.Combine(target.FullName, sourceFile.Name), overwiteFiles)) Parallel.ForEach(source.GetFiles(), sourceFile => Parallel.ForEach(source.GetDirectories(), (sourceChildDirectory) =>ĬopyEntireDirectory(sourceChildDirectory, new DirectoryInfo(Path.Combine(target.FullName, sourceChildDirectory.Name)))) public static void CopyEntireDirectory(DirectoryInfo source, DirectoryInfo target, bool overwiteFiles = true) String output = Regex.Replace(fileName, "^" + Regex.Escape(path), newPath) ĭirectory.CreateDirectory(Path.GetDirectoryName(output)) ĮDIT: Modifying Sabry to full parallel foreach does produce a better result, however the code uses recursive function and its not ideal in some situation. Parallel.ForEach(Directory.GetFileSystemEntries(path, "*", SearchOption.AllDirectories) public static void CopyEntireDirectory(string path, string newPath) My solution is basically a modification of answer, however I have enhanced it a bit and it appears to be more than 5 times faster than the accepted answer. String multiDest = StringArrayToMultiString(dest) įileOpStruct.pTo = Marshal.StringToHGlobalUni(multiDest) įileOpStruct.fFlags = (ushort)_NOCONFIRMATION String multiSource = StringArrayToMultiString(source) Win32.SHFILEOPSTRUCT FileOpStruct = new Win32.SHFILEOPSTRUCT() įileOpStruct.wFunc = (uint)Win32.FO_MOVE Public static bool Move(String source, String dest) Int retval = Win32.SHFileOperation(ref FileOpStruct) What's wrong with the following straightforward method? public static void CopyFilesRecursively(DirectoryInfo source, DirectoryInfo target) ) įileOpStruct.pFrom = Marshal.StringToHGlobalUni(multiSource) įileOpStruct.fFlags = (ushort)_SILENT | (ushort)_NOCONFIRMATION | (ushort)_NOERRORUI | (ushort)_NOCONFIRMMKDIR įileOpStruct.hNameMappings = IntPtr.Zero Hmm, I think I misunderstand the question but I'm going to risk it.
0 Comments
Leave a Reply. |