找到一个文件夹中有相同的名字,但文件扩展名不同 [英] find files with same names but different extensions in a folder
问题描述
我有一个存储客户端在一个特定的文件夹上传发送的文件/ FTP服务器。客户端会上传3个文件具有相同的名称但不同的扩展名。例如,客户端将发送file1.ext1,file1.ext2和file1.ext3。我找了一块code,这将有助于我找到同名的文件(文件1),然后压缩它们。任何帮助是AP preciated。我写了这个code这将被命名的所有文件夹中:
字符串路径=somepath;
的String []文件名= Directory.GetFiles(路径);
这是非常简单的事情:
字符串路径=somepath;
的String []文件名= Directory.GetFiles(路径);
您可以使用LINQ组他们的名字的文件,没有扩展名:
VAR文件组=从步骤f的文件名
通过Path.GetFileNameWithoutExtension(F)为G F组
选择新{名称= g.Key,文件名= G};
//每个小组将与文件
//相同的名字和不同的扩展
的foreach(在文件组VAR G)
{
//初始化zip文件
的foreach(在g.FileNames VAR FNAME)
{
//添加FNAME到拉链
}
//关闭zip文件
}
更新
的任务是不是太困难得多,如果你没有LINQ。首先,要对文件进行排序:
的Array.Sort(文件名);
现在,你有文件,按文件名排序的列表。所以你必须,例如:
file1.ext1
file1.ext2
file1.ext3
file2.ext1
file2.ext2
等等...
然后,只需通过列表,使用相同的基本名称为zip文件添加文件,如下图所示。请注意,我不知道你如何创建zip文件,所以我只是做了一个简单的的ZipFile
类。当然,你需要替换成任何你正在使用。
字符串lastFileName =的String.Empty;
字符串zipFileName = NULL;
ZipFile的zip文件= NULL;
的for(int i = 0; I< FileNames.Length ++ I)
{
字符串baseFileName = Path.GetFileNameWithoutExtension(文件名[I]);
如果(baseFileName!= lastFileName)
{
// zip文件的末尾
如果(zip文件!= NULL)
{
//关闭zip文件
ZipFile.Close();
}
//创建新的zip文件
zipFileName = baseFileName +.ZIP;
zip文件=新的ZipFile(zipFileName);
lastFileName = baseFileName;
}
//这个文件添加到压缩
zipFile.Add(文件名[I]);
}
//一定要关闭的最后一个zip文件
如果(zip文件!= NULL)
{
zipFile.Close();
}
我不知道,如果Compact Framework的有 Path.GetFileNameWithoutExtension
方法。如果没有,那么你就可以得到这个名字不带扩展名是:
字符串文件名= @C:\目录\子目录\ file.ext;
INT dotPos = filename.LastIndexOf('。');
INT slashPos = filename.LastIndexOf('\\');
字符串转;
字符串名称;
INT开始=(slashPos == -1)? 0:slashPos + 1;
INT长;
如果(dotPos == -1 || dotPos< slashPos)
长度= filename.Length - 启动;
其他
长度= dotPos - 启动;
字符串nameWithoutExtension = filename.Substring(开始,长度);
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:
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);
这篇关于找到一个文件夹中有相同的名字,但文件扩展名不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!