Windows Batch:从文本文件设置变量 [英] Windows Batch: Set Variables from Text File

查看:25
本文介绍了Windows Batch:从文本文件设置变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在寻找一种方法,可以通过 txt 文档中的链接在 Windows 批处理文件中设置变量.

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!

推荐答案

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

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

你最终得到:

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

这篇关于Windows Batch:从文本文件设置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆