I am new to C# and I am trying to build a console application to copy a specific file type .e.g *.txt that is nested within subfolders to be copied to another directory.
The directory looks something like this
C:\V1.1\Folder_*\Folder\Folder\Folder\Filetype.txt
* meaning today's date
How would I get a list of files matching that pattern?
Try something like this.
var path = String.Format(#"C:\V1.1\Folder_{0}\Folder\Folder\Folder",
DateTime.ToString("dd-MM-yyyy"))
DirectoryInfo di = new DirectoryInfo(path);
var files=di.GetFiles("*.txt", SearchOption.AllDirectories)
You can check this site out for more information.
Thanks for all the help. I ended up doing this:
foreach (var file in Directory.GetFiles(sourceDir, "*.txt", SearchOption.AllDirectories))
File.Copy(file, Path.Combine(targetDir, Path.GetFileName(file)), true);
Related
Hello StackOverflow community,
I'm working for a C# web application that can show all necessary files in one folder. For example, you have a folder named "Maps" that stores all information about New York City. I will describe this folder here: The bolded word is folders.
Folder Maps:
->NewYorkCity
->>satellite.png
->>coordinates.txt
->>bridges.png
->>Road1
->>>satellite1.png
->>>roads.txt
->>>houses.png
As you can see, inside folder Maps we have folder NewYorkCity, and inside of this, we have folder Road1. Now I want to collect all files that have "*.png" type. It means I want to collect all images inside the root folder. The problem here is the algorithm to collect the file. I have thought to use "for loops" but I don't know the number of subfolders so I assumed it was impossible.
Here is the code to list the file with specified type that I have used but it works for files that in one folder and doesn't have any subfolders.
DirectoryInfo dInfo = new DirectoryInfo(zipPath); //Assuming Test is your Folder
FileInfo[] Files = dInfo.GetFiles("*.png"); //Getting Text files
string str = "";
foreach (FileInfo file in Files)
{
str = str + ", " + file.Name;
}
I hope you understand my question. Thank you.
You could start by reading the documentation, where you would find System.IO.DirectoryInfo.
Create a DirectoryInfo instance, and use, depending on what you want/need, any of its methods
EnumerateDirectories()
EnumerateFiles()
EnumerateFileSystemInfos()
Like so:
DirectoryInfo di = new DirectoryInfo(#"c:\Maps");
foreach (var fsi in di.EnumerateFileSystemInfos("*", SearchOptions.AllDirectories)
{
// Do something useful with fsi here
}
I would like to ask is there any way to get all the folder/directory name in a path:
My problem is:
I would like to get several folders in Local Disk C: but I don't want to use the following code to search all the folder in C: which causes the program stuck.
foreach (string dirPath in Directory.GetDirectories(source, "*", SearchOption.AllDirectories))
{
...
}
Try this:
using System.IO;
var directories = Directory.GetDirectories("c:\\");
This should give you all the directories at the root level of the C drive.
Try this:
string[] dirs = Directory.GetDirectories(#"c:\", "*", SearchOption.TopDirectoryOnly);
This should give you just top directory only.
Hello I am attempting to copy a directory and its subdirectories from my D drive to a shared network but I continue getting the error as
Exception:
Could not find a part of the path '/Projects/08.ASP.NETProjects/ProjectName/'.
My C# copy code:
System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo(D drive path);
var destinationpath = "file:///BZ0025BZV43/Projects/08.ASP.NETProjects/ProjectName/";
var uri = new Uri(destinationpath);
var destinationurl = uri.AbsolutePath;
foreach (System.IO.FileInfo mydirectory in directory.GetFiles())
mydirectory .CopyTo(destinationurl);
I am new to FileHandlers. Please help.
Try to use uri.LocalPath instead of uri.AbsolutePath.
Also, be aware that you are trying to copy a file into a directory. But you have to copy the file into another file of that directory. (Directories are files too, basically).
So to do that check if the target directory exists and create it when necessary. Then replace .copyTo(destinationUrl) with .copyTo(Path.Combine(uri.LocalPath, mydirectory.Name)) and you should be good to go. And please rename the variable mydirectory to file or something similar.
Also note that you are not copying the directory tree recursively. So if you want to recurse the tree, you have to check for subdirectories and copy the files in that hierarchy, too.
You can also check out this example: http://msdn.microsoft.com/de-de/library/bb762914(v=vs.110).aspx
There are many .bmp files present in my C:\TEMP directory.
I am using following code to delete all .bmp file in my C:\TEMP directory but somehow its not working as I expect. Can anyone help me in this?
string[] filePaths = Directory.GetFiles(#"c:\TEMP\");
foreach (string filePath in filePaths)
{
if (filePath.Contains(".bmp"))
File.Delete(filePath);
}
I have already checked that .bmp file and the directory has no read only attribute
For starters, GetFiles has an overload which takes a search pattern http://msdn.microsoft.com/en-us/library/wz42302f.aspx so you can do:
Directory.GetFiles(#"C:\TEMP\", "*.bmp");
Edit: For the case of deleting all .bmp files in TEMP:
string[] filePaths = Directory.GetFiles(#"c:\TEMP\", "*.bmp");
foreach (string filePath in filePaths)
{
File.Delete(filePath);
}
This deletes all .bmp files in the folder but does not access subfolders.
Should also use .EndsWith instead of .Contains
You Could write below code in fast way:
string[] t = Directory.GetFiles(Environment.CurrentDirectory, "*.pdf");
Array.ForEach(t, File.Delete);
or For Text Files:
string[] t = Directory.GetFiles(Environment.CurrentDirectory, "*.txt");
Array.ForEach(t, File.Delete);
So, You could write code for all extension and all directories.
This is a simple question and I am sure you C# Pros know it.
If you want to grab the contents of a directory on a hard drive (local or otherwise) in a C Sharp program, how do you go about it?
Call Directory.GetFiles.
If you mean get a directory then it is very simple. The following code will give you a list of all the directories in a given directory and then list all the files in the directory:
string dir = #"C:\Users\Joe\Music";
DirectoryInfo dirInfo = new DirectoryInfo(dir);
FileInfo[] files = dirInfo.GetFiles();
DirectoryInfo[] directories = dirInfo.GetDirectories();
foreach (DirectoryInfo directory in directories)
Console.WriteLine(directory.Name);
foreach (FileInfo file in files)
Console.WriteLine (file.Name);