Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all files in a folder

Tags:

c#

file

directory

I am looking to create a program that finds all files of a certain type on my desktop and places them into specific folders, for example, I would have all files with .txt into the Text folder.

Any ideas what the best way would be to accomplish this? Thanks.

I have tried this:

string startPath = @"%userprofile%/Desktop"; string[] oDirectories = Directory.GetDirectories(startPath, ""); Console.WriteLine(oDirectories.Length.ToString());  foreach (string oCurrent in oDirectories)     Console.WriteLine(oCurrent);  Console.ReadLine(); 

It was not successful in finding all of the files.

like image 889
Oliver K Avatar asked Aug 08 '12 09:08

Oliver K


People also ask

How do I list all files in a Windows folder?

Type dir /A:D. /B > FolderList. txt and press Enter to generate a top-level folder list. When the list is complete, a new, blank prompt with a flashing cursor will appear.


1 Answers

A lot of these answers won't actually work, having tried them myself. Give this a go:

string filepath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); DirectoryInfo d = new DirectoryInfo(filepath);  foreach (var file in d.GetFiles("*.txt")) {       Directory.Move(file.FullName, filepath + "\\TextFiles\\" + file.Name); } 

It will move all .txt files on the desktop to the folder TextFiles.

like image 172
dtsg Avatar answered Sep 21 '22 07:09

dtsg