directories and files - is this impossible?

G

Guest

I need to write script in c# that will scan directories for files and insert
the files and directory names in the database.. I've have two tables tblDir
and tblDocs.

Example:
-Directory1
a_file1
a_file2
Directory1_1
b_file1
b_file2

-Directory2
a_file1
a_file2
Directory2_1
b_file1
b_file2

root_file1
root_file2

I want to record the file and directory list as shown below:

tblDir
---------------------------------
id | TopID | DirName |
---------------------------------
1 0 Directory1
2 1 Directory1_1
3 0 Directory2
4 2 Directory2_1

tblDocs
-----------------------------------
id | DirID | FileName |
-----------------------------------
1 0 root_file1
2 0 root_file2
3 1 a_File1
4 1 a_File2
5 2 b_File1
6 2 b_File1
7 3 a_File1
8 3 a_File2
9 4 b_File1
10 4 b_File1

Here is my codes.. i seem to be getting the folder records in a wrong
place.. please help.

int FolderID = 0;

foreach (string f in Directory.GetFiles(sDir))
{
file = f.Substring(f.LastIndexOf("\\")+1);
if(!doc.DocExist(DepID,FolderID,file))
{
doc.AddDoc(DepID,FolderID,file);
}
}

foreach (string d in Directory.GetDirectories(sDir))
{
folder = d.Substring(d.LastIndexOf("\\")+1);
if(!doc.FolderExist(DepID,FolderID,folder))
{
FolderID = doc.AddFolder(DepID,FolderID,folder);
}

foreach (string f in Directory.GetFiles(d))
{
folder = d.Substring(d.LastIndexOf("\\")+1);
file2 = f.Substring(f.LastIndexOf("\\")+1);
FolderID = doc.GetFolderID(folder);
if(!doc.DocExist(DepID,FolderID,file2))
{
DocID = doc.AddDoc(DepID,FolderID,file2);
}
}
DirSearch(d,DepID);
}
 
N

Nick Malik [Microsoft]

Directories are recursive. IOW, a directory can contain a fairly deep tree
of directories.
This means that code to look into directories is nearly always written in a
recursive method. This is much better way to do it because the code is
simpler and easier to debug.

For the sake of making this code simpler, I'm assuming that two classes
exist in the project: DocRecord and DirRecord that contain properties for
the fields you've defined. I'll demonstrate the jist of creating a set of
records of each type and adding them to seperate collections.

calling method:
// caveat: uncompiled air code
MyDirectoryCollection.Clear(); // clear out our class-level collection
of directories
MyFileCollection.Clear(); // clear out our class-level collection of
files
DirectoryNumber = 0; // class-level variable
FillDirectoryCollections(@"c:\MyRootDir", 0);

recursive method:
private void FillDirectoryCollections(string startingdir, int
CurrentDirId)
{
foreach (string fname in Directory.GetFiles(startingdir))
{
DocRecord dr = new DocRecord(); // see note above about
assumed classes.
dr.FileName = fname;
dr.DirId = CurrentDirId;
MyFileCollection.Add(dr);
}

foreach (string dname in Directory.GetDirectories(startingdir))
{
DirRecord ddr = new DirRecord(); // assume that the
DirRecord class has logic to create a new dir id when the object is created
ddr.TopID = CurrentDirId;
ddr.DirName = dname;
MyDirCollection.Add(ddr);
// now for the recursion
FillDirectoryCollections(dname,ddr.DirId); // pass in
subdir and it's id
}

}

That's pretty much it. If you take a look at the recursive method, then the
FillDirectoryCollections method is called by the root directory and each
directory under it. As the comments imply, I assumed that creating an
object of type DirRecord would have the class itself generate the unique id.
The same goes for DocRecord. The difference is that I actually _use_ the id
created for DirRecord in the call to get the subdirectories.

Caveat: this is air code. I'm trying to illustrate the point. Please
forgive syntax errors if any are found.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top