473,505 Members | 15,626 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1456
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
1970
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
1855
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
1504
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
300
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
4190
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
252
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
2314
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
3384
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
1876
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
7367
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7018
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7471
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
5613
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,...
1
5028
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...
0
4699
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1528
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.