2

Given a folder on the local file system, what I need to do is this:

  • Get the recursive listing of all the subfolders/files in it
  • Output this in a flat text file
  • Then recreate this folder structure in a tree representation

So what information and how do I need to store it in that file in order to achieve this in an efficient manner?

Efficient manner in this case means spending as little time as possible creating that tree structure given a potential large number of subfolders/files. Obviously I would need to keep aware of the parent-child relationships between folders and perhaps something like file extensions and size.

I can use the facilities of Windows at the command line and/or other software so there is no limitation there.

This question might spill through in having someone recommend some library for the third step and going back from there, and I don't mind that as long as it's clear about the rest of the question.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user3046061
  • 353
  • 4
  • 14
  • How big is the number of files are you talking about? and what is "little time" and what is not so "little time"? I'd use C# to build tree structure in memory then serialise into JSON string and save into text. But this might not be efficient enough for your needs. – trailmax Feb 18 '14 at 23:15
  • about 7,000 would be an example. Reasonable time is probably a better way to put it. I'd hate to waste time for no reason. – user3046061 Feb 18 '14 at 23:25
  • I'm pretty sure the speed of the application will be capped by speed of IO reading all those files/folders. And actual serialisation and storing under 1 sec. Good enough? – trailmax Feb 18 '14 at 23:32
  • Of course, but you can't do anything about that. I was worried about the third step of not getting it right. Remember, creating that tree structure is the main goal. If you have a solution feel free to post it. – user3046061 Feb 18 '14 at 23:35

2 Answers2

1

You can represent the folders as a Json string:

{"name" : "folder_name1", "children" : [{"name" : "folder_name2", "children" : []}, {"name" : "folder_name3", "children" : [{"name" : "folder_name4", "children" : []}]}]}

You can then use any Json library to parse this string into a Json tree, which you can then traverse to generate your internal tree representation. Some libraries will even automatically (de)serialize the internal tree representation to/from a Json string.

Zim-Zam O'Pootertoot
  • 17,888
  • 4
  • 41
  • 69
1

Assumption: able to code C#

Regarding your comments, the question is not really an algorithm question, just plain implementation question.

To create a tree structure of your file system in memory I'd use this recursive approach, just like answer here.

And once you get the structure in memory, serialise that with Json.Net:

string json = JsonConvert.SerializeObject(treeStructure, Formatting.Indented);

this will produce you a text-representation of the tree you already created. And once you get the string, you can save it wherever you need to.

And to re-create the tree-structure from Json string, you use Json.Net again:

TreeView treeStructure = JsonConvert.DeserializeObject<TreeView>(json);

And this should be enough.

Community
  • 1
  • 1
trailmax
  • 34,305
  • 22
  • 140
  • 234