Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
due to some code many files of the form
"acd5604f-5bbd-4e94-9b40-ea0247cdb4ad"
have been created in some of the clients' folders. These files are created using some GUID() function and they have no extension.
I needed to remove them, what is the pattern that helps getting them in the easiest way.
Posted
Comments
Prasad Avunoori 16-Jun-14 5:04am    
//Generate New GUID
Guid objGuid = Guid.NewGuid();
//Take invalid guid format
string strGUID = "aaa-a-a-a-a";

Guid newGuid;

if (Guid.TryParse(objGuid.ToString(), out newGuid) == true)
{
Response.Write(string.Format("<br/>{0} is Valid GUID.", objGuid.ToString()));
}
else
{
Response.Write(string.Format("<br/>{0} is InValid GUID.", objGuid.ToString()));
}


Guid newTmpGuid;

if (Guid.TryParse(strGUID, out newTmpGuid) == true)
{
Response.Write(string.Format("<br/>{0} is Valid GUID.", strGUID));
}
else
{
Response.Write(string.Format("<br/>{0} is InValid GUID.", strGUID));
}
Prasad Avunoori 16-Jun-14 5:05am    
Customize the above code according to your need.

you can use Guid.TryParse Method[^]
C#
string filename = "acd5604f-5bbd-4e94-9b40-ea0247cdb4ad";
Guid guid;
if(Guid.TryParse(filename , out guid))
{
   //delete file 
}
 
Share this answer
 
v2
The pattern is:
- 8 hexadecimal digits
- dash
- 4 hexadecimal digits
- dash
- 4 hexadecimal digits
- dash
- 4 hexadecimal digits
- dash
- 12 hexadecimal digits

A regular expression could catch it easily:
C#
Regex r = nex Regex(@"^[0-9a-zA-Z]{8}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{4}-[0-9a-zA-Z]{12}$"

string fileName;
foreach (FileInfo fi in dir.GetFiles()) {
   fileName = fi.FileName;
   if (r.IsMatch(fileName)) { // Here the file name is a GUID
      fi.Delete();
   }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900