[英]Windows Batch: Set Variables from Text File
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.我希望 output 可以将它们添加到批处理中的变量中。 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!感谢您的帮助,在此先感谢!
Here ya go.这里是 go。 Have fun with this one.
玩得开心。
(
set /p var1=
set /p var2=
set /p var3=
)<Filename.txt
Lands you with the same results!为您带来相同的结果!
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
Based on @Andres' answer, in case anyone is looking only for the values of the variables as an output:根据@Andres 的回答,以防万一有人只查找变量值 output:
@echo off
setlocal ENABLEDELAYEDEXPANSION
set vidx=0
for /F "tokens=*" %%A in (sites.txt) do (
SET /A vidx=!vidx! + 1
set var!vidx!=%%A
)
for /L %%I in (1,1,%vidx%) do (
echo !var%%I!
)
Pause
output: output:
http://website1.com
http://website2.com
http://website3.com
Of course this method is only helpful if you want to do some text manipulation or something, but it isn't the parctical way if you just want to print the contents of the text file.当然这种方法只有在你想做一些文本操作之类的时候才有用,但如果你只是想打印文本文件的内容,它就不是最实用的方法了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.