Windows Batch: Set Variables from Text File

我的梦境 提交于 2019-11-26 11:19:38

问题


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

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!


回答1:


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

@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



回答2:


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!



来源:https://stackoverflow.com/questions/5886334/windows-batch-set-variables-from-text-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!