|
|
|
|
||||||
![]() |
|
|
LinkBack | Outils de la discussion |
|
|
#1 |
|
Messages: n/a
Hébergeur: |
Hi,
I would like to make the following script in ruby: Given a directory, for example /home, I would like to take ALL the subdirectories and files that are inside /home, and put them in a TreeView structure (from Ruby GTK), so I would have all those subdirectories and files arranged in that structure that I could then display as a graphic object. I have found how to print all the stuff that is inside my current directory: require 'find' Find.find('./') do |f| p f end I could insert all what that command prints in the TreeView but then it would be all at the first level of the TreeView. So I need to know, at each loop, what is at what level (which is the father, which is the son, etc), in order to insert it correctly in the TreeView. In other words, right now I am able to do this: /home/a /home/a1 /home/a2 /home/b etc And I would like to do this: /home/a /home/a1 /home/a2 /home/b Any ideas? Thanks. -- Posted via http://www.ruby-forum.com/. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
On May 12, 10:16 am, John Locke <eloi.pl...@campus.uab.es> wrote:
> So I need to know, at each loop, what is at what level (which is the > father, which is the son, etc), in order to insert it correctly in the > TreeView. > > In other words, right now I am able to do this: > > /home/a > /home/a1 > /home/a2 > /home/b > > etc > > And I would like to do this: > > /home/a > /home/a1 > /home/a2 > /home/b > > Any ideas? > > Thanks. > -- > Posted viahttp://www.ruby-forum.com/. What makes /home/a1 and /home/a2 the children of /home/a? They're not / home/a/1 or /home/a/2, are they? -- -yossef |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
Call a function recursively when you hit a directory. This will output something as you mention below, I am just using Dir[*] to get a list of files: def scan_files(dir, level=0) puts "#{' '*level}#{dir}" Dir["#{dir}/*"].each do |f| if File.directory?(f) then scan_files(f, level+1) else puts "#{' '*level}#{f}" end end end scan_files('/home/someone') Martin On Monday 12 May 2008 16:16:37 John Locke wrote: > Hi, > > I would like to make the following script in ruby: > > Given a directory, for example /home, I would like to take ALL the > subdirectories and files that are inside /home, and put them in a > TreeView structure (from Ruby GTK), so I would have all those > subdirectories and files arranged in that structure that I could then > display as a graphic object. > > I have found how to print all the stuff that is inside my current > directory: > > require 'find' > Find.find('./') do |f| p f end > > I could insert all what that command prints in the TreeView but then it > would be all at the first level of the TreeView. > > So I need to know, at each loop, what is at what level (which is the > father, which is the son, etc), in order to insert it correctly in the > TreeView. > > In other words, right now I am able to do this: > > /home/a > /home/a1 > /home/a2 > /home/b > > etc > > And I would like to do this: > > /home/a > /home/a1 > /home/a2 > /home/b > > Any ideas? > > > Thanks. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Yossef Mendelssohn wrote:
> On May 12, 10:16 am, John Locke <eloi.pl...@campus.uab.es> wrote: >> >> >> Thanks. >> -- >> Posted viahttp://www.ruby-forum.com/. > > What makes /home/a1 and /home/a2 the children of /home/a? They're not / > home/a/1 or /home/a/2, are they? Errr yeah well, that's the idea, maybe I expressed it bad. By the way I've found out how to make it: require 'gtk2' treestore = Gtk::TreeStore.new(String) view = Gtk::TreeView.new(treestore) view.selection.mode = Gtk::SELECTION_NONE renderer = Gtk::CellRendererText.new col = Gtk::TreeViewColumn.new("Filename", renderer, :text => 0) view.append_column(col) window = Gtk::Window.new(Gtk::Window::TOPLEVEL) scrollwindow = Gtk::ScrolledWindow.new scrollwindow.add(view) window.signal_connect("delete_event") { Gtk.main_quit; exit! } window.add(scrollwindow) window.show_all Dir.chdir('./') list = Dir['**/**'] hlist = Hash.new list.each { |file| parent_folder = File.dirname(file) parent_node = hlist[parent_folder] current_node = treestore.append(parent_node) current_node[0] = file hlist[file] = current_node } Gtk.main For the moment it seems to work. Now I have another question: actually all this comes because I have to provide a graphic structure for a copy server that allows me to copy entire directories. So when one directory is copied, I have to be able to see the whole list of subdirectories and files that are inside it, and next to each one of those files I have to display a ProgressBar that shows the progress of the copy of that specific file or directory, as well as a global progress bar for the copy of the whole directory. The specific stuff of the progress bar is provided by the copy server itself which is another part of the project; I just make the graphic interface. So my problem now is how to display a progress bar next to each node of the tree view. When I create the treestore to administrate the treeview, I say this: treestore = Gtk::TreeStore.new(String) But next to that string I would also like to display a progress bar, so how could I make it to introduce a progress bar (a widget, a graphic element), inside the treeview. treestore = Gtk::TreeStore.new(String, Widget) ---> obviously that doesn't work - any ideas of how to do it? Actually, is it possible, or the tree view only accepts normal stuff such as integers and strings in its nodes? Thanks. -- Posted via http://www.ruby-forum.com/. |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
John Locke wrote:
> Hi, > > I would like to make the following script in ruby: > > Given a directory, for example /home, I would like to take ALL the > subdirectories and files that are inside /home, and put them in a > TreeView structure (from Ruby GTK), so I would have all those > subdirectories and files arranged in that structure that I could then > display as a graphic object. > > I have found how to print all the stuff that is inside my current > directory: > > require 'find' > Find.find('./') do |f| p f end > > I could insert all what that command prints in the TreeView but then it > would be all at the first level of the TreeView. > > So I need to know, at each loop, what is at what level (which is the > father, which is the son, etc), in order to insert it correctly in the > TreeView. > > In other words, right now I am able to do this: > > /home/a > /home/a1 > /home/a2 > /home/b > > etc > > And I would like to do this: > > /home/a > /home/a1 > /home/a2 > /home/b > > Any ideas? > > > Thanks. Below code can be used to convert directory structure in xml. require 'rubygems' require 'rexml/document' class DirectoryStructure @@excludes = ["..","."] @@doc = REXML: ocument.new %{ <?xml version="1.0" encoding="UTF-8" ?> }def self.traverse_directory(root_path) #traverse initial directory structure folder = @@doc.add_element( 'myroot',{'id' => root_path} ) #call loop_path with root_path and parent_node loop_path(root_path,folder) #create xml file open( "final_directory_structure.xml", "w" ) do |f| f.puts @@doc end end def self.loop_path(path,parent_node) dir_entries= Dir.entries(path) # don't need '.','..' dir_entries = dir_entries-['.','..'] dir_entries.each do | entry | # check if directory or file if File.directory?(path+"/"+entry) #Folders Loop ------------------------------------------------- #Gets the directories of the current node. #It changes for each recursive call folder = parent_node.add_element( 'folder' ,{'label' =>entry ,'file_path' => path+"/"+entry } ) loop_path( path+"/"+entry, folder ) else #Files Loop ------------------------------------------------------ file = parent_node.add_element( 'file' ,{'label' =>entry ,'file_path' => path+"/"+entry } ) end end end #first call to traverse directory Please change the path---to your directory path self.traverse_directory("Authentication/Authentication") end -- Posted via http://www.ruby-forum.com/. |
|
![]() |
| Outils de la discussion | |
|
|