如何使用拆分逻辑批量删除文件名中带有逗号的文件 [英] How to do bulk delete file which having comma in filename with split logic

查看:250
本文介绍了如何使用拆分逻辑批量删除文件名中带有逗号的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用逗号(,)分隔的逻辑在物理路径中保存和删除多个文件.因此,保存时我没有任何问题,但是由于逻辑拆分,文件没有从物理路径中删除.

We are saving and deleting multiple files in a physical path with comma(,) separated logic. So, I am not getting any issue while saving but files are not deleting from the physical path due to split logic.

public ContentResult RemoveFiles(RequestModel reqModel)
    {
        var physicalPath = GetPhysicalPath(reqModel);
        var removeGroupPageId = reqModel.Get(ParamTypes.RouteData, "itemId");
        if (removeGroupPageId != "")
        {
            physicalPath = String.Concat(reqModel.Get(ParamTypes.Config, "RootDirectoryPhysical"), SessionWrapper.HomeDirectory, @"\Group", removeGroupPageId);
        }
        var files = reqModel.Get(ParamTypes.Form, "files", "").Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

        foreach (var path in files.Select(file => String.Concat(physicalPath, @"\", file)).Where(System.IO.File.Exists))
        {
            System.IO.File.Delete(path);
        }
        return Content("{ \"success\": true }", "application/json");
    }



那么,有谁知道我该如何消除这个问题?

我尝试过的事情:

我尚未尝试任何操作,但我正在寻找拆分的解决方案.



So, does anyone know how can I eliminate this issue?

What I have tried:

I''ve not tried anything yet but I am looking for a solution for splitting.

推荐答案

简单:请勿使用任何合法的字符分隔项目文件路径.如果这样做,您正在寻求问题,将很难解决.

因此,不要使用逗号分隔路径,而应使用竖线字符"|",因为根本不允许在路径中使用该字符:
Simple: don''t separate items with any character which is legal in a file path. If you do, you are asking for problems, which will be very difficult to resolve.

So instead of separating paths with commas, use the vertical bar character ''|'' as that is not allowed in paths at all: Naming Files, Paths, and Namespaces (Windows)[^]


public static List<string> GetFileNames(string path, string data)
{

    List<string> filenames = new List<string>();
    if (!string.IsNullOrEmpty(data))
    {
        List<string> parts = data.Split(',').ToList();
        string filename = string.Empty;
        do
        {
            filename = string.Format("{0}{1}{2}", filename, (filename.Length > 0)?",":"", parts[0]);
            if (File.Exists(System.IO.Path.Combine(path, filename)))
            {
                filenames.Add(filename);
                filename = string.Empty;
            }
            parts.RemoveAt(0);
        } while(parts.Count > 0);
    }
    return filenames;
}



用法示例:



Sample usage:

var files = "blah blah,a.xyz,blahblah.xyz";
List<string> filenames = GetFileNames(@"C:\mypath", files);
// now, do something with the resulting valid filenames (you know they're valid 
// because they wouldn't be in the list if they weren't.



注意-文件必须存在才能建立适当的名称,事后看来可能不合适.

您的替代方法-将分隔符更改为不是有效的文件名字符,例如星号,管道符号,问号等.此时,您可以简单地使用string.Split方法.



Caveat - the files must exist in order to establish the appropriate names, which, on hind sight, may not be appropriate.

Alternative for you - CHANGE the delimiter character to something that isn''t a valid filename character, such as an asterisk, pipe symbol, question mark, etc. At that point, you can simply use the string.Split method.


这篇关于如何使用拆分逻辑批量删除文件名中带有逗号的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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