If I have a path like this:
C:\Program Files (x86)\basic\data\
and in this path folders like this :
20160314_002_874
20160314_004_657
20160314_006_169
20160315_006_169
20160316_006_169
How to get all files names in these folders where the date part of these folders names = specific date
Ex :
I want all files names in folders which name begin with the following date(20160314)in a list.
Use DirectoryInfo:
using System.Collections.Generic;
using System.IO;
List<FileInfo> files = new List<FileInfo>();
DirectoryInfo rootDir = new DirectoryInfo(#"C:\Program Files (x86)\basic\data\");
var directories = rootDir.GetDirectories("20160314*");
foreach (var directory in directories)
{
files.AddRange(directory.GetFiles());
}
IEnumerable<string> fileNames = files.Select(f => f.Name);
use IEnumerable<string> fileNames = files.Select(f => f.FullName); to get the file name with path.
var dirs = Directory.GetDirectories(myPath).Where(x => x.StartsWith(20160314));
dirs.ForEach(xx => {
var fullPath = Path.Combine(myPath, xx);
var files = Directory.GetFiles(fullPath);
//Files is a string[], do whatever you want
});
And you will get all subdirectories that match with this date: 20160314. After that you can get the files by Combine the original path with the directory name and using GetFiles(string path) to get the filename list.
Related
I have an array with of folders in a Directory.
I want them displayed in the combo-box but I don't the full directory to display, I just want the folder names in the directory.
I haven't been successful with what I have tried
MY CODE
string[] filePaths = Directory.GetDirectories(#"\\Mgsops\data\B&C_Poker_Builds\Release_Location\Integration\SDL\SP\Prima\");
ProjectDir.DataSource = filePaths;
ProjectDir.SelectedItem.ToString();
MY RESULT
Look at the DirectoryInfo class - you can do something like this:
string folder = new DirectoryInfo(path).Name;
To get an array (using System.Linq), you could do the following:
string[] filePaths = Directory.GetDirectories("<yourpath>").Select(d => new DirectoryInfo(d).Name).ToArray();
Or, even use the DirectoryInfo class to enumerate your directories:
DirectoryInfo dir = new DirectoryInfo("<yourpath>");
string[] filePaths = dir.GetDirectories().Select(d => d.Name).ToArray();
I want to know which folder contains files which have a $ in their names.
But I will get duplicate folder names if I use this code:
string local = #"C:\test\";
string[] dirs = Directory.GetFiles(local, "*$*", SearchOption.AllDirectories);
foreach(string dir in dirs)
{
string a = Path.GetFileName(Path.GetDirectoryName(dir));
}
This is the content of the test folder:
C:\test\20170321\$123.txt
C:\test\20170321\2$4.txt
C:\test\20170322\567.txt
C:\test\20170322\abc.txt
should be get result only 1 20170321
This should do it:
string local = #"C:\test\";
string[] dirs = Directory.GetFiles(local, "*$*", SearchOption.AllDirectories);
List<string> singleDirNames = dirs.Select(x=> Path.GetDirectoryName(x)).Distinct().ToList();
Explanation: Select from all filenames the directory and take only the distinct values of it into a list
EDIT:
As I just realized, you don't want the entire path, so to get the result from your post you need to use the Path.GetFileName() (there should be another way, I am looking for it):
List<string> singleDirNames = dirs
.Select(x=> Path.GetFileName(Path.GetDirectoryName(x)))
.Distinct().ToList();
Found it. Here would be actually the direct way to access the containing folder inspired by this answer:
List<string> singleDirNames = dirs.Select(x=> new FileInfo(x).Directory.Name)
.Distinct().ToList();
I have a curious problem in a C#-program.
I have some local folderpaths like
"C:\test\AB_Systems\ELEGANCE\CB-DOC\live\M7-091.249.99.XX.01\extobjects".
Now i want to search for PDF-files in the subfolder called "extobjects".
Unfortunately there are many subfolders in the folder "live", which got a subfolder called "extobjects", so i thought it would be better to use a wildcard in the searchpath like that:
"C:\test\AB_Systems\ELEGANCE\CB-DOC\live\*\extobjects"
But this doesn't work.
Is there a way do do this?
public static FileInfo[] findFile(String whereToSearch, String searchFor , String mode)
{
IEnumerable<FileInfo> files = null;
if (mode.Equals(""))
mode = "s";
if (searchFor.Equals(""))
searchFor = "*";
if (mode.Equals("r") || mode.Equals("recursive"))
{
DirectoryInfo dir = new DirectoryInfo(whereToSearch);
files = dir.EnumerateFiles(searchFor, searchOption: SearchOption.AllDirectories);
}
if (mode.Equals("s") || mode.Equals("specific"))
{
DirectoryInfo dir = new DirectoryInfo(whereToSearch);
files = dir.EnumerateFiles(searchFor, searchOption: SearchOption.TopDirectoryOnly);
}
if (files != null) return files.ToArray<FileInfo>();
else return null;
}
That's an example how to do it.
It's important to say that only the filename can contain a wildcard pattern like *. The Path can be given as where to start the search and by giving searchOption: searchOption.AllDirectories as an argument it will go through all sub-directories of the entry path.
You will receive an Array of FileInfo which objects that contain the the path and more information.
You can use Linq like this:
var files = Directory
.EnumerateDirectories(#"C:\test\AB_Systems\ELEGANCE\CB-DOC\live", "extobjects", SearchOption.AllDirectories)
.SelectMany(x => Directory.EnumerateFiles(x, "*pdf", SearchOption.TopDirectoryOnly))
.ToArray();
I'd choose a solution exactly what BugFinder proposed, you could optimize the following foreach-loop into a LINQ query if your .NET target supports it.
// Itterate subdirectories of the live folder
foreach (var subDir in Directory.GetDirectories(#"C:\test\AB_Systems\ELEGANCE\CB-DOC\live"))
{
// Check if path to extobjects exists
var extObjects = Path.Combine(subDir, "extobjects");
if (Directory.Exists(extObjects))
{
var pdfFiles = Directory.GetFiles(extObjects, "*").Where(x=>x.EndsWith(".pdf"));
// Do something with the pdf file paths
}
}
In my C: drive I have a folder called "Temp" and in 3 text files called
abc.txt
pqr.txt
xyz.txt
Using the following code:
DirectoryInfo directory = new DirectoryInfo(#"C:\Temp");
FileInfo[] Files = directory.GetFiles("*.txt");
List<string> filenames = new List<string>();
foreach (FileInfo file in Files)
{
filenames.Add(file.Name);
}
I have got all the names of the files in a list. I also have a folder on the C: drive called "Results". Using this list, can someone tell me how I can create text files in the "Results" folder with the names:
result_abc.txt
result_pqr.txt
result_xyz.txt
You could simply do the following to create a set of new empty files from your input.
filenames.ForEach(f => File.Create(Path.Combine(#"C:\Results", "result_" + f)).Dispose());
Also, to get the filenames, you could have used the shorter
var filenames = Files.Select(fi => fi.Name).ToList();
instead of the foreach.
Finally, if you are not doing anything else with the Files, you could reduce this to:
foreach (var fi in new DirectoryInfo(#"C:\Temp").EnumerateFiles("*.txt"))
File.Create(Path.Combine(#"C:\Results", "result_" + fi.Name)).Dispose();
I have a directory which is present at #"F:\\Unzip\\" .This directory will contain a folder.Now as per my requirement i want to move the folder inside it to some other directory but i am not able to get it.Here is the code that i tried to read the folder name present in the directory but it is reading the file present in the folder.
if (Directory.Exists(#"F:\\Unzip\\") == true)
{
//Get the file name
List<String> files = Directory.GetFiles(#"F:\Unzip\", "*.*", SearchOption.AllDirectories).ToList();
string strr=files[0].ToString();
}
Please help me to move the folder present inside the directory.Thanks
You can try like this:
List<string> lst = new List<string>();
DirectoryInfo[] dir = new DirectoryInfo(#"C:\SomePath").GetDirectories("*.*", SearchOption.AllDirectories);
foreach(DirectoryInfo d in dir)
{
lst.Add(d.Name);
}
This will give you the list of all the folders in your directory.
Use this to get the names of subdirectories in the directory specified by "yourpath". The result is an array of strings.
Directory.GetDirectories("yourpath");
Easy as this:
string[] folders = System.IO.Directory.GetDirectories(#"F:\Unzip\","*", System.IO.SearchOption.AllDirectories);
Reference: http://msdn.microsoft.com/en-us/library/c1sez4sc.aspx
Directory.GetDirectories(#"F:\Unzip") will yield a string[] of full paths to each directory in F:\Unzip
read all directories without foreach loop using lambda
List<string> lst = new List<string>();
var allDir = new DirectoryInfo(#"D:\Github").GetDirectories("*.*", SearchOption.AllDirectories).Select(x=>x.Name).ToList();
lst = allDir;