How to get files names inside folder using regular expression in c# - c#

I need to retrieve all files names inside a specific folder, but I need to use regular expression in which I need to get files names depends on a number in the file name. For example :
fitness-0Chromosom1
I mean zero in this case, I wrote the following:
//GetFiles on DirectoryInfo returns a FileInfo object.
var pdfFiles = new DirectoryInfo("C:/Users/Welcome/Desktop/Rounds/Fitness/AUV" + 1).GetFiles("fitness-"+geneticIteration+"*"+".txt");
Where geneticIteration is the number represent 0. Is it true or not?

Yes, your code above will return all file names matching "fitness-[a number]*.txt" in directory "C:/Users/Welcome/Desktop/Rounds/Fitness/AUV1".
Example results could be (given that "a number" = 0):
fitness-0Chromosom1.txt
fitness-0abc.txt
fitness-0.txt

GetFiles(path) also accepts the another parameter called 'searchPattern' where you can pass your regular expression. Another way to match the file name with your regular expression is using the Regex.IsMatch(path, pattern), which will return the true or false based on the comparison/match.

Related

Using Directory.GetFiles as a condition to check if certain files are inside the directory , c# scripts

How can I check all the files inside a directory that contains the (.jpg,.jpeg,.png and .pdf) file format and then would only proceed to store the filenames inside a variables if those files exist? I tried using this code but it does not work. Reason why I said it does not work is because the process I put inside is not being initiated. Is there something wrong with my code? Please enlighten me or lead me to the proper way. All help is appreciated!
if(Directory.GetFiles(directory).All(x=> string.Compare(Path.GetExtension(x),"*.pdf", StringComparison.CurrentCultureIgnoreCase) == 0) && Directory.GetFiles(directory).All(x=> string.Compare(Path.GetExtension(x),"*.jpg", StringComparison.CurrentCultureIgnoreCase))
{
// insert process here of getting the file names that has the extension of .jpg,.jpeg,.png and .pdf
}
The overload of the string compare method you're using does not accept a pattern to compare to, instead a second string to compare the first to. Which means if you have a file "fooBar.png" in your directory, your eventually comparing it's extension (so ".png") to "*.png", which is not the same.
You also said you want to get all file names that end with one of a number of specified extensions, but your using .All(...), which only returns true if all items inside the enumeration match the given expression. So
All(x=> string.Compare(Path.GetExtension(x),"*.pdf", StringComparison.CurrentCultureIgnoreCase) == 0)
would only return true if all files inside the directory were pdf files.
There is also not necessarily a problem, but something sub-optimal in your code: Your reading the same content from disk multiple times, which is as said, sub-optimal.
That being said, here is some updated code to fix your problem:
var acceptedFileTypes = new List<string>
{
".png",
".pdf",
...
};
// Get all files in the specified directory
var filesInDirectory = Directory.GetFiles(directory);
// Only select those with accepted file extensions
// Explicit type for better understanding
List<string> supportedFiles = filesInDirectory.Where(
file => acceptedFileTypes.Contains(Path.GetExtension(file))
).ToList();
// Do something with the supportedFiles
// e.g print them to the console:
foreach (var file in supportedFiles)
{
Console.WriteLine($"Found supported file: {file}");
}
You can do whatever you want with this, put it in a method and swap acceptedFileTypes for a static member, or put this in a static class of its own etc.
As well you can add new file types easily by appending the List

Confused about Directory.GetFiles

I've read the docs about the Directory.GetPath search pattern and how it is used, because I noticed that *.dll finds both test.dll and test.dll_20170206. That behavior is documented
Now, I have a program that lists files in a folder based on a user-configured mask and processes them. I noticed that masks like *.txt lead to the above mentioned "problem" as expected.
However, the mask fixedname.txt also causes fixedname.txt_20170206 or the like to appear in the list, even though the documentation states this only occurs
When you use the asterisk wildcard character in a searchPattern such as "*.txt"
Why is that?
PS: I just checked: Changing the file mask to fixednam?.txt does not help even though the docs say
When you use the question mark wildcard character, this method returns only files that match the specified file extension. For example, given two files, "file1.txt" and "file1.txtother", in a directory, a search pattern of "file?.txt" returns just the first file, whereas a search pattern of "file*.txt" returns both files.
If you need a solution you may transform the filter pattern into a regular expression by replacing * by (.*) and ? by .. You also have to escape some pattern characters like the dot. Then you check each filename you got from Directory.GetFiles against this regular expression. Keep in mind to not only check if it is a match but that the match length is equal to the length of the filename. Otherwise you get the same results as before.
GetFiles uses pattern serach, it searches for all names in path ending with the letters specified.
You can write code similar to below to get only .txt extension file
foreach (string strFileName in Directory.GetFiles(#"D:\\test\","*.txt"))
{
string extension;
extension = Path.GetExtension(strFileName);
if (extension != ".txt")
continue;
else
{
//processed the file
}
}

Get the last part of file name in C#

I need get last part means the numeric value(318, 319) of the following text (will vary)
C:\Uploads\X\X-1\37\Misc_318.pdf
C:\Uploads\X\X-1\37\Misc_ 319.pdf
C:\Uploads\X\C-1\37\Misc _ 320.pdf
Once I get that value I need to search for the entire folder. Once I find the files name with matching number, I need to remove all spaces and rename the file in that particular folder
Here is What I want
First get the last part of the file(numeric number may vary)
Based upon the number I get search in the folder to get all files names
Once I get the all files name check for spaces with file name and remove the spaces.
Finding the Number
If the naming follows the convention SOMEPATH\SomeText_[Optional spaces]999.pdf, try
var file = System.IO.Path.GetFileNameWithoutExtension(thePath);
string[] parts = file.split('_');
int number = int.Parse(parts[1]);
Of course, add error checking as appropriate. You may want to check that there are 2 parts after the split, and perhaps use int.TryParse() instead, depending on your confidence that the file names will follow that pattern and your ability to recover if TryParse() returns false.
Constructing the New File Name
I don't fully understand what you want to do once you have the number. However, have a look at Path.Combine() to build a new path if that's what you need, and you can use Directory.GetFiles() to search for a specific file name, or for files matching a pattern, in the desired directory.
Removing Spaces
If you have a file name with spaces in it, and you want all spaces removed, you can do
string newFilename = oldFilename.Replace(" ", "");
Here's a solution using a regex:
var s = #"C:\Uploads\X\X-1\37\Misc_ 319.pdf";
var match = Regex.Match(s, #"^.*?(\d+)(\.\w+)?$");
int i = int.Parse(match.Groups[1].Value);
// do something with i
It should work with or without an extension of any length (as long as it's a single extension, not like my file 123.tar.gz).

Filtered File Listing

i have a project to make and i want in option 2 the user to give an extension/keyword for example and the program to search the directory for this and print all the files that include this extension/keyword and i need some help if it's possible.
Then use another overload of the Directory.GetFiles() method which accepts a search pattern as a parameter.
Check it out on MSDN. And here is an example.
var files = Directory.GetFiles(#"c:\", "*.exe");
Obviously, replace that second argument with a variable name where you store an input from a user.
Or perhaps something like:
IEnumerable<string> results = filePaths.Where(x => x.EndsWith(".exe"));

C# - Check filename ends with certain word

Hi I would like to know how to validate a filename in C# to check that it ends with a certain word (not just contains but is located at the end of the filename)
e.g I want to modify certain files that end with the suffix Supplier so I want to validate each file to test if it ends with Supplier, so LondonSupplier.txt, ManchesterSupplier.txt and BirminghamSupplier.txt would all be validated and return true but ManchesterSuppliers.txt wouldn't.
is this even possible? I know you can validate a filename to test for a certain word anywhere within a filename but is it possible to do what i'm suggesting?
Try:
Path.GetFileNameWithoutExtension(path).EndsWith("Supplier")
if (myFileName.EndsWith("whatever"))
{
// Do stuff
}
By utilizing the System.IO.Path.GetFileNameWithoutExtension(string) method, you can extract the filename (sans extension) from a string. For example, calling it with the string C:\svn\trunk\MySourceFile.cs would return the string MySourceFile. After this, you can use the String.EndsWith method to see if your filename matches your criteria.
Linq solution:
var result = FilePaths.Where(name => Path.GetFileNameWithoutExtension(name).EndsWith("Supplier"))
.Select(name => name).ToList();
Assuming that FilePaths is a list containing all the paths.

Categories

Resources