First read from file always fails

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Birage
Posts: 7
Joined: 17 Feb 2019 08:32

First read from file always fails

#1 Post by Birage » 17 Feb 2019 08:39

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

Squashman
Expert
Posts: 4111
Joined: 23 Dec 2011 13:59

Re: First read from file always fails

#2 Post by Squashman » 17 Feb 2019 10:48

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!
)

elzooilogico
Posts: 113
Joined: 23 May 2016 15:39
Location: Spain

Re: First read from file always fails

#3 Post by elzooilogico » 17 Feb 2019 13:42

and more over, here simply

Code: Select all

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

Birage
Posts: 7
Joined: 17 Feb 2019 08:32

Re: First read from file always fails

#4 Post by Birage » 17 Feb 2019 20:34

Thanks a lot for the tip guys ! setlocal enabledelayedexpansion did the trick.

Post Reply