根据 searchPattern 移动文件
Move files according to searchPattern
我有 excel 列表,其中包含要从一个文件夹移动到另一个文件夹的文件名。而且我不能只是将文件从一个文件夹复制粘贴到另一个文件夹,因为有很多文件与 excel 列表不匹配。
private static void CopyPaste()
{
var pstFileFolder = "C:/Users/chnikos/Desktop/Test/";
//var searchPattern = "HelloWorld.docx"+"Test.docx";
string[] test = { "HelloWorld.docx", "Test.docx" };
var soruceFolder = "C:/Users/chnikos/Desktop/CopyTest/";
// Searches the directory for *.pst
foreach (var file in Directory.GetFiles(pstFileFolder, test.ToString()))
{
// Exposes file information like Name
var theFileInfo = new FileInfo(file);
var destination = soruceFolder + theFileInfo.Name;
File.Move(file, destination);
}
}
}
我已经尝试了几种方法,但我仍然认为使用数组是最简单的方法(如果我错了请纠正我)。
我现在面临的问题是找不到任何文件(这个名字下有文件。
实际上不会,这不会在目录中搜索 pst 文件。要么使用 Path.Combine 自己构建路径,然后遍历字符串数组,要么使用你的方法。使用上面的代码,您需要更新过滤器,因为在给定 string[].ToString () 时它不会找到任何文件。应该这样做:
Directory.GetFiles (pstFileFolder, "*.pst")
或者,您可以在没有过滤器的情况下遍历所有文件,并将文件名与您的字符串数组进行比较。为此,List<string>
将是更好的方法。只需像您正在做的那样遍历文件,然后通过 List.Contains
.
检查列表是否包含该文件
foreach (var file in Directory.GetFiles (pstFileFolder))
{
// Exposes file information like Name
var theFileInfo = new FileInfo(file);
// Here, either iterate over the string array or use a List
if (!nameList.Contains (theFileInfo.Name)) continue;
var destination = soruceFolder + theFileInfo.Name;
File.Move(file, destination);
}
我想你需要这个
var pstFileFolder = "C:/Users/chnikos/Desktop/Test/";
//var searchPattern = "HelloWorld.docx"+"Test.docx";
string[] test = { "HelloWorld.docx", "Test.docx" };
var soruceFolder = "C:/Users/chnikos/Desktop/CopyTest/";
// Searches the directory for *.pst
foreach (var file in test)
{
// Exposes file information like Name
var theFileInfo = new FileInfo(file);
var source = Path.Combine(soruceFolder, theFileInfo.Name);
var destination = Path.Combine(pstFileFolder, file);
if (File.Exists(source))
File.Move(file, destination);
}
您可以使用 Directory.EnumerateFiles 枚举目录中的文件,并使用 linq 表达式检查该文件是否包含在您的字符串数组中。
Directory.EnumerateFiles(pstFileFolder).Where (d => test.Contains(Path.GetFileName(d)));
所以你的 foreach 看起来像
这个
foreach (var file in Directory.EnumerateFiles(pstFileFolder).Where (d => test.Contains(Path.GetFileName(d)))
{
// Exposes file information like Name
var theFileInfo = new FileInfo(file);
var destination = soruceFolder + theFileInfo.Name;
File.Move(file, destination);
}
我有 excel 列表,其中包含要从一个文件夹移动到另一个文件夹的文件名。而且我不能只是将文件从一个文件夹复制粘贴到另一个文件夹,因为有很多文件与 excel 列表不匹配。
private static void CopyPaste()
{
var pstFileFolder = "C:/Users/chnikos/Desktop/Test/";
//var searchPattern = "HelloWorld.docx"+"Test.docx";
string[] test = { "HelloWorld.docx", "Test.docx" };
var soruceFolder = "C:/Users/chnikos/Desktop/CopyTest/";
// Searches the directory for *.pst
foreach (var file in Directory.GetFiles(pstFileFolder, test.ToString()))
{
// Exposes file information like Name
var theFileInfo = new FileInfo(file);
var destination = soruceFolder + theFileInfo.Name;
File.Move(file, destination);
}
}
}
我已经尝试了几种方法,但我仍然认为使用数组是最简单的方法(如果我错了请纠正我)。
我现在面临的问题是找不到任何文件(这个名字下有文件。
实际上不会,这不会在目录中搜索 pst 文件。要么使用 Path.Combine 自己构建路径,然后遍历字符串数组,要么使用你的方法。使用上面的代码,您需要更新过滤器,因为在给定 string[].ToString () 时它不会找到任何文件。应该这样做:
Directory.GetFiles (pstFileFolder, "*.pst")
或者,您可以在没有过滤器的情况下遍历所有文件,并将文件名与您的字符串数组进行比较。为此,List<string>
将是更好的方法。只需像您正在做的那样遍历文件,然后通过 List.Contains
.
foreach (var file in Directory.GetFiles (pstFileFolder))
{
// Exposes file information like Name
var theFileInfo = new FileInfo(file);
// Here, either iterate over the string array or use a List
if (!nameList.Contains (theFileInfo.Name)) continue;
var destination = soruceFolder + theFileInfo.Name;
File.Move(file, destination);
}
我想你需要这个
var pstFileFolder = "C:/Users/chnikos/Desktop/Test/";
//var searchPattern = "HelloWorld.docx"+"Test.docx";
string[] test = { "HelloWorld.docx", "Test.docx" };
var soruceFolder = "C:/Users/chnikos/Desktop/CopyTest/";
// Searches the directory for *.pst
foreach (var file in test)
{
// Exposes file information like Name
var theFileInfo = new FileInfo(file);
var source = Path.Combine(soruceFolder, theFileInfo.Name);
var destination = Path.Combine(pstFileFolder, file);
if (File.Exists(source))
File.Move(file, destination);
}
您可以使用 Directory.EnumerateFiles 枚举目录中的文件,并使用 linq 表达式检查该文件是否包含在您的字符串数组中。
Directory.EnumerateFiles(pstFileFolder).Where (d => test.Contains(Path.GetFileName(d)));
所以你的 foreach 看起来像 这个
foreach (var file in Directory.EnumerateFiles(pstFileFolder).Where (d => test.Contains(Path.GetFileName(d)))
{
// Exposes file information like Name
var theFileInfo = new FileInfo(file);
var destination = soruceFolder + theFileInfo.Name;
File.Move(file, destination);
}