gpt4 book ai didi

batch-file - 为什么 forfiles 吞下命令的第一个参数?

转载 作者:行者123 更新时间:2023-12-03 13:19:03 24 4
gpt4 key购买 nike

我正在尝试做类似于 Get Visual Studio to run a T4 Template on every build 的事情使用 cmd 的 forfiles 转换 VS2008 中的每个模板。

如果我执行

forfiles /m "*.tt" /s /c "\"%CommonProgramFiles(x86)%\Microsoft Shared\TextTemplating\1.2\TextTransform.exe\" @file"

然后我收到 TextTransform.exe 的错误消息(文本屏幕解释了将其作为参数传递的内容)。

如果我改为执行

forfiles /m "*.tt" /s /c "cmd /c echo Transforming @path && \"%CommonProgramFiles(x86)%\Microsoft Shared\TextTemplating\1.2\TextTransform.exe\" @file"

然后它就完美地工作了。

为了对此进行调试,我创建了一个名为 debugargs 的简单命令行程序,它只打印它接收到的参数数量及其值。然后一些实验表明,直接将命令传递给 forfiles 的第一种形式导致第一个参数被吞没。例如

forfiles /m "*.tt" /s /c "debugargs.exe 1 2 3"

给出输出

2 arguments supplied
#1: 2
#2: 3

documentation我已经能够找到非常稀疏,而且我不认为有任何提及的可能性。这只是一个不明显的错误,还是我遗漏了什么?

最佳答案

这似乎是 forfiles 调用 .exe 的方式中的错误。凭直觉我扩展了我的 debugargs 程序来打印完整的命令行。

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"

因此最合适的解决方法是将可执行文件名称加倍:

forfiles /m "*.tt" /s /c "debugargs.exe debugargs.exe 1 2 @file"

另一种解决方法是使用 cmd/c 调用。但是,请注意,如果您需要引用可执行文件的路径(例如,因为它包含一个空格),您将需要一个额外的解决方法,即在 @ 之前添加:

forfiles /m "*.tt" /s /c "cmd /c @\"debugargs.exe\" 1 2 @file"

关于batch-file - 为什么 forfiles 吞下命令的第一个参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15817191/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com