By Dustin


2011-05-04 15:58:54 8 Comments

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!

2 comments

@Navigatron 2013-01-05 06:13:05

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!

@rahuldottech 2016-06-27 10:01:43

This is awesome!

@illusivetech 2018-01-25 21:40:36

Mucho grande divertida

@Anders 2011-05-04 16:33:18

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

@forsvarir 2011-05-04 16:41:22

Well, you learn something new everyday!

@Dustin 2011-05-04 16:46:28

Absolutely brilliant! You're a life saver. So simple too. Thanks a lot!

@kamlendra 2017-02-26 05:55:25

hi Anders, what if i want to take only website addrerss as out put? current output is "var1=website1.com" desired out put is "website1.com"

Related Questions

Sponsored Content

3 Answered Questions

[SOLVED] Defining and using a variable in batch file

2 Answered Questions

[SOLVED] Text editor to open big (giant, huge, large) text files

16 Answered Questions

[SOLVED] How can I pass arguments to a batch file?

18 Answered Questions

[SOLVED] How can I echo a newline in a batch file?

  • 2008-09-25 11:50:24
  • Brian R. Bondy
  • 856755 View
  • 562 Score
  • 18 Answer
  • Tags:   batch-file newline

23 Answered Questions

[SOLVED] Batch file to delete files older than N days

18 Answered Questions

[SOLVED] Why should text files end with a newline?

41 Answered Questions

[SOLVED] How do I find all files containing specific text on Linux?

14 Answered Questions

[SOLVED] Windows batch files: .bat vs .cmd?

2 Answered Questions

[SOLVED] Windows Batch: Loop through to echo a set of variables

  • 2013-09-03 03:59:37
  • Brian
  • 3440 View
  • 0 Score
  • 2 Answer
  • Tags:   batch-file

Sponsored Content