473,657 Members | 2,504 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.GetFi les(sDir))
{
file = f.Substring(f.L astIndexOf("\\" )+1);
if(!doc.DocExis t(DepID,FolderI D,file))
{
doc.AddDoc(DepI D,FolderID,file );
}
}

foreach (string d in Directory.GetDi rectories(sDir) )
{
folder = d.Substring(d.L astIndexOf("\\" )+1);
if(!doc.FolderE xist(DepID,Fold erID,folder))
{
FolderID = doc.AddFolder(D epID,FolderID,f older);
}

foreach (string f in Directory.GetFi les(d))
{
folder = d.Substring(d.L astIndexOf("\\" )+1);
file2 = f.Substring(f.L astIndexOf("\\" )+1);
FolderID = doc.GetFolderID (folder);
if(!doc.DocExis t(DepID,FolderI D,file2))
{
DocID = doc.AddDoc(DepI D,FolderID,file 2);
}
}
DirSearch(d,Dep ID);
}

Jul 21 '05 #1
1 1470
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
MyDirectoryColl ection.Clear(); // clear out our class-level collection
of directories
MyFileCollectio n.Clear(); // clear out our class-level collection of
files
DirectoryNumber = 0; // class-level variable
FillDirectoryCo llections(@"c:\ MyRootDir", 0);

recursive method:
private void FillDirectoryCo llections(strin g startingdir, int
CurrentDirId)
{
foreach (string fname in Directory.GetFi les(startingdir ))
{
DocRecord dr = new DocRecord(); // see note above about
assumed classes.
dr.FileName = fname;
dr.DirId = CurrentDirId;
MyFileCollectio n.Add(dr);
}

foreach (string dname in Directory.GetDi rectories(start ingdir))
{
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
FillDirectoryCo llections(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
FillDirectoryCo llections 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**@discussio ns.microsoft.co m> wrote in message
news:5C******** *************** ***********@mic rosoft.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.GetFi les(sDir))
{
file = f.Substring(f.L astIndexOf("\\" )+1);
if(!doc.DocExis t(DepID,FolderI D,file))
{
doc.AddDoc(DepI D,FolderID,file );
}
}

foreach (string d in Directory.GetDi rectories(sDir) )
{
folder = d.Substring(d.L astIndexOf("\\" )+1);
if(!doc.FolderE xist(DepID,Fold erID,folder))
{
FolderID = doc.AddFolder(D epID,FolderID,f older);
}

foreach (string f in Directory.GetFi les(d))
{
folder = d.Substring(d.L astIndexOf("\\" )+1);
file2 = f.Substring(f.L astIndexOf("\\" )+1);
FolderID = doc.GetFolderID (folder);
if(!doc.DocExis t(DepID,FolderI D,file2))
{
DocID = doc.AddDoc(DepI D,FolderID,file 2);
}
}
DirSearch(d,Dep ID);
}

Jul 21 '05 #2

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

Similar topics

5
1977
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
1870
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 using the mingw c++ compiler on a windows machine to write a console application. system("mkdir blah") calls aren't what I need because I need return values to check for things going wrong.
12
1523
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 a_file1 a_file2 Directory1_1 b_file1
4
4221
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 the 2 of them together. This is what I tried: Sub Page_Load(.....) Dim dInfo As DirectoryInfo dInfo = New DirectoryInfo(Server.MapPath(MyDir))
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 'MyDir' directory. Both 'Dir1' & 'Dir2' also house sub-directories & files. Assume that the sub-directory 'Dir1' has 3 directories & 3 files. When a user comes to a ASPX page for the first time, the ListBox lists all the directories & files...
12
2331
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 visualize the XML input through the XSL sheet. Correct the error, then click on Update, or try another time.
63
3434
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, in this day and age, does the C standard have no abstract and portable method for dealing with directories? It doesn't seem like a particularly difficult problem. For instance, this int show_current_directory(struct DIRSTRUCT *current_directory);
4
1882
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 part is easy. The part that I am having trouble with is using the code in a form and having a Label's Text property update as a new directory or file is found. What I keep getting is the work being done before it is shown to the user via the...
0
8325
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8844
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8742
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8518
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7354
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6177
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5643
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2743
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 we have to send another system

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.