/// <summary> /// Creates a new directory (and parent directories). Used recursively. /// </summary> /// <param name="path">The full path of the directory (and parent directories) to create.</param> /// <returns>The new (or existing) directory.</returns> private Directory NewDirectory(FOS_System.String path) { //Output info to the user. console.WriteLine("Searching for directory: " + path); //Attempt to find the directory. If it already exists, we don't want to // accidentally re-create it! Directory theDir = Directory.Find(path); //If the directory does not exist: if (theDir == null) { //Output info to the user. console.WriteLine("Creating directory..."); //Attempt to get the file system mapping for the new directory FileSystemMapping mapping = FileSystemManager.GetMapping(path); //If the mapping was found: if(mapping != null) { //Remove trailing "/" if there is one else the code below would end // up with a blank "new directory name" if (path.EndsWith(FileSystemManager.PathDelimiter)) { path = path.Substring(0, path.length - 1); } // + 1 as we wish to include the path delimeter in parent dir name and // not in the new dir name. // Note: It is important to include the path delimeter at the end of the parent dir name // as the parent dir name may be a FS root which requires the trailing path delimeter. int lastIdx = path.LastIndexOf(FileSystemManager.PathDelimiter) + 1; FOS_System.String dirParentPath = path.Substring(0, lastIdx); FOS_System.String newDirName = path.Substring(lastIdx, path.length - lastIdx); console.WriteLine("Checking parent path: " + dirParentPath); //This causes NewDirectory to become a recursive, self-calling // method which could potentially overflow. However, if has // the benefit that the entire new directory tree can be created // in one call, rather than having to create each directory and // sub-directory one at a time. Directory parentDir = NewDirectory(dirParentPath); if (parentDir != null) { console.WriteLine("New dir name: " + newDirName); //Create the directory theDir = mapping.TheFileSystem.NewDirectory(newDirName, parentDir); console.WriteLine("Directory created."); } else { console.WriteLine("Failed to find or create parent directory."); } } else { console.WriteLine("File system mapping not found."); } } else { console.WriteLine("Directory already exists."); } return theDir; }
/// <summary> /// Copies the specified directory. /// </summary> /// <param name="srcDir">The directory to copy.</param> /// <param name="dst">The path to copy to.</param> private void CopyDirectory(Directory srcDir, FOS_System.String dst) { //If source directory is null, it means it wasn't found by the caller (or some // other error but we will assume not found since that is the expected use // case from the other overload of CopyDirectory). if (srcDir == null) { console.WriteLine("Source directory not found!"); return; } //Add a trailing "/" to the destination path name if(!dst.EndsWith(FileSystemManager.PathDelimiter)) { dst += FileSystemManager.PathDelimiter; } //Creates the entire directory tree as required or returns // the existing directory. Directory dstDir = NewDirectory(dst); //For explanation of this, see CopyFile FOS_System.String srcFullPath = srcDir.GetFullPath(); FOS_System.String dstFullPath = dstDir.GetFullPath(); if (srcFullPath == dstFullPath) { console.WriteLine("Atempted to copy a directory to itself! (" + srcFullPath + ")"); return; } else { console.WriteLine("Copying " + srcFullPath + " to " + dstFullPath); } //Copy listings // This causes CopyDirectory to be a recursive, self-calling method // which could potentially overflow. It has the benefit though that // the entire sub-directory/sub-file tree will be copied. List listings = srcDir.GetListings(); for(int i = 0; i < listings.Count; i++) { Base listing = (Base)listings[i]; if(listing.IsDirectory) { if (listing.Name != "." && listing.Name != "..") { CopyDirectory((Directory)listing, dst + listing.Name + FileSystemManager.PathDelimiter); } } else { CopyFile((File)listing, dst + listing.Name); } } }