Click here to Skip to main content
15,882,113 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi Guys,
I have a question about scanning directories for particular files.

I would like to be able to scan a particular folder for .avi files and add the file names into a sql database for use with another program.

Is it possible to scan a directory and have a "foreach" .avi file found, commit the data to sql?

I have an understanding on how to extract the filename using the System.IO.Path.GetFileNameWithoutExtension Method and enter the data into the sql database via linq i just can't figure out how to scan for files.

Thanks for the help in advance,
Alex
Posted
Comments
Member 11330626 20-Jan-15 6:07am    
how to scan the directory selected from the treeview in c#.net

Look at the DirectoryInfo.GetFiles() method. It does exactly what you need (returns an array of strings that represents the file names that exist in the folder in question.
 
Share this answer
 
string[] A = Directory.GetFiles(textBox1.Text, "*.txt", SearchOption.AllDirectories);
for (int i = 0; i < A.Length; i++)
listBox2.Items.Add(A[i]);

that my friend would be the code for the get all files ;)
it will give you all directories included in the folder!
explanation for:
Directory.GetFiles(textBox1.Text, "*.txt", SearchOption.AllDirectories);
Directory.GetFiles(Directory, file extension to look for, SearchOption);
 
Share this answer
 
v2
Brilliant! Thanks for your help John.
 
Share this answer
 
There is another method, by using recursive loop..

the different is, using
DirectoryInfo.GetFiles()
method, it will search all files in the particular directory then store in an array..then we will loop the array and scan/do function on each item in the array...

the purpose i'm doing the coding manually (recursive loop) is because everytime i encounter a file, i will scan/do some function on it...then store it in the array...

here's my coding..

declare array mypath to store choosen path to be scan for files
private string[] mypath;


store all choosen path in the array
mypath = File.ReadAllLines("scanpath.scn");


this will call the function to search all files in the directory and subdirectory
searchFile();


the function searchFile, loop root directory that i choose in scanpath file just now.
C#
private void searchFile()
        {
            for (int i = 0; i < mypath.Length ; i++)
            {
                DirectoryInfo myRoot = new DirectoryInfo(mypath[i]);
                string[] myDir = Directory.GetDirectories(mypath[i]);

                showFile(myRoot);
                recursiveLoop(myDir);

            }
        }



the function showFile, show/add the list of files in the directory into a listview
C#
private void showFile(DirectoryInfo myDir)
        {
            string myPath = "";
            string myPath2 = "";

            foreach (FileInfo file in myDir.GetFiles())
            {
                myPath = file.ToString();
                myPath2 = myDir + "\\" + myPath;

                lstScan.Items.Add(myPath2);
                string result = doFunction(myPath2);
                if (!result.Equals(string.Empty))
                {
                    addIntoListView(myPath, result);
                }
            }
        }


the function recursiveLoop, which will loop and search files in each subdirectory in the directory..and also subdirectory in the subdirectory..untill all files in the root directory and all its subdirectory'subdirectory and so on..
C#
private void recursiveLoop(string[] subdir)
        {
            foreach (string s in subdir)
            {
                try
                {

                    DirectoryInfo mySub = new DirectoryInfo(s);
                    string myPath = "";
                    string myPath2 = "";

                    foreach (FileInfo file in mySub.GetFiles())
                    {
                        myPath = file.ToString();
                        myPath2 = mySub + "\\" + myPath;

                        lstScan.Items.Add(myPath2);
                        string result = doFunction(myPath2);
                        if (!result.Equals(string.Empty))
                        {
                            addIntoListView(myPath, result);
                        }
                    }

                    string[] myDir = Directory.GetDirectories(s);
                    recursiveLoop(myDir);
                }
                catch (Exception c)
                {
                    //    Console.WriteLine("Error: " + c.Message);
                }
            }
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900