This is a lot, I know; trying to dig myself out of a hole at work.
Basically, I have many files across multiple servers that I need to get a hold of. Right now I'm running in to two problems, 1) I can't figure out the best way to search through multiple UNC paths. 2) I'm having to search by a partial name, it's possible that there is more than one file that matches, but I only want to use the file created in the last three days.
Here is my code so far. I'm not looking for someone to write it, but I would appreciate any logistical pointers.
uncPath1 = "\\server\share\";
string partial = "2002265467";
DateTime date = Convert.ToDateTime("10/5/2015");
DirectoryInfo a = new DirectoryInfo(uncPath1);
FileInfo[] interactionlist = a.GetFiles("*" + partial + "*.*", SearchOption.AllDirectories);
foreach (FileInfo f in interactionlist)
{
string fullname = f.FullName;
Console.WriteLine(fullname);
Console.Read();
}
You mentioned that you need to find only files made in the past 3 days. Instead of using Convert.ToDateTime and hard-coding the date in, you should use DateTime.Today.AddDays( -3 ) to get the date three days before the day the program is being run.
And of course, in your finding files method, compare the dates with something like:
DateTime time = DateTime.Today.AddDays( -3 );
if ( File.GetCreationTime( filePath ) > time ) {
// Add the file
}
1) You want to make a basic function that looks for a filespec in a single folder. You already wrote that in your code above, you just need to turn it into a function with parameters UNC path and filespec. Have the function take a third parameter of List<FileInfo> to add found files to.
2) If you need to search subfolders, create a function that will search a UNC path's subfolders by calling the function you wrote in #1, then getting a list of all folders, and calling itself for each folder found (and in turn, those calls will call for sub-subfolders, etc.) This is called recursion. Have this function take a List and add all found files to the List, by passing it to your #1 function.
3) Get the root UNC paths you want to search into a List or Array and then call foreach on them passing them, the filespec, and the intially empty List to the #2 function.
So:
bool FindFiles(string uncPath, string fileSpec, List<FileInfo> found);
bool FildFilesSubfolders(string uncPath, string fileSpec, List<FileInfo> found);
string fileSpec = "whatever";
string[] uncPaths = { "abc", "def" }; // etc
List<FileInfo> found = new List<FileInfo>();
foreach (string nextPath in uncPaths)
{
if (FindFilesSubfolders(nextPath, fileSpec, found))
break;
}
foreach (FileInfo f in found)
{
string fullname = f.FullName;
Console.WriteLine(fullname);
Console.Read();
}
One final thought: if you are searching subdirs and you are worried about two UNC paths that are essentially duplicates (e.g., c:\foo and c:\foo\foo2), you can use This method to check for paths within another path.
Edit: If you find something you are looking for and want to exit early, have the functions return a boolean meaning you found what you wanted to stop early. Then use break in your loops. I've edited the code.
Related
I'm trying to search for a specific value within an array of files. I'm not sure what I'm missing here, but some insight would be great.
I've tried containing all lines from each file into an array that can be read from within an if statement.
void getAssetTag()
{
string path = #"\\SERVER\SHARE\FOLDER";
DirectoryInfo d = new DirectoryInfo(path);//Grabbing Directory
FileInfo[] Files = d.GetFiles("*.txt"); //Getting Text files
foreach (FileInfo file in Files)
{
string[] asset = File.ReadAllLines(file.FullName);
if (asset.Contains(AssetT.Text) == true) {
string allinfo = File.ReadAllText(file.FullName);
Results.Text = allinfo;
}
}
}
The results should output the entire data from the text file contained within AssetT.Textinto Results.Text.
asset is a string[], where each string is a line of text. When you do if (asset.Contains(AssetT.Text)), you're comparing an entire line to AssetT.Text.
If you want to find out if any single line contains AssetT.Text, then we need to call Contains on the line, not the array:
if (asset.Any(line => line.Contains(AssetT.Text))
Also, you're ending up reading the file twice here, once when you do ReadAllLines, and again when you do ReadAllText. Since it seems you will always read the whole file (either to determine that the file doesn't contain the text, or to get all the contents because it does contain the text), you should just do it once.
If you use File.ReadAllText in the beginning, now we have a string representation of the entire file which we can call .Contains on:
foreach (FileInfo file in new DirectoryInfo(path).GetFiles("*.txt"))
{
string asset = File.ReadAllText(file.FullName);
if (asset.Contains(AssetT.Text))
{
Results.Text = asset;
// No use reading more files unless we're going
// to save the contents to another variable
break;
}
}
Note that we break out of the loop since it appears you're setting the contents of the file to a single field of some class, so searching for more files will just overwrite any previous results found.
This can be simplified further using System.Linq extension methods and method chaining. We can also use Directory.GetFiles (which returns a list of file paths) instead, since we don't need a full-blown FileInfo object:
Results.Text = Directory
.GetFiles(path, "*.txt")
.Select(File.ReadAllText)
.FirstOrDefault(fileText => fileText.Contains(AssetT.Text)) ?? Results.Text;
I have a folder structure similar to this:
HostnameA->DateTimeA->hashdumpDateTimeA.txt
HostnameA->DateTimeB->hashdumpDateTimeB.txt
HostnameA->DateTimeC->hashdumpDateTimeC.txt
HostnameA->DateTimeD->hashdumpDateTimeD.txt
My Goal:
Given a folder(in this case HostnameA) i need to:
1) Get each hashdumpDateTime.txt filename and place it in a String array -Assumed the file always exist in all folder-
2) Generate DropDownBox using the array (I can figure this out)
3) When user selects a filename via dropdownbox, it will fill my datagridview with the
contents (I can figure this out)
So my problem really is the #1 since i don't know how to make a loop to check the filename coming from a HostnameA folder, I need to know this since the DateTime of these filenames changes
I really appreciate the future help, thanks and cheers =)
You can use Directory.GetFiles method
var files = Directory.GetFiles("directoryPath","*.txt",SearchOption.AllDirectories)
That will give you all file names.If you don't want just names for example if you want file's full path, and some other attributes (like CreationTime, LastAccessTime) use DirectoryInfo class
DirectoryInfo di = new DirectoryInfo("path");
var files = di.GetFiles("*.txt",SearchOption.AllDirectories)
That will return an array of FileInfo instances.Then use a loop and do what you want with the files.
You doesn't need to know the exact name of DirectoryName or FileName, using a for loop and a searchPattern instead.
private string[] GetFileNames(string folder)
{
var files = new List<string>();
foreach (var dateTimeFolder in Directory.GetDirectories(folder))
{
files.AddRange(Directory.GetFiles(dateTimeFolder, "hashdump*.txt"));
}
return files.ToArray();
}
Is there an easy way to get a list of filenames that matach a filename pattern including references to parent directory? What I want is for "..\ThirdParty\dlls\*.dll" to return a collection like ["..\ThirdParty\dlls\one.dll", "..\ThirdParty\dlls\two.dll", ...]
I can find several questions relating matching files names including full path, wildcards, but nothing that includes "..\" in the pattern. Directory.GetFiles explicitly disallows it.
What I want to do with the names is to include them in a zip archive, so if there is a zip library that can understand relative paths like this I am happier to use that.
The pattern(s) are coming from an input file, they are not known at compile time. They can get quite complex, e.g ..\src\..\ThirdParty\win32\*.dll so parsing is probably not feasible.
Having to put it in zip is also the reason I am not very keen on converting the pattern to fullpath, I do want the relative paths in zip.
EDIT: What I am looking for really is a C# equivalent of /bin/ls.
static string[] FindFiles(string path)
{
string directory = Path.GetDirectoryName(path); // seperate directory i.e. ..\ThirdParty\dlls
string filePattern = Path.GetFileName(path); // seperate file pattern i.e. *.dll
// if path only contains pattern then use current directory
if (String.IsNullOrEmpty(directory))
directory = Directory.GetCurrentDirectory();
//uncomment the following line if you need absolute paths
//directory = Path.GetFullPath(directory);
if (!Directory.Exists(directory))
return new string[0];
var files = Directory.GetFiles(directory, filePattern);
return files;
}
There is the Path.GetFullPath() function that will convert from relative to absolute. You could use it on the path part.
string pattern = #"..\src\..\ThirdParty\win32\*.dll";
string relativeDir = Path.GetDirectoryName(pattern);
string absoluteDir = Path.GetFullPath(relativeDir);
string filePattern = Path.GetFileName(pattern);
foreach (string file in Directory.GetFiles(absoluteDir, filePattern))
{
}
If I understand you correctly you could use Directory.EnumerateFiles in combination with a regular expression like this (I haven't tested it though):
var matcher = new Regex(#"^\.\.\\ThirdParty\\dlls\\[^\\]+.dll$");
foreach (var file in Directory.EnumerateFiles("..", "*.dll", SearchOption.AllDirectories)
{
if (matcher.IsMatch(file))
yield return file;
}
I have a directory that contains jpg,tif,pdf,doc and xls. The client DB conly contains the file names without extension. My app has to pick up the file and upload the file. One of the properties of the upload object is the file extension.
Is there a way of getting file extension if all i have is the path and name
eg:
C:\temp\somepicture.jpg is the file and the information i have through db is
c:\temp\somepicture
Use Directory.GetFiles(fileName + ".*"). If it returns just one file, then you find the file you need. If it returns more than one, you have to choose which to upload.
Something like this maybe:
DirectoryInfo D = new DirectoryInfo(path);
foreach (FileInfo fi in D.GetFiles())
{
if (Path.GetFileNameWithoutExtension(fi.FullName) == whatever)
// do something
}
You could obtain a list of all of the files with that name, regardless of extension:
public string[] GetFileExtensions(string path)
{
System.IO.DirectoryInfo directory =
new System.IO.DirectoryInfo(System.IO.Path.GetDirectoryName(path));
return directory.GetFiles(
System.IO.Path.GetFileNameWithoutExtension(path) + ".*")
.Select(f => f.Extension).ToArray();
}
Obviously, if you have no other information and there are 2 files with the same name and different extensions, you can't do anything (e.g. there is somepicture.jpg and somepicture.png at the same time).
On the other hand, usually that won't be the case so you can simply use a search pattern (e.g. somepicture.*) to find the one and only (if you're lucky) file.
Search for files named somepicture.* in that folder, and upload any that matches ?
Get the lowest level folder for each path. For your example, you would have:
'c:\temp\'
Then find any files that start with your filename in that folder, in this case:
'somepicture'
Finally, grab the extension off the matching filename. If you have duplicates, you would have to handle that in a unique way.
You would have to use System.IO.Directory.GetFiles() and iterate through all the filenames. You will run into issues when you have a collision like somefile.jpg and somefile.tif.
Sounds like you have bigger issues than just this and you may want to make an argument to store the file extension in your database as well to remove the ambiguity.
you could do something like this perhaps....
DirectoryInfo di = new DirectoryInfo("c:/temp/");
FileInfo[] rgFiles = di.GetFiles("somepicture.*");
foreach (FileInfo fi in rgFiles)
{
if(fi.Name.Contains("."))
{
string name = fi.Name.Split('.')[0].ToString();
string ext = fi.Name.Split('.')[1].ToString();
System.Console.WriteLine("Extension is: " + ext);
}
}
One more, with the assumption of no files with same name but different extension.
string[] files = Directory.GetFiles(#"c:\temp", #"testasdadsadsas.*");
if (files.Length >= 1)
{
string fullFilenameAndPath = files[0];
Console.WriteLine(fullFilenameAndPath);
}
From the crippled file path you can get the directory path and the file name:
string path = Path.GetDirectoryName(filename);
string name = Path.GetFileName(filename);
Then you can get all files that matches the file name with any extension:
FileInfo[] found = new DirectoryInfo(path).GetFiles(name + ".*");
If the array contains one item, you have your match. If there is more than one item, you have to decide which one to use, or what to do with them.
All the pieces are here in the existing answers, but just trying to unify them into one answer for you - given the "guaranteed unique" declaration you're working with, you can toss in a FirstOrDefault since you don't need to worry about choosing among multiple potential matches.
static void Main(string[] args)
{
var match = FindMatch(args[0]);
Console.WriteLine("Best match for {0} is {1}", args[0], match ?? "[None found]");
}
private static string FindMatch(string pathAndFilename)
{
return FindMatch(Path.GetDirectoryName(pathAndFilename), Path.GetFileNameWithoutExtension(pathAndFilename));
}
private static string FindMatch(string path, string filename)
{
return Directory.GetFiles(path, filename + ".*").FirstOrDefault();
}
Output:
> ConsoleApplication10 c:\temp\bogus
Best match for c:\temp\bogus is [None found]
> ConsoleApplication10 c:\temp\7z465
Best match for c:\temp\7z465 is c:\temp\7z465.msi
> ConsoleApplication10 c:\temp\boot
Best match for c:\temp\boot is c:\temp\boot.wim
If I have a string variable that has:
"C:\temp\temp2\foo\bar.txt"
and I want to get
"foo"
what is the best way to do this?
Use:
new FileInfo(#"C:\temp\temp2\foo\bar.txt").Directory.Name
Far be it for me to disagree with the Skeet, but I've always used
Path.GetFileNameWithoutExtension(#"C:\temp\temp2\foo\bar.txt")
I suspect that FileInfo actually touches the file system to get it's info, where as I'd expect that GetFileNameWithoutExtension is only string operations - so performance of one over the other might be better.
I think most simple solution is
DirectoryInfo dinfo = new DirectoryInfo(path);
string folderName= dinfo.Parent.Name;
Building on Handleman's suggestion, you can do:
Path.GetFileName(Path.GetDirectoryName(path))
This doesn't touch the filesystem (unlike FileInfo), and will do what's required. This will work with folders because, as the MSDN says:
Return value: The characters after the last directory character in path. If the last
character of path is a directory or volume separator character, this
method returns String.Empty. If path is null, this method returns
null.
Also, looking at the reference source confirms that GetFilename doesn't care if the path passed in is a file or a folder: it's just doing pure string manipulation.
I had an occasion when I was looping through parent child directories
string[] years = Directory.GetDirectories(ROOT);
foreach (var year in years)
{
DirectoryInfo info = new DirectoryInfo(year);
Console.WriteLine(info.Name);
Console.WriteLine(year);
//Month directories
string[] months = Directory.GetDirectories(year);
foreach (var month in months)
{
Console.WriteLine(month);
//Day directories
string[] days = Directory.GetDirectories(month);
foreach (var day in days)
{
//checkes the files in the days
Console.WriteLine(day);
string[] files = Directory.GetFiles(day);
foreach (var file in files)
{
Console.WriteLine(file);
}
}
}
}
using this line I was able to get only the current directory name
DirectoryInfo info = new DirectoryInfo(year);
Console.WriteLine(info.Name);
It'll depend on how you want to handle the data, but another option is to use String.Split.
string myStr = #"C:\foo\bar.txt";
string[] paths = myStr.Split('\\');
string dir = paths[paths.Length - 2]; //returns "foo"
This doesn't check for an array out of bounds exception, but you get the idea.