for 循环中的多个 do 命令:将字符串回显到文件,然后重定向到命令窗口
我正在尝试编写一个批处理文件来迭代执行 Fortran 编译的可执行文件。通常,我们会进入 Windows 命令提示符,输入“Model.exe”。这将打开一个 dos 命令窗口,要求用户在 dos 提示符下直接在命令窗口中键入所需的文件名。
我想编写一个批处理文件来为我完成这一步,并迭代此步骤,以便我可以连续运行 10 次模拟,而不必手动执行。这种 shell 操作在 Linux 中很简单,但我没有可用的。
我的伪代码如下所示:
for /L %%run in (1,1,10) do
(设置str=Sim%%run
回声。%str%>输入.txt
模型.exe < input.txt)
您可以将其分解为以下步骤:
- 为变量“run”分配一个值。 (例如 1)
- 将其与字符串(“Sim”)连接以创建一个新变量,“Sim1”
- 将其回显到文本文件(“input.txt”)
- 从文件“input.txt”中读取变量“Sim1”
- 可执行文件走开并做它的事情。
- 重复步骤 1 -> 5,但使用新的变量调用“Sim2”等。
如果我使用 set str=Sim1 ,然后将其直接回显到“input.txt”,我可以使上述工作正常,但我无法得到这作为一个循环工作。我错过了什么吗?
最好的问候,
本
I am trying to write a batch file to iteratively execute a fortran compiled executable. Normally one would go to the windows command prompt, type 'Model.exe'. This would bring up a dos command window asking the user to type a required file name directly in to the command window at the dos prompt.
I want to write a batch file that will do this bit for me, and also iterate this step so that I can run 10 simulations consecutively instead of having to do it by hand. This kind of shell operation would be straightforward in linux, but I do not have this available.
My pseudo code would look like this:
for /L %%run in (1,1,10) do
(set str=Sim%%run
echo.%str% > input.txt
Model.exe < input.txt)
You could break this down in to the following steps:
- Assign variable 'run' a value. (e.g. 1)
- Concatenate this with a string ("Sim") to make a new variable, "Sim1"
- echo this to a text file ("input.txt")
- Read the variable "Sim1" from file "input.txt"
- Executable goes away and does its thing.
- Repeat steps 1 -> 5, but with a new variable calle "Sim2" etc.
I can get the above to work if I use set str=Sim1 and then echo this directly to "input.txt", but I cannot get this to work as a loop. Am I missing something?
Best regards,
Ben
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
呃,cmd.exe 对变量扩展的处理很丑陋。因此,您需要“延迟扩展”,如下所示:(
当然,在这种特殊情况下,您可以只说
echo Sim%%i > input.txt
但我认为您想要的有一个很好的理由通过另一个变量。)Ugh, cmd.exe's treatment of variable expansion is ugly. So, you need "delayed expansion", as follows:
(Of course in this particular case you could just say
echo Sim%%i > input.txt
but I assume there's a good reason why you want to go via another variable.)