I am having some trouble searching directories for Files that have certain criteria in their file names. Below is my code and it captures the correct Files most of the time but sometimes skips the files it needs to capture. What is the best way to do this?
Many thanks.
So below I'm trying to capture All Files in a Directory that have the word "FINAL" and the correct Actual_Date. I have a datatable called dtResult2 that has these Actual_Dates stored in them.
foreach (DataRow drow in dtResult2.Rows)
{
//check for Null and start searching if not Null
if(drow["Well_Name"] != DBNull.Value)
{
//Now lets start searching the entire ARCHIVE folder/subfolders for a DWG that has
//this Well_Name and Actual_Date with FINAL in File Name...
DirectoryInfo myDir = new DirectoryInfo(myCollection3[v]);
//Collect the Final, Approved DWGs only...
var files = myDir.GetFileSystemInfos().Where(f => f.Name.Contains("FINAL") || f.Name.Contains(drow["Well_Name"].ToString()) || f.Name.Contains(drow["Actual_Date"].ToString()));
//More code not shown due to premise of question..
}
}
there is nothing wrong with the code probably the where condition is wrong.
I can suggest you to convert it into a classic for/foreach which allows you to debug the conditions and is also more performant. You can even convert it back to a LINQ expression as soon as you have identify the bug
Most likely the problem lies with the use of the Contains method which is case sensitive:
f.Name.Contains("FINAL") will not match "Final" it will only match "FINAL".
Use IndexOf for better results for case insensitive comparisons.
Related
I have a path that looks like the following:
C:\Program Files\AnsysEM\AnsysEm16.0\Win64
I am particularly interested in getting the "16.0" portion of the path. Is there a good way of doing this?
I was thinking of splitting the path, and extracting numbers from the 3rd element in the array. However, I am not sure how robust of a solution this is, or if there is a better way of doing this.
EDIT:
For a little more background, I am getting paths from the registry to determine the location of a couple of exe's. The paths I get are in the form shown above. I need to write these file locations as Environment Variable.
So, if the version is 16.0, I would write something in the registry like: DIR_16_0 with the value of it being the path.
It's a bit hacky, but you can search the string from the regex '\d+.\d+' to match anything of form XXX+.XXX+
var rg = new Regex(#"\d+\.\d+");
var matches = regex.Match(path);
var versionNumber = match.Groups.FirstOrDefault() //eg.
was hoping for some advice as to how to convert existing file names in a folder...all to lower case.
I felt that a good start would be to save the file names in a list and convert them all to lower.
How can I replace the existing file names in the folder to the lower case ones?
List<string> codes = new List<string>();
string[]productCodes = Directory.GetFiles(#"C:\Users\Ariang\Desktop\screenshotslowercase\screenshots");
codes = productCodes.ToList();
codes = codes.ConvertAll(t => t.ToLower());
This should work:
foreach (var file in Directory.GetFiles(#"C:\Temp\testrename"))
{
File.Move(file, file.ToLowerInvariant());
}
A few notes, first of all I have tested this and it works, somebody else mentioned using a temporary variable, but I haven't needed to do this.
Also, I have run this multiple times on the same directory, and I don't get an IOException the second or third time around, so I don't think any additional checking is necessary.
However, I am on Windows 8 and targeting .Net 4.5, things may be different on earlier versions of Windows or .Net.
Windows system doesn't see difference betweeen lower and upper letters in file names. Thats why you can't convert like "MyFile" -> "myfile". Use two steps instead:
foreach (var file in Directory.GetFiles(#"C:\Temp\testrename"))
{
var tempName = "." + file.ToLowerInvariant();
File.Move(file, tempName);
File.Move(tempName, file.ToLowerInvariant());
}
no need for list and all that. Simple read the file name from directory and use
System.IO.File.Move("oldfilename", "oldfilename".ToLower());
string[] files = Directory.GetFiles(dir);
foreach(string file in files)
{
System.IO.File.Move(file, file.ToLowerInvariant());
}
Let me start of by saying that my C# is pretty bad. I am working on a tool for my job (I hope others could find it useful too) that will create security groups in AD based on a provided file path from an SMB share, assign appropriate permissions on the directory (last folder in the file path), and then give the groups list permissions to each parent folder above it so users can navigate to the directory while not seeing other directories they have not been given access to (we use access based enumeration on our file servers, so they will not see any other folders unless they already have access to them by another security group, etc...). I have the first two parts working. So, right now the program creates the needed security groups in AD and assigns them the correct permissions on the directory for the path provided (\fileserver\some\example\shareddirectory).
What I think I need to do is basically make a list that will contain a path for each of the parent folders for the directory then use a foreach loop to assign the list permissions on the ACLs of each directory. So, using the example above, the list would include the following:
\\fileserver\some
\\fileserver\some\example
The number of parent folders can vary, so the number of items in the list could vary too.
First question: is making a list and then using a foreach loop a good way to do this or is there a better way?
Second question: how would I do this? So far, I have learned that dealing with backslashes is tricky because they are escape characters. I basically got stuck on trimming the string to remove the last backslash and the characters that follow it from the string before adding it to the list and how to do this recursively till there is nothing left to trim.
Thanks!
You can do something like the following
class DirectoryHelper
{
public List<string> GetDirectories(string path)
{
List<string> list = new List<string>();
if (!string.IsNullOrEmpty(path))
{
if (path.Last() != '\\')
{
path += "\\";
}
EnumerateDictories(list, path);
}
return list;
}
private void EnumerateDictories(IList<string> results, string path)
{
var parent = Directory.GetParent(path);
if (parent != null)
{
EnumerateDictories(results, parent.FullName);
results.Add(parent.FullName);
}
}
}
This is called like:
DirectoryHelper helper = new DirectoryHelper();
var dir = helper.GetDirectories(#"C:\Temp\Folder1\");
You have to add some code to protect against invalid directory strings I believe,
but you probably get the idea.
-update-
have edited some backslash handling, so that you do not have to care for it.
You know that in linux it's easy but I can't just understand how to do it in C# on Windows. I want to delete all files matching the wildcard f*.txt. How do I go about going that?
You can use the DirectoryInfo.EnumerateFiles function:
var dir = new DirectoryInfo(directoryPath);
foreach (var file in dir.EnumerateFiles("f*.txt")) {
file.Delete();
}
(Of course, you'll probably want to add error handling.)
I know this has already been answered and with a good answer, but there is an alternative in .NET 4.0 and higher. Use Directory.EnumerateFiles(), thus:
foreach (string f in Directory.EnumerateFiles(myDirectory,"f*.txt"))
{
File.Delete(f);
}
The disadvantage of DirectoryInfo.GetFiles() is that it returns a list of files - which 99.9% of the time is great. The disadvantage is if the folder contains tens of thousands of files (which is rare) then it becomes very slow and enumerating through the matching files is much faster.
You can use the Directory.GetFiles method with the wildcard overload. This will return all the filenames that match your pattern. You can then delete these files.
I appreciate this thread is a little old now, but if you want to use linq then
Directory.GetFiles("f:\\TestData", "*.zip", SearchOption.TopDirectoryOnly).ToList().ForEach(File.Delete);
I've used the setargv.obj linking for Expanding Wildcard Arguments in the past for a number of C and C++ apps, but I can't find any similar mention for .net applications.
Is there a standard way to have your app's command line parameters automatically wildcard expanded? (i.e. expand *.doc from one entry in args parameter to all that match that wildcard).
P.S. I've hacked something together with Directory.GetFiles() for my current little project, but it does not cover wildcards with paths (yet), and it would be nice to do it without custom code.
Update: here is my rough hack, for illustration. It needs to split the parameters for the path and name for the GetFiles(), but this is a general idea. Linking setargv.obj into a C or C++ app would basically do all the wildcard expansion, leaving the user to only iterate over the argv array.
public static void Main(string[] args)
{
foreach (string argString in args)
{
// Split into path and wildcard
int lastBackslashPos = argString.LastIndexOf('\\') + 1;
path = argString.Substring(0, lastBackslashPos);
filenameOnly = argString.Substring(lastBackslashPos, argString.Length - lastBackslashPos);
string[] fileList = System.IO.Directory.GetFiles(path, filenameOnly);
foreach (string fileName in fileList)
{
// do things for each file
}
}
}
Here us my rough hack. I'd love for it to be recursive. And having experienced the shortcoming of Windows wildcards I might decide to use regular expressions rather than letting GetFiles() do it for me.
using System.IO;
public static string[] ExpandFilePaths(string[] args)
{
var fileList = new List<string>();
foreach (var arg in args)
{
var substitutedArg = System.Environment.ExpandEnvironmentVariables(arg);
var dirPart = Path.GetDirectoryName(substitutedArg);
if (dirPart.Length == 0)
dirPart = ".";
var filePart = Path.GetFileName(substitutedArg);
foreach (var filepath in Directory.GetFiles(dirPart, filePart))
fileList.Add(filepath);
}
return fileList.ToArray();
}
I'm not sure exactly what you're after... but if I get where you're going with the Directory.GetFiles() "hack" you mentioned, then something like this might work:
var Dirs = Directory.GetDirectories(#"C:\Windows", "sys*",
SearchOption.TopDirectoryOnly).ToList();
var Files = new List<String>();
Dirs.ForEach(dirName => Files.AddRange(Directory.GetFiles(dirName, "*.sys", SearchOption.AllDirectories)));
The wildcard option on the GetDirectories call will allow you to grab all the directories contained in the Windows folder [directly] that match the pattern "sys*".
You can then iterate over those folders grabbing all the files that match the pattern "*.sys".
Is that the kind of thing you're looking for? To automatically expand the args, you'd have to extract the wildcards in some kind of meaningful manner and apply them to that model...
For instance:
RunMyApp "C:\Windows\Sys*\ *.sys"
You'd pull out the string C:\Windows - probably with a regular expression, find the lowest level directory that doesn't contain a wildcard and apply it to the GetDirectories method, attaching the wildcarded string as the search parameter.
Then if your end of string (in this case *.sys) as the search pattern for Directory.GetFiles.
If you wanted to get more complicated and do something like:
C:\Windows\*\Sys*\*.sys
You would use the SearchOptions to set this behaviour:
Directory.GetDirectories(#"C:\Windows", "sys*", SearchOptions.AllDirectories)
This would grab all directories that matched the sys* wildcard in the Windows directory and all directories below it.
If you wanted to get much more complicated than that, then I'm not sure how you would do that... for instance, say you wanted folders that are contained by folders directly inside the Windows directory - I have no idea how you would go about something like that I'm afraid...I don't imagine exporting the entire tree structure to XML and using XPath to do it would be so efficient - the XPath would be fabulously simple for parsing out using wildcards - but converting to XML wouldn't be so efficient...
see the source code of disassembled Microsoft.Build.Shared.FileMatcher class in Microsoft.Build.dll
you can get some idea from the implementation of method GetFiles.
as a client you may use the method as follows
var filelist = FileMatcher.GetFiles(#"c:\alaki",#"c:\alaki\**\bin\*.dll");
Your code looks like exactly how you're supposed to do it.