Splitting folders and sub-folders to add to tree
-
@ Folder1
|
|--- Folder2
|--- Folder3
|--- Folder4
|--- Folder5
|--- Folder6
|--- Folder7
|--- Folder8
|--- Folder9@I need to split them so then i create the folders, so what i've tried is splitting the folders by "/". Leaving a QStringList like this: Folder3 subfolders = ("Folder4", "Folder5", "Folder8"). So then i do a loop and add them to the tree. But now if we look at Folder5 it has more sub-folders, so i would have to do the same process over and over until we don't find more "/". It can be an end-less process.
This is what i've done:
@ for(int counter = 0; counter < folders.length(); counter++) { // Loop though all folders.
if(folders[counter].contains("/")) { // If the folders[counter] contains a "/"
QStringList temp = folders[counter].split("/"); // We make a StringList splitting the "/", so, the temp has the sub-folders of the folders[counter]
QTreeWidgetItem *mainFolder = m_ProjectTree->addFolder(m_ProjectTree->projectItem(), temp[0]); // We add the main Folder.
for(int counter2 = 0; counter < temp.length(); counter2++) { // We loop though the sub-folders
m_ProjectTree->addFolder(mainFolder, temp[counter2]); // We add the sub-folders to the tree.
}
}@(I feel like it's very tricky :()
-
As you already noted that may be an endless process. Even so it is in principle possible to program it in one routine it is not very elegant. Certainly you to define on the folder depth and things.
This is a classical example for recursive routines. You need to write a routine considering only one folder and it sub-folders. When you have a sub-folder it becomes the folder for the next, recursive call to the very same routine and so on.
"Here is a very general article":http://www.cplusplus.com/forum/articles/2231/ on recursion. This may help to understand recursion. "Here is something similar":http://www.qtcentre.org/threads/4307-how-to-recursive-a-directory-in-qt of what you are trying. Use google, there probably more and better cases already posted somewhere.
-
Lets say i have a text file with all the folders: Text file contains this:
@Folder1/Folder2 // Folder1 contains a sub-folder called Folder2.
Folder1/Folder3 // Folder1 contains a sub-folder called Folder3.
Folder1/Folder2/Folder4 // Folder1 contains a sub-folder called Folder2 that it contains a SubFolder Called4 @Would that make the process easier? If so, how it would be?
-
Bump. :)
-
What is your input?
A file with the structure as shown in the first post?
Or the file with the structure as shown in your second post? -
What i mean is if i have all t he folders in a .txt file like this:
@Folder1/Folder2 // Folder1 contains a sub-folder called Folder2.
Folder1/Folder3 // Folder1 contains a sub-folder called Folder3.
Folder1/Folder2/Folder4 // Folder1 contains a sub-folder called Folder2 that it contains a SubFolder Called4@And then having files listed like this:
@Folder1/exe1.exe
Folder1/Folder2/exe2.exe
Folder1/Folder2/Icon.png
Folder1/Folder2/Folder4/exe3.exe
Folder1/Folder2/Folder4/Icon.png@Would ti make the process easier of creating folders and sub-folders?
-
Bumpy. I still can't figure out how to do that? (It actually confuses me. :/)