473,406 Members | 2,208 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

directories and files - is this impossible?

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

Jul 21 '05 #1
1 1447
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.
--
"huzz" <hu**@discussions.microsoft.com> wrote in message
news:5C**********************************@microsof t.com...
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);
}

Jul 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Tum | last post by:
Hi folks, I've been trying to make a decision and it's driving me crazy. Is a directory a file or is a directory NOT a file but a node? Should I have A)
3
by: Jamie Vicary | last post by:
Dear all, Despite a good few hours of googling, I have been unable to find out what the standard C++ libraries are for handling directories and getting a list of the files inside them. I am...
12
by: salvo | last post by:
Hi, I'd like to write a platform-indipendent method that lists all the files contained in a directory. Where do I start from?! Thanks in advance Salvo
1
by: huzz | last post by:
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...
4
by: rn5a | last post by:
I have a ListBox which should list all the files & directories that exist in a particular directory. The problem is I can get the ListBox to list either all the files or all the directories but not...
1
by: rn5a | last post by:
A ListBox lists all the folders & files existing in a directory named 'MyDir' on the server. Assume that the ListBox lists 2 directories - 'Dir1' & 'Dir2' i.e. these 2 directories reside in the...
12
by: Pao | last post by:
Hi all For all NEW sites (virtual directories) that I create, I receive always the same error: (I translate so may be a little different) Impossible to visualize the XML page Impossible to...
63
by: David Mathog | last post by:
There have been a series of questions about directory operations, all of which have been answered with "there is no portable way to do this". This raises the perfectly reasonable question, why,...
4
by: Edwin Velez | last post by:
http://msdn.microsoft.com/en-us/library/806sc8c5.aspx The URL above gives sample code for use within a Console Application. What I would like to do is use this code within a Windows Form. That...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.