问题
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.
回答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.
来源:https://stackoverflow.com/questions/40143368/tcl-wildcard-glob-usage-within-file-name