使用通配符存档并从文件夹中删除文件。 [英] Archive and remove the files from folders using wildcard characters.

查看:95
本文介绍了使用通配符存档并从文件夹中删除文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





让我们考虑一下,



E:/ Folder2 / Fol24 / Fo2222

E:/ Folder3 / Fol23 / Folder333

E:/ Folder4 / Folde25 / Fold44

E:/ Folder5 / Folder55 / Folde5


我需要使用像E:/ Folder?/ Fol2?/ Fo *

这样的输入来存档和删除通配符匹配文件夹中的所有文件这里我期待文件夹E:/ Folder2 / Fol24 / Fo2222和E:/ Folder3 / Fol23 / Folder333



这里的问题是用户可以输入任何模式通配符。根据模式,我必须将文件复制到其他位置并删除文件。在这里,我不能使用任何固定的搜索模式。它应该是动态的,可以有任何不。通配符。



感谢您的帮助。



问候

Chandra

解决方案

Directory.GetFiles(string,string)允许您指定搜索模式。 Directory.GetFiles方法(字符串,字符串)(System.IO) [ ^ ]


  private   static  List< string> folders =  new  List< string>(); 

private void buttonExample_Click( object sender,EventArgs e)
{
folders.Clear();

string input = textBox1.Text;
string currentDir = string .Empty,currentPattern = string .Empty,nextPattern = string .Empty;

foreach char c 输入中)
{
if (c == ' ?'
{

string [] f =输入.Split( new [] {' ?'}, 2 );
if (f [ 0 ]。长度 > 0
{
currentDir = Directory.GetParent(f [ 0 ])全名。
}

if (f [ 1 ]。长度< span class =code-keyword>>
0
{
nextPattern = f [ 1 ]。TrimStart(' \\');
}

string [] w = input.Split('' \\');
foreach string s in w)
{
if (s.Contains(' ?'))
{
currentPattern = s;
break ;
}
}

break ;
}
其他 如果(c == ' *'
{
string [ ] f = input.Split( new [] {' *'}, 2 );

if (f [ 0 ]。长度> 0
{
currentDir = Directory.GetParent(f [ 0 ])全名。
}
if (f [ 1 ]。长度> 0
{
nextPattern = f [ 1 ]。TrimStart(' \\');
}

string [] w = input.Split('' \\');
foreach string s in w)
{
if (s.Contains(' *'))
{
currentPattern = s;
break ;
}
}

break ;
}
}
DirectoryInfo di = new DirectoryInfo(currentDir);
List< string> foldersNew = new List< string>();
foldersNew = GetAllFolders(di,currentPattern,nextPattern);
}

私人 静态列表< string> GetAllFolders(DirectoryInfo currentDir, string currentPattern, string nextPatten)
{
DirectoryInfo [ ] dis = currentDir.GetDirectories(currentPattern);
if (dis.Length == 0 && string .Equals(currentPattern, string .Empty))
folders.Add(currentDir.FullName);

if (dis.Length > 0
{
string [] remainPattern = nextPatten.Split( \\ .ToCharArray());
if (remainPattern.Length > 0
{
foreach (DirectoryInfo di in dis)
{
GetAllFolders(di,remainPattern.First(),
string .Join( \\,remainingPattern.Skip( 1 )。ToArray()) );
}
}

}

return 个文件夹;
}





Quote:

我的解决方案可能对有需要的人有帮助。这将解决问题。感谢大家的帮助。



参考来自:http://stackoverflow.com/questions/3306445/parsing-an-canonical-path-with-wildcards


Hi,

Let us consider,

E:/Folder2/Fol24/Fo2222
E:/Folder3/Fol23/Folder333
E:/Folder4/Folde25/Fold44
E:/Folder5/Folder55/Folde5

I need to archive and remove all the files from wildcard character matching folders using some input like E:/Folder?/Fol2?/Fo*
Here I am expecting the folders E:/Folder2/Fol24/Fo2222 and E:/Folder3/Fol23/Folder333

The problem here is the user can enter any pattern of wildcard characters. According to the pattern, I have to copy the files to other location and remove the files. Here I can't use any fixed search pattern. It should be dynamic and can have any no. of wildcard characters.

Thanks for your help.

Regards
Chandra

解决方案

Directory.GetFiles(string, string) allows you to specify a search pattern. Directory.GetFiles Method (String, String) (System.IO)[^]


private static List<string> folders = new List<string>();

    private void buttonExample_Click(object sender, EventArgs e)
    {
        folders.Clear();

        string input = textBox1.Text;
        string currentDir = string.Empty, currentPattern = string.Empty, nextPattern = string.Empty;

        foreach (char c in input)
        {
            if (c == '?')
            {

                string[] f = input.Split(new[] {'?'}, 2);
                if (f[0].Length > 0)
                {
                    currentDir = Directory.GetParent(f[0]).FullName;
                }

                if (f[1].Length > 0)
                {
                    nextPattern = f[1].TrimStart('\\');
                }

                string[] w = input.Split('\\');
                foreach (string s in w)
                {
                    if (s.Contains('?'))
                    {
                        currentPattern = s;
                        break;
                    }
                }

                break;
            }
            else if(c == '*')
            {
                string[] f = input.Split(new[] { '*' }, 2);

                if (f[0].Length > 0)
                {
                    currentDir = Directory.GetParent(f[0]).FullName;
                }
                if (f[1].Length > 0)
                {
                    nextPattern = f[1].TrimStart('\\');
                }

                string[] w = input.Split('\\');
                foreach (string s in w)
                {
                    if (s.Contains('*'))
                    {
                        currentPattern = s;
                        break;
                    }
                }

                break;
            }
        }
        DirectoryInfo di = new DirectoryInfo(currentDir);
        List<string> foldersNew = new List<string>();
        foldersNew = GetAllFolders(di, currentPattern, nextPattern);
    }

    private static List<string> GetAllFolders(DirectoryInfo currentDir,string currentPattern, string nextPatten)
    {
        DirectoryInfo[] dis = currentDir.GetDirectories(currentPattern);
        if (dis.Length == 0 && string.Equals(currentPattern,string.Empty))
            folders.Add(currentDir.FullName);

        if (dis.Length > 0)
        {
            string[] remainPattern = nextPatten.Split("\\".ToCharArray());
            if (remainPattern.Length > 0)
            {
                foreach (DirectoryInfo di in dis)
                {
                    GetAllFolders(di, remainPattern.First(),
                                   string.Join("\\", remainPattern.Skip(1).ToArray()));
                }
            }

        }

        return folders;
    }



Quote:

My solution might be helpful for those who need. This will solve the problem. Thanks for all for your help.

Reference taken from: http://stackoverflow.com/questions/3306445/parsing-an-canonical-path-with-wildcards


这篇关于使用通配符存档并从文件夹中删除文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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