文件名中的 TCL 通配符/glob 用法 [英] TCL wildcard/glob usage within file name

查看:271
本文介绍了文件名中的 TCL 通配符/glob 用法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码:

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

当我运行它时,任何在数字后面没有任何内容的文件都可以正常运行.但如果数字后面有东西,那就没有了.例如,我有名为 PlainText0.txtPlainText00.txtPlainText1_Plaintext.txt 的有效文件.前两个有效,PlainText1_Plaintext.txt 无效.

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.

推荐答案

这类事情的常见问题是当你有你想要的错误时,或者当你有一个需要由 glob 返回的 list 展开.

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
}

说到 glob 本身,您不太清楚 PlainText1_stuff.txt 是否为您所接受.但是,它与模式 PlainText1* 匹配.如果不能接受,也许你需要 PlainText1.*;额外的 . 对这里匹配的内容很重要.

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.

另外,考虑使用 -directory 选项到 glob,因为它使您的代码更清晰(尤其,如果您使用允许在文件名中使用全局元字符的平台).

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.

这篇关于文件名中的 TCL 通配符/glob 用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆