Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0 .NET3.5 WPF
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 30-Mar-10 0:30am
lxmyers536
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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 Wink | ;)
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);
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Brilliant! Thanks for your help John.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
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
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..
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);
                }
            }
        }
  Permalink  

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

  Print Answers RSS
0 Kornfeld Eliyahu Peter 169
1 Zoltán Zörgő 139
2 George Jonsson 135
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 1 Nov 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100