Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

C#
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.
Posted
Updated 15-Feb-18 1:30am

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)[^]
 
Share this answer
 
C#
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:

C#
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.


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.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900