根据searchPattern移动文件 [英] Move files according to searchPattern

查看:211
本文介绍了根据searchPattern移动文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想要从一个文件夹移动到另一个文件名的excel列表。而且我不能将文件从一个文件夹复制到另一个文件夹,因为有不匹配的Excel文件的分配。

  private static void CopyPaste()
{
var pstFileFolder =C:/ Users / chnikos /桌面/测试/;
// var searchPattern =HelloWorld.docx+Test.docx;
string [] test = {HelloWorld.docx,Test.docx};
var soruceFolder =C:/ Users / chnikos / Desktop / CopyTest /;

//在目录中搜索* .pst
foreach(在Directory.GetFiles中的var文件(pstFileFolder,test.ToString()))
{
//公开文件信息,如名称
var theFileInfo = new FileInfo(file);
var destination = soruceFolder + theFileInfo.Name;
File.Move(file,destination);



$ b code
$ b

我有尝试了几件事情,但我仍然认为,与数组这将是最简单的方法来做到这一点(纠正我,如果我错了)。

我面对的问题是正确的现在是它无法找到任何文件(有这个名字的文件。

解决方案

您可以枚举目录中的文件通过使用 Directory.EnumerateFiles 使用linq表达式来检查文件是否包含在你的字符串数组中。

  Directory.EnumerateFiles(pstFileFolder) .Where(d => test.Contains(Path.GetFileName(d))); 

你的foreach会看起来像
这样的

  foreach(Directory.EnumerateFiles(pstFileFolder) > test.Contains(Path.GetFileName(d)))
{
//显示名称
的文件信息var theFileInfo = new FileInfo(file);
var destination = soruceFolder + theFileInfo.Name;
File.Move(file,destination);
}


I have excel list with file names that I want to move from one folder to another. And I can not just copy paste the files from one folder to another since there are allot of files that do not match the excel list.

  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);

        }
    }
}

I have tried several things but I still think that with a array it would be the easiest way to do it(correct me if I am wrong).

The issue that I face right now is that it can not find any files (there are files under this name.

解决方案

You can enumerate the files in the directory by using Directory.EnumerateFiles and use a linq expression to check if the file is contained in you string array.

Directory.EnumerateFiles(pstFileFolder).Where (d => test.Contains(Path.GetFileName(d)));

So your foreach would look like this

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);
}

这篇关于根据searchPattern移动文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆