使用Glob在TCL中复制多个文件 [英] Copying Multiple Files in TCL using glob
问题描述
我想使用tcl从一个文件夹复制所有文件。
这是我到目前为止编写的代码,但是它不起作用。有什么问题?
I want to copy all files from one folder using tcl. This is the code that I have written so far, but it does not work. What is the problem?
set files [glob -dir E:\Music *]
puts "Moving files"
foreach f { $files } {
file copy $f E:\
}
推荐答案
我以前的假设是错误的,但是不幸的是,我没有注意到您在 $ files
。与C之类的语言(其中括号或花括号是句法结构的一部分)不同,花括号用于在Tcl中引用。在这种情况下, $ files
是 glob
找到的文件列表,而 {$ files}
是文字字符串 $ files。由于没有名为 $ files的文件,因此尝试复制该文件时会出错。
My earlier assumption was wrong, and I unfortunately didn't notice that you were using braces around $files
. Unlike languages like C, where parentheses or braces are part of syntactic constructions, braces are used for quoting in Tcl. In this case, $files
is the list of files found by glob
, while { $files }
is the literal string " $files ". Since there is no file named " $files ", you get an error when you try to copy it.
反斜杠字符仍然会出现问题。在解释器准备评估命令的阶段,任何反斜杠字符都将作为转义序列的一部分读取。将反斜杠字符更改为常规斜杠( E:\
-> E:/
)可以解决您的问题。您也可以像这样 E:\\
或类似这样的 {E:/}
来引用它。在后一种情况下,花括号必须将整个字符串括起来,因此它将是 {E:\Music}
。
You are still going to have problems with the backslash character. During the phase when the interpreter prepares a command for evaluation, any backslash character is read as a part of an "escape sequence". Changing the backslash character to a regular slash (E:\
-> E:/
) may solve your problem. You can also quote it like this E:\\
or like this {E:/}
. In the latter case, the braces must enclose the whole string, so it would be {E:\Music}
.
(注意:评论中提到的 Hoodecrow是我,我之前用过这个昵称。)
(Note: the 'Hoodiecrow' mentioned in the comments is me, I used that nick earlier.)
这篇关于使用Glob在TCL中复制多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!