Search code examples
c#.netfile-iocompact-framework2.0

find files with same names but different extensions in a folder


I have an FTP server which stores files sent/uploaded by the client in a certain folder. The client will upload 3 files with same names but different extensions. For example,the client will send file1.ext1,file1.ext2 and file1.ext3. I am looking for a piece of code which will help me find files with same names("file1") and then zip them. Any help is appreciated. I have written this code which gets the name of all the files in the folder:

string path = "somepath";
String[] FileNames = Directory.GetFiles(path);

Solution

  • This is fairly simple to do:

    string path = "somepath";
    String[] FileNames = Directory.GetFiles(path);
    

    You can use LINQ to group the files by their name, without extension:

    var fileGroups = from f in FileNames
        group f by Path.GetFileNameWithoutExtension(f) into g
        select new { Name = g.Key, FileNames = g };
    
    // each group will have files with the
    // same name and different extensions
    foreach (var g in fileGroups)
    {
        // initialize zip file
        foreach (var fname in g.FileNames)
        {
            // add fname to zip
        }
        // close zip file
    }
    

    Update

    The task isn't too much more difficult if you don't have LINQ. First, you want to sort the files:

    Array.Sort(FileNames);
    

    Now, you have a list of files, sorted by file name. So you'll have, for example:

    file1.ext1
    file1.ext2
    file1.ext3
    file2.ext1
    file2.ext2
    etc...
    

    Then just go through the list, adding files with the same base name to zip files, as below. Note that I don't know how you're creating your zip files, so I just made up a simple ZipFile class. You'll of course need to replace that with whatever you're using.

    string lastFileName = string.Empty;
    string zipFileName = null;
    ZipFile zipFile = null;
    for (int i = 0; i < FileNames.Length; ++i)
    {
        string baseFileName = Path.GetFileNameWithoutExtension(FileNames[i]);
        if (baseFileName != lastFileName)
        {
            // end of zip file
            if (zipFile != null)
            {
                // close zip file
                ZipFile.Close();
            }
            // create new zip file
            zipFileName = baseFileName + ".zip";
            zipFile = new ZipFile(zipFileName);
            lastFileName = baseFileName;
        }
        // add this file to the zip
        zipFile.Add(FileNames[i]);
    }
    // be sure to close the last zip file
    if (zipFile != null)
    {
        zipFile.Close();
    }
    

    I don't know if the Compact Framework has the Path.GetFileNameWithoutExtension method. If not, then you can get the name without extension by:

    string filename = @"c:\dir\subdir\file.ext";
    int dotPos = filename.LastIndexOf('.');
    int slashPos = filename.LastIndexOf('\\');
    string ext;
    string name;
    int start = (slashPos == -1) ? 0 : slashPos+1;
    int length;
    if (dotPos == -1 || dotPos < slashPos)
        length = filename.Length - start;
    else
        length = dotPos - start;
    string nameWithoutExtension = filename.Substring(start, length);