Home > Net >  Rookie help, a master to help hand, thank you
Rookie help, a master to help hand, thank you

Time:03-12

Requirements:
Under the target path has many same file name, file of different suffix, such as 1. ZIP and 1, RAR this, I want to put all the same file name, suffix is RAR file deleted.

My train of thought:
1. Traverse under the target path file name (including suffix), get a filename variable filesname;
2. Get _filesname substring off after the suffix;
3. Put _filesname in to an array or a list again for loop, array [] I - 1==array [I], I on the array [] temp inside;
4. Traversal temp, get the variable j, j + "RAR", then the file. The delete ();

Stuck places:
1. Third, don't know how to put it in, only remember should be

If someone see this post, please help hand ah, if you can, best code, very grateful! Thank you very much!

CodePudding user response:



The same can file in a directory below?

CodePudding user response:

The forehead, file mask you can see the
Many operations are support file wildcard

CodePudding user response:

Test before use,
 var keys=System. IO. Directory. GetFiles (@ "C: " XXX). GroupBy (f=& gt; The Regex. Match (f, ". * (?=\ \ [^.] +) $"). The Value); 
//by file name (without the suffix) as the key, for grouping all files
The foreach (var g in keys)
{
//as a result, if a name has a variety of suffix file delete rar at the end of the file;
//if a name is only one file, it does not handle
If (right to mount () & gt;
=2){
Try
{
"System. IO. File. Delete (${g. ey}. Rar");
}//try to delete. Rar file, also don't have this file,
The catch (Exception) {}
}
}

CodePudding user response:

Regular written wrong, $symbol should be in inside the parentheses, front. + match better,
Correct regular: Regex. Match (" a - copy. Rar, "") + (?=\ \ [^.] + $) "). The Value

CodePudding user response:

 var groupFiles=System. IO. Directory. GetFiles (direPath). GroupBy (p=& gt; System. IO. Path. GetFileNameWithoutExtension (p)); 
The foreach (var files in groupFiles)
{
If (files. The Count () & gt;
=2){
The foreach (string file in files)
{
String the ext=System. IO. Path. GetExtension (file);
If (the ext. ToLower ()=="rar")
{
System. IO. File. Delete (File);
}
}
}
}

CodePudding user response:

refer to 6th floor Bridge_go response:
 var groupFiles=System. IO. Directory. GetFiles (direPath). GroupBy (p=& gt; System. IO. Path. GetFileNameWithoutExtension (p)); 
The foreach (var files in groupFiles)
{
If (files. The Count () & gt;
=2){
The foreach (string file in files)
{
String the ext=System. IO. Path. GetExtension (file);
If (the ext. ToLower ()=="rar")
{
System. IO. File. Delete (File);
}
}
}
}

The second floor for saves,. There is only one rar File, if you want to do judgment, directly in the File. The Exists,

CodePudding user response:

Is such a thing, how to play at random, I just tell you have file wildcard here, change the suffix have changeExtension method, judgment documents is there Exists

 System. IO. DirectoryInfo DirectoryInfo=new DirectoryInfo (@ "C: \ Users \ \ Administrator \ Downloads"); 

Foreach (var the fileInfo in directoryInfo GetFiles (" *.zip "). The Select (p=& gt; New to the FileInfo (System. IO. Path. ChangeExtension (p.F ullName, "rar"))) Where (p=& gt; P.E xists))
{
The fileInfo. Delete ();
}
  •  Tags:  
  • C#
  • Related