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);
This is fairly simple to do:
You can use LINQ to group the files by their name, without extension:
Update
The task isn't too much more difficult if you don't have LINQ. First, you want to sort the files:
Now, you have a list of files, sorted by file name. So you'll have, for example:
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.I don't know if the Compact Framework has the
Path.GetFileNameWithoutExtension
method. If not, then you can get the name without extension by:You can try the below code. This will also solve if there are any dots in the filename itself.
List of files without extensions: fileNamesWithoutExtension
Your required set of files: myFiles
Use an asterisk wildcard for the File Extension in the call to
GetFiles
, eg:Or: