Windows Batch:从文本文件设置变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5886334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:21:03  来源:igfitidea点击:

Windows Batch: Set Variables from Text File

windowstextbatch-filetext-files

提问by Dustin

Im currently looking for a method to set variables in a windows batch file from linkes in txt document.

我目前正在寻找一种从 txt 文档中的链接在 Windows 批处理文件中设置变量的方法。

So for example, if the text file reads:

例如,如果文本文件读取:

http://website1.com
http://website2.com
http://website3.com

I can hopefully output them to variables in the batch. Example:

我希望可以将它们输出到批处理中的变量中。例子:

set var1="Line one of text file, ex: http://website1.com"
set var2="Line two of text file, ex :http://website2.com"
set var3="Line three of text file, ex: http://website3.com"

Any help is appreciated, thanks in advance!

任何帮助表示赞赏,提前致谢!

回答by Navigatron

Here ya go! Have fun with this one.

给你!玩得开心。

(
set /p var1=
set /p var2=
set /p var3=
)<Filename.txt

Lands you with the same results!

以相同的结果降落您!

回答by Anders

The FOR /F loop command can be used to read lines from a text file:

FOR /F 循环命令可用于从文本文件中读取行:

@echo off
setlocal ENABLEDELAYEDEXPANSION
set vidx=0
for /F "tokens=*" %%A in (sites.txt) do (
    SET /A vidx=!vidx! + 1
    set var!vidx!=%%A
)
set var

You end up with:

你最终得到:

var1=http://website1.com
var2=http://website2.com
var3=http://website3.com