How to GetFiles from multiple different paths - c#

Can we get files from different location using some Built-In Function in C# without any loop. Like if I have following paths
C:\Folder1
C:\abc\Folder2
D:\Folder3
I want to get all files from Folder1, Folder2 and Folder3 at same time without using any loop.

According to MSDN, you can search for files in a single directory.
For example:
Directory.GetFiles("C:\Folder1")
You just need to adapt, however an extension method is not possible since it's a static class.
More info here: http://msdn.microsoft.com/en-us/library/07wt70x2(v=vs.110).aspx
Basiccly, it means that a loop is required to do search for all the paths. Otherwise, it's not possible.

A loop is required there is no built-in function for that.
You can maintain List<string> for the purpose.
Example:
List<string> lstPaths = new List<string>();
lstPaths.Add(#"C:\Folder1");
lstPaths.Add(#"C:\abc\Folder2");
lstPaths.Add(#"D:\Folder3");
foreach(string sPath in lstPaths)
{
string[] arrFiles = Directory.GetFiles(sPath);
//you can loop through arrFiles here
}

Related

Reading a specific time file and writing the contents to another one

At runtime, I want to read all files who has the time stamp of a particular time. For example: if the application is running at 11:00:--, then it should read all files which is created after 11:00:00 till now(excluding the present one) and must write in the present file..I have tried like:
string temp_file_format = "ScriptLog_" + DateTime.Now.ToString("dd_MM_yyyy_HH");
string path = #"C:\\ScriptLogs";
var all_files = Directory.GetFiles(path, temp_file_format).SelectMany(File.ReadAllLines);
using (var w = new StreamWriter(logpath))
foreach (var line in all_files)
w.WriteLine(line);
But, this doesn't seems to be working.No error..No exception..But it doesn't read the files, while it exist.
The pattern parameter of the GetFiles method should probably also include a wildcard, something like:
string temp_file_format = "ScriptLog_" + DateTime.Now.ToString("dd_MM_yyyy_HH") + "*";
This will match all files starting with "ScriptLog_13_09_2013_11"
As #Edwin already solved your problem, I'd just like to add a suggestion regarding your code (mostly performance related).
Since you are only reading these lines in order to write them to a different file and discard them from memory, you should consider using File.ReadLines instead of File.ReadAllLines, because the latter method loads all lines from each file into memory unnecessarily.
Combine this with the File.WriteAllLines method, and you can simplify your code while reducing memory pressure to:
var all_files = Directory.GetFiles(path, temp_file_format);
// File.ReadLines returns a "lazy" IEnumerable<string> which will
// yield lines one by one
var all_lines = all_files.SelectMany(File.ReadLines);
// this iterates through all_lines and writes them to logpath
File.WriteAllLines(logpath, all_lines);
All that can even be written as a one-liner (that is, if you are not paid by your source code line count). ;-)

converting filenames to lower case

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());
}

Trim End of a UNC path to a backslash and then add to a list

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.

Create directory structure

Is there any way I can create a list with all the folders and files that are in a directory? I will specify the path and I want to list all its child folders and files, and write them in a txt file, or maybe an xml file to make it easier to read.
The Directory.GetFiles method should give you a list of all files, along with their full paths:
string[] filePaths = Directory.GetFiles(#"c:\MyDir\", "*.*", SearchOption.AllDirectories);
Directory.GetFiles and Directory.GetDirectories methods should help
This is a good link to get all files:
http://www.csharp-examples.net/get-files-from-directory/
And to get all directories: use Directories.GetFolders() instead.
Then you have to make a for loop or something to traverse them them.
Perhaps a recursive method would be a good selection. Something like
void PrintFilesAndFolders(string directory)...

Is there a wildcard expansion option for .net apps?

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.

Categories