I'm making a program which the user build directories (not in windows, in my app) and in these folders there are subfolders and so on; every folder must contain either folders or documents. What is the best data structure to use? Notice that the user may select a subfolder and search for documents in it and in its subfolders. And I don't want to limit the folders or the subfolders levels.
This is what I do:
Every record in the database has two fields: ID and ParentID. IDs are 4-5 characters (Base36, a-z:0-9 or something similar). Parent IDs are a concatenation of the parent's complete structure...
So...
This structure:
Root
Folder1
Folder2
Folder3
Folder4
Folder5
Folder6
Would be represented like this:
ID ParentID Name
0000 NULL ROOT
0001 0000 Folder1
0002 0000 Folder2
0003 00000002 Folder3
0004 0000 Folder4
0005 00000004 Folder5
0006 000000040005 Folder6
I like this structure because if I need to find all the files under a folder I can do a query like:
SELECT * FROM Folders WHERE ParentID LIKE '0000%' -- to find all folders under Folder1
To delete a folder and all its children:
DELETE FROM Folders WHERE ID='0004' AND ParentID LIKE '00000004%'
To move a folder and its children, you have to update all the records that use the same parent, to the new parent.
And I don't want to linit the folders or the subfolders levels
An obvious limitation to this is that the number of subfolders are limited to the size of your ParentID field.
I can think of a few ways you could structure this, but nothing would beat the obvious:
Use the actual file system.
I would look into using some sort of tree data structure
I should recommend B+ Tree .... You can easily use indexing (page,folder etc ) and all .
B+ Tree http://commons.wikimedia.org/wiki/File:Btree.png
for more info :
http://ozark.hendrix.edu/~burch/cs/340/reading/btree/index.html
I know that the question is specifically asking for a data structure but...
If you are using an object oriented language maybe you can use the composite design pattern which is ideally suited for this type of hierarchical tree like structure. You get what you are asking for.
Most OO languages come with some sort of abstraction for the file system, so there is where I would start. Then subclass it if you need to.
I would expect directories as an array of objects which are directories or files, for instance.
you can use m-way tree data structure
Related
I need to move a directory structure from an ext4 partition to a FAT partition. There are different rules for filenames for the two. For example, ext4 allows two files foo and Foo to be in the same directory and be unique. This is not true for FAT. I am looking to see if there is an algo already out there for converting names between the two. It is ok for the filename to be changed in my case, but I need to be able to read the new FAT name and be able to know what old ext4 name it corresponded with. So the algo has to be something that can be reversed. Does anyone know of such a thing?
On my "D:\" drive in Windows 7 I have a directory named "Downloads"
In this folder I have about 1400 folders with all kinds of funky/weird names with no real common denominator between them. What they do have in common is that they ALL contain one directory that has file contents.
Is there ANY way to either delete the root directories of those 1400 or so folders leaving only the subs (I need the one nested folder in there to remain intact name and content wise) OR is there any way to move every sub folder in those many directories just into "D:\Downloads" ? (I can shift select the 1400 useless directories and use the delete key afterwards.)
I'm slamming my face against the desk trying to figure this one out. Its going to take me DAYS to cut/paste all those little nested goobers into the downloads folder. I have VERY little programming experience (Basic on the TI-99/4A was as far as I got)
Thanks so much guys!
Crazy solution. Did a wildcard search in the downloads folder and sorted results by directory - was able to cut/paste all (1408) of the nested folders into the root!
I need to read some data from a file that follows a tree like structure( bookmarks and I guess you can say they follow the windows explorer dir /file structure )
After reading this data needs to be applied to an identical file . Is there a quick STL structure I can use to both load the tree info from the first file and then traverse it with the second file and write it out.
I want the list of all files given the folder name, i.e. it should traverse deep inside given the folder name ?
If I give any path like 'employee/app',it should list all files deep inside 'employee/app' folders
What about a nice typeglob:
p Dir['**/*']
I have a pet project where I build a text-to-HTML translator. I keep the content and the converted output in a directory tree, mirroring the structure via the filesystem hierachy. Chapters go into directories and subchapters go into subdirectories. I get the chapter headings from the directory and file names. I want to keep all data in files, no database or so.
Kind of a keep-it-simple approach, no need to deal with meta-data.
All works well, except for the sort order of the directories and files to be included. I need sort of an arbitrary key for sorting directories and files in my application. That would determine the order the content goes into the output.
I have two solutions, both not really good:
1) Prepend directories and files with a sort key (e.g. "01_") and strip that in the output files in order not to pollute the output file names. That works badly for directories since they must keep the key data in order not to break the directory structure. That ends with an ugly "01_Introduction"...
2) put an config file into each directory with information on how to sort the directory content, to be used from my applications. That is error-prone and breaks the keep-it-simple no meta-data approach.
Do you have an idea? What would you do?
If your goal is to effectively avoid metadata, then I'd go with some variation of option 1.
I really do not find 01_Introduction to be ugly., at all.