Page 1 of 1

First read from file always fails

Posted: 17 Feb 2019 08:39
by Birage
As titled, the first read from the file always fails, as the displayed output will be "ECHO is off." Subsequent reads will be fine, as the IP address in IP.txt file will be displayed.

Code: Select all

@echo off

for /F %%A in (C:\Users\user1\Desktop\IP.txt) do (
	set F=%%A
	echo %F%
)
Sample output when script is run 3x:
ECHO is off.
192.168.1.1
192.168.1.1

Re: First read from file always fails

Posted: 17 Feb 2019 10:48
by Squashman
Environmental variables that are created and need to be referenced in a parentheses code block need to use delayed expansion.

Code: Select all

@echo off
setlocal enabledelayedexpansion
for /F %%A in (C:\Users\user1\Desktop\IP.txt) do (
	set F=%%A
	echo !F!
)

Re: First read from file always fails

Posted: 17 Feb 2019 13:42
by elzooilogico
and more over, here simply

Code: Select all

echo(%%A
but, of course, details are not explicit

Re: First read from file always fails

Posted: 17 Feb 2019 20:34
by Birage
Thanks a lot for the tip guys ! setlocal enabledelayedexpansion did the trick.