Best way to parse filenames and add to tree-view control?

Given a list of filenames:

Path/to/file1
Path/to/file2
Pathto/file3
etc

What is the best way to go about parsing all of this and adding it to a tree-view control while maintaining the folder structure? The tree-view should look like this:

1
2
3
4
5
Path
  ` to
     ` file1
     ` file2
  ` file3

The question is very unclear.

Do you know what parsing is?

What is path to? The directory in which the files are located? Do you mean something similar to:

1
2
3
4
5
6
C:
    Users
        User1
        User2
    Messages
        Msg1


Etc.
Last edited on
Sorry I should have been more clear. I'm using this:

http://www.codeproject.com/Articles/7530/Zip-Utils-clean-elegant-simple-C-Win

When I loop through all the entries in a zip file they appear like this in order:

Folder1/
Folder1/Subfolder1/
Folder1/Subfolder1/File1
Folder1/Subfolder1/File2
Folder1/Subfolder2/
Folder2/
Folder2/Subfolder1/
Folder2/Subfolder1/File1
Folder2/Subfolder1/Subfolder2/File1
....

I want to preserve the folder structure in a tree-view control. In other words I want to add Folder1 as a root node, and Subfolder1 and Subfolder2 as children and so on.

I know how to use the tree control but preserving the folder structure is my problem. It should look like this in the tree-view control:

1
2
3
4
5
6
Folder1/
     Subfolder1/
     Subfolder2/
Folder2/
     Subfolder1/
          Subfolder2/


For the record I am only adding folders to the tree-view and not files. The only solution I could come up with was to split each path into individual folder names and for each folder search the entire tree control to see if it's been added and add it if not. This won't work though as it takes way too long.
Ahh, well, in that case, you are parsing the folder entries. I am assuming they are strings, with that in mind, you should be able to use str.substr read up here:

http://www.cplusplus.com/reference/string/string/substr/
I already know about strings. The problem i'm having is adding folder names to the tree-view while preserving the folder structure of the zip file and doing it in a reasonable amount of time.

I know how to recursively search the tree-view but doing this for each and every folder takes too long(or maybe this is the correct way but my code is bad?). I thought about taking advantage of the fact that pathnames are always listed in order where the root folder is followed by subfolders(as I showed in my second post_) but I don't know...
Last edited on
Bump

So given this function:

HTREEITEM AddItem(HTREEITEM Parent, PWSTR Text);

And a listing of path names like this:

1
2
3
4
5
6
7
RootFolder1/
RootFolder1/SubFolder1/
RootFolder1/SubFolder1/SubFolder2/
RootFolder1/SubFolder3/
RootFolder2/
RootFolder2/SubFolder1/
etc


How would you enumerate and parse all the path names and use the AddItem function with the correct Parent argument?
I am not understanding your problem, if you want to put them in a list view, use a for or while loop and substr I linked above. In the loop, split the text with substr, then assign them separate variables, and cout the variables in the tree view or whatever you want to do with them, and where is the HTREEITEM AddItem function from? The Util you linked above?
The problem is maintaining the folder structure when adding each folder to the tree-view. The difficulty is getting the handle to the parent HTREEITEM. As you see there are a lot of repetitions in the path list I get from ziputil so I cant simply search through the entire tree for every folder in every path to find the right parent.

But anyway I started using std::map to associate full path names with HTREEITEM handles and it works wonderfully.
Last edited on
Assign the whole path, all of this:

1
2
3
4
5
6
RootFolder1/
RootFolder1/SubFolder1/
RootFolder1/SubFolder1/SubFolder2/
RootFolder1/SubFolder3/
RootFolder2/
RootFolder2/SubFolder1/


to a string array, the first line is str[1], second line is str[2], etc., loop though it until you reach the depth of the folder:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
string str[6] =
{
    "RootFolder1/"
    "RootFolder1/SubFolder1/"
    "RootFolder1/SubFolder1/SubFolder2/"
    "RootFolder1/SubFolder3/"
    "RootFolder2/"
    "RootFolder2/SubFolder1/"
};

for (int i = 0; i < 6; i++)
{
    unsigned pos = str[i].find("/");
    string str1 = str[i].substr(pos);
}


that is untested and unfinished, you can do the rest and debug.
Topic archived. No new replies allowed.