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;
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();
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.
I need to list all directories from a path. I need to get only last subdirectory path and not all paths.
i.e. for
pathbase/2016/01
I don't need pathbase/2016 but pathbase/2016/01
Now I'm using this code:
List<string> dirs = new List<string>(Directory.EnumerateDirectories(pathBase));
string[] entries = System.IO.Directory.GetDirectories(pathBase, "*",
SearchOption.AllDirectories);
var listDir = from dir
in entries
return listDir.ToList();
What type of filter can i use in Linq to exclude this directory?
Thanks.
Do you mean you need all leafs directories (directories that don't have subdirectories)?
If so this should do it
// First off, don't use GetDirectories on the static Directory class, it returns a string path and not a DirectoryInfo which isn't very usefull here, instead create a DirectoryInfo and go from there:
var RootDirectory = new DirectoryInfo(pathBase);
// Then queries all of it's subdirectories and filter on those
var listDir = RootDirectory.GetDirectories("*",SearchOption.AllDirectories)
.Where(dir=>!dir.GetDirectories().Any())
.ToList();
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();