简体   繁体   中英

How to assign variables from text file in for loop of windows batch

@echo off
for /f "delims=" %%i in (servers.txt) do (
set server=%%i
echo server is %server%
)

Here it is unable to set value from text file to variable server.

It is setting the value. You are just not using it correctly. You need to use Delayed Expansion when you are inside a code block.

@echo off
setlocal enabledelayedexpansion
for /f "delims=" %%i in (servers.txt) do (
    set server=%%i
    echo server is !server! 
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM