Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is fairly simple to do:</p> <pre><code>string path = "somepath"; String[] FileNames = Directory.GetFiles(path); </code></pre> <p>You can use LINQ to group the files by their name, without extension:</p> <pre><code>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 } </code></pre> <h2>Update</h2> <p>The task isn't too much more difficult if you don't have LINQ. First, you want to sort the files:</p> <pre><code>Array.Sort(FileNames); </code></pre> <p>Now, you have a list of files, sorted by file name. So you'll have, for example:</p> <pre><code>file1.ext1 file1.ext2 file1.ext3 file2.ext1 file2.ext2 etc... </code></pre> <p>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 <code>ZipFile</code> class. You'll of course need to replace that with whatever you're using.</p> <pre><code>string lastFileName = string.Empty; string zipFileName = null; ZipFile zipFile = null; for (int i = 0; i &lt; 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(); } </code></pre> <p>I don't know if the Compact Framework has the <code>Path.GetFileNameWithoutExtension</code> method. If not, then you can get the name without extension by:</p> <pre><code>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 &lt; slashPos) length = filename.Length - start; else length = dotPos - start; string nameWithoutExtension = filename.Substring(start, length); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload