所以...我做了一个哎呀。一个相当大的一个。我写了一些代码来搜索文件夹和所有子目录,并提取任何PDF文件。我打算做的是将PDF复制到一个新的位置,而我所做的就是移动它们。代码一夜之间运行,因为我知道我将处理70K +文件,并且在尝试做其他事情时不想放慢计算机的速度。 我需要做的就是将它们移回去。这里的问题是每个文件都是一个唯一的名称,来自一个名称非常相似的文件夹。例如:文件名ULT00004605_20161105.pdf来自名为ULT00004605的子目录。 有没有人知道如何索引子目录并将它们放回原点? 我用来移动它们的代码是我试过了什么? 。当我应该使用CopyTo时,你可以看到我使用了MoveTo。 我尝试了什么:
So... I made an oops. A rather large one. I wrote some code to search through folders and all sub directories and pull any PDF file found. What I meant to do was to copy the PDF to a new location, instead what I did was move them. The code ran overnight since I knew I would be dealing with 70K+ files and didn't want to slow my computer while trying to do other things. What I need to do is move them back. The problem here is that each file is a unique name and came from a folder with a very similar name. For example: File name ULT00004605_20161105.pdf came from a sub directory named ULT00004605. Does anyone know how to index through the sub directories and put these back where they came from? The code I used to move them is under "What have I tried?". You can see I used MoveTo when I should've used CopyTo. What I have tried:
String path = @"S:\QS Storage Server\DHR\ULT\"; String directoryName = @"G:\SHARED\MoveMe\SNHR_To_Upload\"; DirectoryInfo dirInfo = new DirectoryInfo(directoryName); if (dirInfo.Exists == false) { Directory.CreateDirectory(directoryName); } List<String> SNHRFiles = Directory.GetFiles(path, "*.pdf*", SearchOption.AllDirectories).ToList(); foreach (string file in SNHRFiles) { FileInfo mFile = new FileInfo(file); // to remove name collisions if (new FileInfo(dirInfo + "\\" + mFile.Name).Exists == false) { mFile.MoveTo(dirInfo + "\\" + mFile.Name); } }推荐答案
这很容易解决。您需要做的就是从文件名中获取文件夹名称。请参阅: It's pretty easy to resolve. All what you need to do is to get folder name from file name. See: string sFullFileName = @"G:\SHARED\MoveMe\SNHR_To_Upload\ULT00004605_20161105.pdf"; string sDestPathName = @"S:\QS Storage Server\DHR\ULT\"; string sShortFileName = Path.GetFileName(sFullFileName); string sDestFolderName = sShortFileName.Split(new string[]{"_"}, StringSplitOptions.RemoveEmptyEntries)[0]; Console.WriteLine("File: '{0}' will be copied to: '{1}'", sShortFileName, Path.Combine(sDestPathName, sDestFolderName));
结果:
Result:
File: 'ULT00004605_20161105.pdf' will be copied to: 'S:\QS Storage Server\DHR\ULT\ULT00004605'
由于文件夹已包含我不想删除或不得不移动的其他文件,因此最终解决方案有点比Meciej Los的反应更深入,但他的意见帮助我解决了问题。这是最终的代码。 Since the folders already contained other files that I did not want to delete or have to move, the ultimate solution was a bit more in depth than Meciej Los's response, but his input helped me figure it out. Here is the final code. String ToPath = @"O:\QS Storage Server\DHR Storage\ULT\"; String FromPath = @"O:\QS Storage Server\DHR Storage\ULT\SNHR_To_Upload\"; DirectoryInfo FromdirInfo = new DirectoryInfo(FromPath); DirectoryInfo TodirInfo = new DirectoryInfo(ToPath); List<String> SNHRFilesToMoveBack = Directory.GetFiles(FromPath, "*.pdf*", SearchOption.AllDirectories).ToList(); List<String> FoldersForMoveBack = Directory.GetDirectories(ToPath, "*.*", SearchOption.AllDirectories).ToList(); foreach (string folder in FoldersForMoveBack) { //search the SNHR_To_Upload folder for the file that matches the folder name DirectoryInfo mDirectory = new DirectoryInfo(folder); string folderToCopyTo = mDirectory.Name; foreach (string file in SNHRFilesToMoveBack) { FileInfo mFile = new FileInfo(file); string FileName = Path.GetFileName(file); string ShortName = FileName.Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries)[0]; if (folderToCopyTo == ShortName) { try { mFile.MoveTo(mDirectory + "\\" + mFile.Name); } catch (Exception nope) { MessageBox.Show("Cannot Find File" + nope); continue; } } } }
更多推荐
移动后恢复文件
发布评论