3

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);
user1550951
  • 369
  • 2
  • 9
  • 26
  • Have you tried reading the directory files to get the list of the files in the directory? – Roy Apr 17 '13 at 03:39
  • Did you give my method a go? Its a one-liner! – Jeremy Thompson Apr 17 '13 at 06:53
  • 1
    @JeremyThompson: your answer is a 'spot-on' but I wouldn't know the name of the files.I have to write a method that runs in the background,scans the folder and creates zip files from the 3 files having same name but different extensions.The filenames are auto-generated by a device(over which I have no control).The device also FTPs these files over to my FTP server.The filenames are random alpha-numeric strings. – user1550951 Apr 17 '13 at 07:09

3 Answers3

5

Use an asterisk wildcard for the File Extension in the call to GetFiles, eg:

 List<string> files = Directory.GetFiles(pathName, "SpecificFileName.*");

Or:

 string[] files = Directory.GetFiles(pathName, "SpecificFileName.*");
j08691
  • 204,283
  • 31
  • 260
  • 272
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
5

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);
Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
0

You can try the below code. This will also solve if there are any dots in the filename itself.

        string[] fileNames = Directory.GetFiles(path);

        List<string> fileNamesWithoutExtension = new List<string>(); 
        List<string> myFiles = new List<string>();

        string myFile = mySearchFileWithoutExtension;

        foreach (string s in fileNames)
        {
            char[] charArray = s.ToCharArray();

            Array.Reverse(charArray);
            string s1 = new string(charArray);

            string[] ext = s1.Split(new char[] { '.' }, 2);

            if ((ext.Length > 1))
            {
                char[] charArray2 = ext[1].ToCharArray();

                Array.Reverse(charArray2);

                fileNamesWithoutExtension.Add(new string(charArray2));

                if ((new string(charArray2)).Trim().Equals(myFile))
                {
                    myFiles.Add(s);
                }
            }
        }

List of files without extensions: fileNamesWithoutExtension

Your required set of files: myFiles

KbManu
  • 420
  • 3
  • 7
  • getting a compilation error at this line: `string[] ext = s1.Split(new char[] { '.' }, 2);` – user1550951 Apr 17 '13 at 05:19
  • I have tested the above code. Its working and also I have edited for your requirement. – KbManu Apr 17 '13 at 05:40
  • appreciate your help but I still get the same compilation error at the same line.The error says:"The best overloaded method match for 'string.Split(params char[])' has some invalid arguments. – user1550951 Apr 17 '13 at 05:48
  • just replace with other signature: s1.Split('.'); Other lines in the method may not be useful, but it solves your problem if you have only one '.' in the filename. – KbManu Apr 17 '13 at 06:09