从文件中读取.bat的命令行参数

时间:2022-10-10 15:12:56

I have a build.bat file which uses %1 internally... so you might call:

我有一个build.bat文件,它在内部使用%1 ...所以你可以调用:

build 1.23

I wanted it to read the parameter from a separate file, so I tried putting "1.23" in version.txt and doing:

我希望它从一个单独的文件中读取参数,所以我尝试在version.txt中添加“1.23”并执行:

build < version.txt

build

But it doesn't work. Isn't this how piping works? Is what I want possible and if so how?

但它不起作用。这不是管道的工作原理吗?我想要的是什么,如果是这样的话怎么样?

2 个解决方案

#1


1  

The FOR command in DOS has a form which parses files and assigns the tokens it finds to variables, which can then be used as arguments to other batch files. Assuming version.txt contains the single line "1.23", this batch file will open it, assign 1.23 to the variable, then call your original batch file, passing the variable's value as a command-line argument.

DOS中的FOR命令有一个表单,它解析文件并将它找到的标记分配给变量,然后可以将其用作其他批处理文件的参数。假设version.txt包含单行“1.23”,此批处理文件将打开它,为变量分配1.23,然后调用原始批处理文件,将变量的值作为命令行参数传递。

@echo off
for /f %%i in (version.txt) do call build.bat %%i

If version.txt contains more than one line, build.bat is called once for each line. Type help for at a DOS prompt to see what other features you might be able to use.

如果version.txt包含多行,则每行调用一次build.bat。在DOS提示符下键入help以查看您可以使用的其他功能。

#2


-1  

I think it would make more sense if you handle the file processing within the batch file on the off chance that version.txt is not edited correctly.

我认为,如果你没有正确编辑version.txt,那么在批处理文件中处理文件处理会更有意义。

You should modify your script to parse the file to get the version if the .bat file is executed as:

如果执行.bat文件,则应修改脚本以解析文件以获取版本:

     build FILE version.txt

#1


1  

The FOR command in DOS has a form which parses files and assigns the tokens it finds to variables, which can then be used as arguments to other batch files. Assuming version.txt contains the single line "1.23", this batch file will open it, assign 1.23 to the variable, then call your original batch file, passing the variable's value as a command-line argument.

DOS中的FOR命令有一个表单,它解析文件并将它找到的标记分配给变量,然后可以将其用作其他批处理文件的参数。假设version.txt包含单行“1.23”,此批处理文件将打开它,为变量分配1.23,然后调用原始批处理文件,将变量的值作为命令行参数传递。

@echo off
for /f %%i in (version.txt) do call build.bat %%i

If version.txt contains more than one line, build.bat is called once for each line. Type help for at a DOS prompt to see what other features you might be able to use.

如果version.txt包含多行,则每行调用一次build.bat。在DOS提示符下键入help以查看您可以使用的其他功能。

#2


-1  

I think it would make more sense if you handle the file processing within the batch file on the off chance that version.txt is not edited correctly.

我认为,如果你没有正确编辑version.txt,那么在批处理文件中处理文件处理会更有意义。

You should modify your script to parse the file to get the version if the .bat file is executed as:

如果执行.bat文件,则应修改脚本以解析文件以获取版本:

     build FILE version.txt