Regex to parse out filename and partial path, conditionally - c#

I have a C# app that uses the search functions to find all files in a directory, then shows them in a list. I need to be able to filter the files based on extension (possible using the search function) and directory (eg, block any in the "test" or "debug" directories from showing up).
My current code is something like:
Regex filter = new Regex(#"^docs\(?!debug\)(?'display'.*)\.(txt|rtf)");
String[] filelist = Directory.GetFiles("docs\\", "*", SearchOption.AllDirectories);
foreach ( String file in filelist )
{
Match m = filter.Match(file);
if ( m.Success )
{
listControl.Items.Add(m.Groups["display"]);
}
}
(that's somewhat simplified and consolidated, the actual regex is created from a string read from a file and I do more error checking in between.)
I need to be able to pick out a section (usually a relative path and filename) to be used as the display name, while ignoring any files with a particular foldername as a section of their path. For example, for these files, only ones with +s should match:
+ docs\info.txt
- docs\data.dat
- docs\debug\info.txt
+ docs\world\info.txt
+ docs\world\pictures.rtf
- docs\world\debug\symbols.rtf
My regex works for most of those, except I'm not sure how to make it fail on the last file. Any suggestions on how to make this work?

Try Directory.GetFiles. This should do what you want.
Example:
// Only get files that end in ".txt"
string[] dirs = Directory.GetFiles(#"c:\", "*.txt", SearchOption.AllDirectories);
Console.WriteLine("The number of files ending with .txt is {0}.", dirs.Length);
foreach (string dir in dirs)
{
Console.WriteLine(dir);
}

^docs\\(?:(?!\bdebug\\).)*\.(?:txt|rtf)$
will match a string that
starts with docs\,
does not contain debug\ anywhere (the \b anchor ensures that we match debug as an entire word), and
ends with .txt or .rtf.

Related

Get the first character from the filename in a filepath

I have an array of filepaths in a directory and I'm trying to move certain files based on alphabet.
string[] filePaths = Directory.GetFiles(#"C:\user\desktop\folder", "*.txt");
foreach (var file in filePaths)
{
if (file.StartsWith("A"))
{
//Move file
The obvious problem is that file.StartWith is pulling the entire filepath (C:\user\desktop\folder\Albert.txt) Which doesn't start with 'A'
So what would be the best way to just target the start of the actual file?
Thanks in advance.
I got it working with Path.GetFileName as per the suggestion by #Jimi
What about this code
var dir = new DirectoryInfo(#"C:\user\desktop\folder");
var files = dir.GetFiles();
foreach (var file in files)
{
if(file.Name.StartsWith("A"))
{
//Move file
You say you're looking to move files by alphabet- if you mean to put files into a folder whose name is the same as the first char of the filename then perhaps:
var desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); //don't hard code the path to the desktop
var root = Path.Combine(desktop, "foldernamehere"); //use path.combine to build paths
foreach(var f in directory.EnumerateFiles(root, "*.txt")){ //prefer EnumerateFiles over GetFiles
var filename = Path.GetFileName(f);
var dest = Path.Combine(root, filename.Remove(1));
Directory.CreateDirectory(dest); //safe to call even if exists, ensures exists
File.Move(f, Path.Combine(dest, filename));
}
See comments for more info
If you have a string that represents a full filename (or directory name), and you want the name without the directory, consider to use Path.GetFileName
string fullFileName = "C:\user\desktop\folder\Albert.txt";
string fileName = Path.GetDirectoryName(fullFileName);
fileName will be "Albert.txt"
With this in mind, your query will be easy:
IEnumerable<string> fullFileNames = ...
char startChar = 'A';
IEnumerable<string> fileNamesThatStartWithStartChar = fullFileNames
.Where(fileName => Path.GetDirectoryName(fileName).StartsWitch(startChar));
In words: from every fileName in the sequence of fullFileName, take the fileName without the directory information. Keep the fileName if this "fileName without directory information" starts with the startChar.
Note: StartsWitch(char) is case sensitive. If you want to check case insensitive, use String.StartsWitch(string, stringComparison)
There's room for improvement!
If you think that there might be a chance that you won't be using all information of all files, consider to use Directory.EnymerateFiles instead of GetFiles. This way, if at the end of your LINQ you decide to use only 3 of the fetched files (or worse: FirstOrDefault(), or Any()), you won't have fetched all files.

How to read all items on the Start menu

How can I get a list of all the shortcuts/programs in the start menu using C#?
I had to do this recently and surprisingly couldn't find a question for it anywhere on SO so thought I would share how I did it.
The following code reads the files from the special folder then LINQ to split the full pathname based upon the backslash character and takes the last element to get just the filename. It then splits the filename based upon the full stop (period) character and takes the first element to get the filename minus the extension.
Note the only difference between the All Users and Current User is the SpecialFolder name which is either CommonStartMenu or StartMenu.
Current User Start Menu
string startmenu = Environment.GetFolderPath(Environment.SpecialFolder.StartMenu);
IEnumerable<string> files = Directory.GetFiles(startmenu, "*.*", SearchOption.AllDirectories).Select(x => x.Split('\\').Last().Split('.').First());
foreach (var file in files)
{
Console.WriteLine(file);
}
Console.ReadKey();
All Users Start Menu
string startmenu = Environment.GetFolderPath(Environment.SpecialFolder.CommonStartMenu);
IEnumerable<string> files = Directory.GetFiles(startmenu, "*.*", SearchOption.AllDirectories).Select(x => x.Split('\\').Last().Split('.').First());
foreach (var file in files)
{
Console.WriteLine(file);
}
Console.ReadKey();

Search for file in c#

I wanted to search for files in c# begin with a string.
I followed the code in the internet
string[] dirs = Directory.GetFiles(#"c:\", "c*");
but instead of finding "c", I want to find files contains a string (i mean the file name for example contain.txt and contain.pdf both has "contain") i created. Here is my code
string filetofind;
string[] dirs = Directory.GetFiles(#"c:\", filetofind + "*");
but it just not working, is there anyway else?
If
I want to find files contains a string i created
means you want to check file's content (not name) You have to load the file, e.g. (assuming stringToFind doesn't have line breaks)
string[] dirs = Directory
.EnumerateFiles(#"c:\", "*.txt"); // all txt files (put the right wildcard)
.Where(file => File
.ReadLines(file) // with at least one line
.Any(line => line.Contains(stringToFind))) // which contains stringToFind
.ToArray();
Edit: In case you want files' names which contain c, e.g. "mycode.txt", "Constraints.dat" etc. (but not "demo.com" since c is in the file's extension); you can try *c*.* wild card: file name contains c with any extension:
string[] dirs = Directory
.GetFiles(#"c:\", $"*{filetofind}*.*");
In case of elaborated condition, when standard wildcard in not enough, just add Where:
string[] dirs = Directory
.EnumerateFiles(#"c:\", "*.*")
.Where(path => Your_Condition(Path.GetFileNameWithoutExtension(path)))
.ToArray();
For instance, let's test file name for small (not capital) letter c
string[] dirs = Directory
.EnumerateFiles(#"c:\", "*.*")
.Where(path => Path.GetFileNameWithoutExtension(path).Contains('c'))
.ToArray();
To find files where the file name contains "foo", use
var files = Directory.EnumerateFiles("C:\\dir", "*foo*", SearchOption.AllDirectories);
To find files where the text content contains "foo" use:
var files = Directory.EnumerateFiles("C:\\dir", "*", SearchOption.AllDirectories)
.Where(f => File.ReadAllText(f).Contains("foo"));
This should work, but it will read the entire file as text until you stop enumerating the list of files, so you might want to filter the file list search pattern before reading them. You could also write your own method to inspect each file rather than reading the entire thing into memory for every file.
Substitute SearchOption.AllDirectories for SearchOption.TopDirectoryOnly if you only want to search that directory, and not recursively search subdirectories.
if the file you want find starts with "filetofind" then code is correct. But if "filetofind" comes somewhere between the complete file name then your code must change to
string filetofind;
string[] dirs = Directory.GetFiles(#"c:\", "*filetofind*");

C# Split File Name Beginner Exercise

I have a directory filled with multiple excel files that I would like to rename. The names all have leading integers and a '-'. For example: 0123456-Test_01. I would like to rename all of the files within this directory by removing this prefix. 0123456-Test_01 should just be Test_01. I can rename a hard coded instance of a string, but am having trouble getting the files and renaming all of them.
My code is below. Any help is appreciated, as I am clearly new to C#.
public static void Main()
{
//Successfully splits hardcoded string
var temp = "0005689-Test_01".Split('-');
Console.WriteLine(temp[1]);
Console.ReadLine();
//Unsuccessful renaming of all files within directory
List<string> files = System.IO.Directory.GetFiles(#"C:\Users\acars\Desktop\B", "*").ToList();
System.IO.File.Move(#"C:\Users\acars\Desktop\B\", #"C:\Users\acars\Desktop\B\".Split('-'));
foreach (string file in files)
{
var temp = files.Split('-');
return temp[1];
};
}
There are some errors to fix in your code.
The first one is the wrong usage of the variable files. This is the full list of files, not the single file that you want to split and move. As explained comments you should use the iterator result stored in the variable file
The most important problem is the fact that the File.Move method throws an exception if the destination file exists. After removing the first part of your filename string, you cannot be sure that the resulting name is unique in your directory.
So a check for the existance of the file before the Move is mandatory.
Finally, it is better use Directory.EnumerateFiles because this method allows you to start the execution of your moving code without loading first all filenames in memory in a list. (In a folder full of files this could make a noticeable difference in speed)
public static void Main()
{
string workPath = #"C:\Users\acars\Desktop\B";
foreach (string file in Directory.EnumerateFiles(workPath)
{
string[] temp = file.Split('-');
if(temp.Length > 1)
{
string newName = Path.Combine(workPath, temp[1]);
if(!File.Exists(newName))
File.Move(file, newName);
}
}
}
Pay also attention to the comment below from CodeNotFound. You are using an hard-coded path so the problem actually doesn't exist, but if the directory contains a single "-" in its name then you should use something like this to get the last element in the splitted array
string newName = Path.Combine(workPath, temp[temp.Length-1]);

Resolving relative paths with wildcards in C#

In C#, if I have a directory path and a relative file path with wildcard, e.g.
"c:\foo\bar" and "..\blah\*.cpp"
Is there a simple way to get the list of absolute file paths? e.g.
{ "c:\foo\blah\a.cpp", "c:\foo\blah\b.cpp" }
Background
There is a source code tree, where any directory can contain a build definition file. This file uses relative paths with wildcards to specify a list of source files. The task is to generate a list of absolute paths of all source files for each one of these build definition files.
You can get the absolute path first and then enumerate the files inside the directory matching the wildcard:
// input
string rootDir = #"c:\foo\bar";
string originalPattern = #"..\blah\*.cpp";
// Get directory and file parts of complete relative pattern
string pattern = Path.GetFileName (originalPattern);
string relDir = originalPattern.Substring ( 0, originalPattern.Length - pattern.Length );
// Get absolute path (root+relative)
string absPath = Path.GetFullPath ( Path.Combine ( rootDir ,relDir ) );
// Search files mathing the pattern
string[] files = Directory.GetFiles ( absPath, pattern, SearchOption.TopDirectoryOnly );
It's simple.
using System.IO;
.
.
.
string[] files = Directory.GetFiles(#"c:\", "*.txt", SearchOption.TopDirectoryOnly);

Categories