简体   繁体   English

Windows 批处理:从文本文件设置变量

[英]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.

相关问题 如何从txt文档在Windows批处理文件中设置变量 - How to set variables in a Windows batch file from a txt document 如何在Windows批处理的for循环中从文本文件分配变量 - How to assign variables from text file in for loop of windows batch Windows批处理文件 - 拆分字符串以设置变量 - Windows batch file - splitting a string to set variables 使用批处理文件设置 windows 环境变量 - Set windows environment variables with a batch file 如何使用另一个批处理文件中的变量启动Windows批处理文件 - How to start a windows batch file with variables from another batch file 在安装软件后,寻找从批处理文件中设置Windows环境变量的更好方法 - Looking for a better way to set up Windows environment variables from batch file after installing a software ECHO没有显示变量,即使它们出现在SET - Windows批处理文件中 - ECHO is not showing variables, even though they appear in SET - Windows Batch File 从文本文件中更改值设置变量-Windows 批处理 - set variable from changing value in text file-Windows batch processing 如何为Windows批处理文件提供从CSV文件提取的变量 - How to supply a Windows batch file with variables pulled from a CSV file Windows 批处理文件命令和变量 - Windows batch file commands and variables
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM