问题描述
我试图做同样的事情来使用CMD的 FORFILES
变换VS2008中的每一个模板。
I'm trying to do something similar to Get Visual Studio to run a T4 Template on every build using cmd's forfiles
to transform each template in VS2008.
如果我执行
forfiles /m "*.tt" /s /c "\"%CommonProgramFiles(x86)%\Microsoft Shared\TextTemplating\1.2\TextTransform.exe\" @file"
然后我得到 TextTransform.exe
的错误消息(文字解释什么把它作为参数传递的画面)。
then I get TextTransform.exe
's error message (the screen of text explaining what to pass it as arguments).
如果我不是执行
forfiles /m "*.tt" /s /c "cmd /c echo Transforming @path && \"%CommonProgramFiles(x86)%\Microsoft Shared\TextTemplating\1.2\TextTransform.exe\" @file"
那么完美。
为了调试此,我创建了一个名为 debugargs
一个简单的命令行程序,它简单的打印参数接收它们的值的数量和。然后进行一些试验表明,直接将命令传递给 FORFILES第一种形式
导致被吞噬的第一个参数。例如。
In order to debug this, I created a simple command-line program called debugargs
which simply prints the number of arguments it receives and their values. Then some experimentation shows that the first form of directly passing the command to forfiles
causes the first argument to be swallowed. E.g.
forfiles /m "*.tt" /s /c "debugargs.exe 1 2 3"
给输出
2 arguments supplied
#1: 2
#2: 3
借助我已经能够找到相当稀疏,我没有看到这个任何提及作为一种可能性。难道仅仅是一个不起眼的错误,还是我失去了一些东西?
The documentation I've been able to find is quite sparse, and I don't see any mention of this as a possibility. Is it just an obscure bug, or am I missing something?
推荐答案
这似乎是在路上的一个错误 FORFILES
调用 .exe文件
秒。在一个预感我伸出我的 debugargs
程序打印完整的命令行。
This appears to be a bug in the way forfiles
invokes .exe
s. On a hunch I extended my debugargs
program to print the full command line.
X:\MyProject>forfiles /m "*.tt" /s /c "debugargs.exe 1 2 @file"
2 arguments supplied
#1: 2
#2: Urls.tt
Full command line: 1 2 "Urls.tt"
所以最合适的解决方法是将可执行文件的名称翻番:
So the most appropriate workaround would be to double the executable name:
forfiles /m "*.tt" /s /c "debugargs.exe debugargs.exe 1 2 @file"
另一种解决方法是用 CMD / C
来调用。不过,这里需要注意,如果你需要使用引号可执行文件的路径(例如,因为它包含空格),你需要的prepending 额外的解决办法@
:
The alternative workaround is to invoke with cmd /c
. However, note here that if you need to quote the executable's path (e.g. because it contains a space), you'll need an extra workaround of prepending @
:
forfiles /m "*.tt" /s /c "cmd /c @\"debugargs.exe\" 1 2 @file"
这篇关于为什么FORFILES吞命令的第一个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!