-1

I have code that is like this:

...
proc myProc {first last} {
    for { set i $first } { $i <= $last } { incr i } {
        set i_cur "PlainText$i"
        <command> [glob ./../myDir/${i_cur}*]
    }
}

When I run this, any file that has nothing after the number will run fine. But if there is something after the number then it doesn't. For example, I have the valid files named PlainText0.txt, PlainText00.txt, and PlainText1_Plaintext.txt. The first two work and PlainText1_Plaintext.txt doesn't.

Basically, I do not think I am using a glob/wildcard correctly, but don't know how.

Raj
  • 138
  • 1
  • 11
  • 2
    Works for me. Does work properly when passed multiple filenames? – Brad Lanam Oct 20 '16 at 01:24
  • 1
    @BradLanam, if I remove all of the text after the number (i.e. change `text1_stuff.txt` to `text1.txt` then it works like a charm and iterates through all of my files. If I do not, then it does not process). If I rename a portion of them, it iterates correctly though the renamed ones and does not work from the un-renamed ones and on. – Raj Oct 21 '16 at 22:52

1 Answers1

1

The usual issues with this sort of thing are when you've got the glob for what you want wrong, or when you've got a command that needs the list returned by glob expanded.

If it's that the command needs the list expanded, you need to use:

<command> {*}[glob ...]

That {*} in front of the bracket expands the results into multiple arguments. Sometimes, this instead requires you to iterate over the results and pass them in one at a time:

foreach filename [glob ...] {
    <command> $filename
}

When it comes to the glob itself, you're not quite clear whether PlainText1_stuff.txt is acceptable to you or not. However, it is matched by the pattern PlainText1*. If it isn't acceptable, maybe you need PlainText1.*; the extra . is important to what is matched here.

Also, consider using the -directory option to glob as it makes your code clearer (especially if you're on one of the platforms that allows glob metacharacters in filenames).


Overall, you're perhaps looking at something like this:

<command> {*}[glob -directory ../myDir PlainText$i.*]

You can use a helper variable if you want.

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
  • 1
    Perhaps the `-nocomplain` option would be applicable as well. – glenn jackman Oct 20 '16 at 14:45
  • @Donal Fellows, To clarify if `PlainText1_stuff.txt` is valid, I updated the question. It is: I have all of the files I want in a directory, and can iterate and run my (which is application specific. eg. QuestaSim is adding the file to a project and compiling an RTL source file), all of the files without more characters after the number work and all of the files with characters after do not. All of the files are valid and should work. – Raj Oct 21 '16 at 22:55
  • 1
    Then go with: foreach file [glob ..."PlainText*"] { do_something_with $file } . If you get more files then you wanted, either craft beter mask for glob, or use [string match] or [regexp] to check $file further. – Miloslav Raus Oct 24 '16 at 07:04