This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

directory/subdirectory

Old posts that have not been replied to for several years.
s
shenot

directory/subdirectory

Post by shenot »

Hello everyone,

I'm a newbie in tcl and I need to be able to list the content of a directory files and eventual subdirectories.
I have a directory which contains several files and several subdirectories.
I managed to obtain a list of all the files thanks to the glob command but I don't know how to obtain a list with the subdirectories.
How can I do this?

Thank you by advance.

s.henot :)
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

well.. for dirs:

Code: Select all

catch { glob -type d /the/way/to/path/* } dirs
as for files:

Code: Select all

catch { glob -type f /the/way/to/path/* } files
In $dirs you will have the name of the dirs and in $files you will have the name of the files. Hope this helps.
Once the game is over, the king and the pawn go back in the same box.
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Re: directory/subdirectory

Post by user »

Try this little proc

Code: Select all

proc recursiveGlob {pattern {dir ./}} {
	set files [glob -nocomplain -types f -dir $dir $pattern]
	foreach d [glob -nocomplain -types d -dir $dir *] {
		set files [concat $files [recursiveGlob $pattern $d]]
	}
	set files
}
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

caesar wrote:

Code: Select all

catch { glob -type d /the/way/to/path/* } dirs
You should start listening to your own advice :) (learn what you don't know :)) As I said before in the weird thread, I recommend -nocomplain to avoid having to 'catch' the glob, and -dir to avoid matching problems. Remember that the last argument(s) to glob is/are used as a glob matching pattern.
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

The "catch" looks to me like a set, at least it dose exactly the same thing.. Till now I didn't had problems like you've said, anyway, sugestion noted. Thanks.
Once the game is over, the king and the pawn go back in the same box.
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

caesar wrote:The "catch" looks to me like a set, at least it dose exactly the same thing.
The catch will result having the error message stored in the variable if there are no matches and would require an "if" to determine the outcome. Usually you do some kind of loop through the list of names and since foreach doesn't execute its body when passed an empty list, using -nocomplain will lead to results most people find convenient :)
s
shenot

thanks

Post by shenot »

Thanks for your answers but I have met a problem with these commands:

catch { glob -type d /the/way/to/path/* } dirs

catch { glob -type f /the/way/to/path/* } files

both give me the same result and it's impossible for me to know whether an element is a file or a directory.

maybe I'm not using the options in the right way...

thanx for your help
p
ppslim
Revered One
Posts: 3914
Joined: Sun Sep 23, 2001 8:00 pm
Location: Liverpool, England

Post by ppslim »

Works for me

Code: Select all

% catch { glob -type f /users/ppslim/sand/* } files
0
% set files
/users/ppslim/sand/phpBB-2.0.6.tar.gz
% catch { glob -type d /users/ppslim/sand/* } dirs
0
% set dirs
/users/ppslim/sand/phpBB2
s
shenot

Post by shenot »

it still doesn't work and i don't understand why...
i obtain the same list of files and directories without distinction with both commands.
it's really weird if you tell me that it works...
s
shenot

Post by shenot »

the command :
glob -nocomplain d c:/*/

gives me:
c:/Config.Msi/ {c:/Documents and Settings/} c:/eMatrix9/ c:/I386/ c:/Inetpub/ c:/Lotus/ c:/MASTERS/ c:/MesSites/ {c:/Microsoft UAM Volume/} c:/MSSQL7/ {c:/OfficeScan NT/} c:/orant/ c:/orantold/ {c:/Program Files/} c:/RECYCLER/ {c:/System Volume Information/} c:/temp/ c:/WINNT/ c:/WUTemp/
first i have both directories and files (as usual) and second i don't understand why some directories are displayed between brackets whereas others are just displayed like c:/dir/

thank you for your answers... :D
O
Ofloo
Owner
Posts: 953
Joined: Tue May 13, 2003 1:37 am
Location: Belguim
Contact:

Post by Ofloo »

if i remember correctly you can't use it like that you should do this c:\path\\path\\ ..

or

c:/path//path// ..
XplaiN but think of me as stupid
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

shenot wrote:the command :
glob -nocomplain d c:/*/
You seem to have no idea what you're doing. Read the manual. The glob command returns a list of files, the {}'s are just the way tcl chose to represent your data in the list. To access single elements from this list, use a foreach loop/other loop combined with list commands/just list commands. You might want to take a look at the proc i posted too...if you combine that with reading the manual you might learn a thing or two.
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

Ofloo wrote:if i remember correctly
You don't.

In Tcl, backslash is the escape char and in most cases it will have to be escaped if you want it to be part of a string. The backslash char is also used for paths in dos, but when running Tcl through cygwin the paths are translated on the fly, so you can pretend you're on a UNIX machine and use forward slashes. (They don't need to be escaped.)
Last edited by user on Mon Sep 08, 2003 9:51 am, edited 1 time in total.
s
shenot

Post by shenot »

here's what your proc does when adapted to my situation:

bad switch "-files": must be -nocomplain or --

Code: Select all


and for your information i read the manual and i could'nt find what interested me that's why i came in that forum. if you're not able to discuss with newbies because they are too dumb for you you should try another forum.


thank you
User avatar
user
 
Posts: 1452
Joined: Tue Mar 18, 2003 9:58 pm
Location: Norway

Post by user »

shenot wrote:bad switch "-files": must be -nocomplain or --
Ah..you're using some older tcl version I guess. Then you'll have to loop through the list of files and check each one using 'file isdirectory' / 'file isfile'

something like this:

Code: Select all

proc rglob {pattern {dir ./}} {
   set files {}
   regsub -all {[\\\[\]\{\}\?\*]} $dir {\\&} dir
   foreach file [glob -nocomplain [file join $dir *]] {
      if {[file isfile $file]} {
         if {[string match $pattern $file]} {lappend files $file}
      } elseif {[file isdir $file]} {
         set files [concat $files [rglob $pattern $file]]
      }
   }
   set files
}
Being a newbie doesn't justify doing insane stuff like adding random arguments to a command. (like you did)
What version are you using btw?

EDIT: I added a regsub to escape any glob chars in the path. (This won't work under cygwin because of the transparent rewrite to backslashes in paths, so just remove the regsub and pray if you're using it on a win* box ;P ...or upgrade to a tcl version that supports the -dir option (for glob))
Locked