For Programmers: Free Programming Magazines  


Home > Archive > Tcl > February 2007 > List files of a specific type









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author List files of a specific type
Dave (from the UK)

2007-02-24, 7:06 pm

Is there any way in tcl to return a list of the files with a specific
extension in a given directory?

I know I could so use a shell command (ls or dir) to list them. For
example on UNIX I could do:

teal / % ls /export/home/drkirkby/*.pgn

/export/home/drkirkby/blatny_godena_1986.pgn
/export/home/drkirkby/twic633.pgn
/export/home/drkirkby/godena_smagin_1991.pgn
/export/home/drkirkby/twic634.pgn

but is there anything that does not rely on an external command, so
would return the above 4 files in a list?

--
Dave (from the UK)

Please note my email address changes periodically to avoid spam.
It is always of the form: month-year@althorne.org
Hitting reply will work for a few months only - later set it manually.

http://chessdb.sourceforge.net/ - a Free open-source Chess Database
Neil Madden

2007-02-24, 7:06 pm

Dave (from the UK) wrote:
> Is there any way in tcl to return a list of the files with a specific
> extension in a given directory?
>
> I know I could so use a shell command (ls or dir) to list them. For
> example on UNIX I could do:
>
> teal / % ls /export/home/drkirkby/*.pgn
>
> /export/home/drkirkby/blatny_godena_1986.pgn
> /export/home/drkirkby/twic633.pgn
> /export/home/drkirkby/godena_smagin_1991.pgn
> /export/home/drkirkby/twic634.pgn
>
> but is there anything that does not rely on an external command, so
> would return the above 4 files in a list?


set files [glob /export/home/drkirkby/*.pgn]

or more robustly:

set files [glob -nocomplain -types f \
-directory /export/home/drkirkby *]

The -nocomplain is needed to prevent glob throwing an error if no files
match the pattern.

-- Neil
Joachim Moskalewski

2007-02-24, 7:06 pm

"Dave (from the UK)" wrote:

> Is there any way in tcl to return a list of the files with a specific
> extension in a given directory?
>
> I know I could so use a shell command (ls or dir) to list them. For
> example on UNIX I could do:
>
> teal / % ls /export/home/drkirkby/*.pgn
>
> /export/home/drkirkby/blatny_godena_1986.pgn
> /export/home/drkirkby/twic633.pgn
> /export/home/drkirkby/godena_smagin_1991.pgn
> /export/home/drkirkby/twic634.pgn
>
> but is there anything that does not rely on an external command, so
> would return the above 4 files in a list?


I haven't done hugh tests on it, but:

puts [ glob -nocomplain [ file join ~ *.pgn ] ]

Ciao,
:) Jo
Gerald W. Lester

2007-02-24, 7:06 pm

Dave (from the UK) wrote:
> Is there any way in tcl to return a list of the files with a specific
> extension in a given directory?
>
> I know I could so use a shell command (ls or dir) to list them. For
> example on UNIX I could do:
>
> teal / % ls /export/home/drkirkby/*.pgn
>
> /export/home/drkirkby/blatny_godena_1986.pgn
> /export/home/drkirkby/twic633.pgn
> /export/home/drkirkby/godena_smagin_1991.pgn
> /export/home/drkirkby/twic634.pgn
>
> but is there anything that does not rely on an external command, so
> would return the above 4 files in a list?


Read the glob man/help page.

--
+--------------------------------+---------------------------------------+
| Gerald W. Lester |
|"The man who fights for his ideals is the man who is alive." - Cervantes|
+------------------------------------------------------------------------+
Dave (from the UK)

2007-02-24, 7:06 pm

Neil Madden wrote:
> Dave (from the UK) wrote:
>
>
>
> set files [glob /export/home/drkirkby/*.pgn]
>
> or more robustly:
>
> set files [glob -nocomplain -types f \
> -directory /export/home/drkirkby *]
>
> The -nocomplain is needed to prevent glob throwing an error if no files
> match the pattern.
>
> -- Neil


Thank you, that works great.

--
Dave (from the UK)

Please note my email address changes periodically to avoid spam.
It is always of the form: month-year@althorne.org
Hitting reply will work for a few months only - later set it manually.

http://chessdb.sourceforge.net/ - a Free open-source Chess Database
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com