Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Filenames 2

Status
Not open for further replies.

geezer34

Technical User
Mar 17, 2004
11
GB
Hi guys,

Done some real basic things in TCL and have now want to do something slighty harder.

I'm cd'ing to a directory that I know has a number of files of a certain type eg jpgs. These have a format similar to the following:

picture01.jpg
picture02.jpg
...


What I want to do is to get hold of the complete filename of the first jpg file I find in the directory.

How do I do this???

I then want to end up with just the word picture ie remove the 01.jpg part. I'm planning on using the following

set tmp [string range $word 0 end-6]

Is this the best way of doing this?


Cheers
 
Several things here. First, to get a list of file names use glob. If you know that the extension is always ".jpg", then you can put all files with .jpg into a list:
set flst [glob *.jpg ]

Next you refer to "the first jpg file". What do you mean by "first"? At this point, the files in the list, flst, are arranged in the order that glob found them. You can sort them alphabetically:
set flst [lsort $flst]

Anyway, you can go through each member (file name) in the list using foreach file $flst { ... },
or you can get the "first" one in the list using lindex $flst 0

Now to the stripping part. You can get rid of the .jpg part with a file command:
set name [file rootname <filename>]

Now you have to know what to expect. If the number part will always be 2 digits, then your string range will work fine (without the preceding file stuff or with it and 6->2). If you can't depend on that you may have to go to regexp:
regexp {^[a-zA-Z]*} $name name
will take only alphabetical characters starting at the begining of "name".

_________________
Bob Rashkin
rrashkin@csc.com
 
Little bug :
The regexp would through away also have of the string if it contain chars you didn't think of Like the '_' in Bongs example .

Better solution would be to use the "string trim" command

set trim_name [string trim $name "1234567890"]

# I know this would surprise many but go check the "string trim" man pages

This would work also on
July_4th_picture001
my picture095
or anything you can (or cannot) think of



Amir
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top