For loop gets line from file but can't re-assign it.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
bobluhrs
Posts: 5
Joined: 08 Aug 2013 13:24

For loop gets line from file but can't re-assign it.

#1 Post by bobluhrs » 08 Aug 2013 13:34

This works:
set name=kd -k kdsrv:server=@{tcp:port=5505,server=nebp2-nns0067,password=20254001},trans=@{com:pipe,port=\\.
\pipe\c5402e0c-19e8-4026-af46-e00c1d3fa082-DDPRTMCOOWMX03,resets=0,reconnect}

echo %name% (echoes correctly)

rem get last 34 chars
set name=%name:~-34%
echo %name% (echoes DDPRTMCOOWMX03,resets=0,reconnect})

rem get machine name first 15 chars from the last 34 chars
set name=%name:~0,14%
echo %name% (echoes DDPRTMCOOWMX03)

DDPRTMCOOWMX03 is the correct output, a machine name from this kernel debugger string.

The original string is a single line from a file 'output.txt'.
When used literally, I can parse and extract the machine name.

Now I use the FOR loop to read the same line, and assign to a variable then try to parse that.
1. the %%i variable in the FOR loop receives the right string (I tried echo %%i, works fine).
2. But when I go to assign %%i to a new variable ('name'), I find the new variable is blank, no string was received by it.
3. echo %name% returns blank.

FOR /F "delims=" %%i IN (output.txt) DO (
set name=%%i
echo %name% (this echoes a blank line)
)
since name does not receive the long string from %%i in the set statement, the rest of the parsing to extract the machine name also fails.

...anyone know what might be wrong and how to fix? I think it should work but many tries and no luck. I tried messing with setlocal diaable...enable...etc delayed..etc. no luck. Any working code snips welcome!
..thnx, Bob
Last edited by bobluhrs on 08 Aug 2013 15:31, edited 4 times in total.

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

Re: For loop gets line from file but can't re-assign it.

#2 Post by Squashman » 08 Aug 2013 14:41

Looks like you are missing a closing parenthesis.
If all that code is inside the closing parenthesis then you need to use delayed expansion and reference your variables with exclamation points.

bobluhrs
Posts: 5
Joined: 08 Aug 2013 13:24

Re: For loop gets line from file but can't re-assign it.

#3 Post by bobluhrs » 08 Aug 2013 16:00

parens are there, and I created a new file, in which the variable does work...not sure why...but ok.
So enableDelayedExpansion and use Excl. pts,
tnx,
will try this and let you know..
bob

bobluhrs
Posts: 5
Joined: 08 Aug 2013 13:24

Re: For loop gets line from file but can't re-assign it.

#4 Post by bobluhrs » 08 Aug 2013 16:15

getting closer...

This returns the machine name, but only for the last line in the file.
I have heard of this happening, not sure how code goes to fix it...

@echo off
for /F "delims=" %%i in (output.txt) DO (
setlocal EnableDelayedExpansion

set name=%%i

rem get last 34 chars
set name1=%name:~-34%

rem extract first 15 chars for machine name
echo %name1:~0,14%
endlocal
)
Last edited by bobluhrs on 08 Aug 2013 16:46, edited 1 time in total.

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

Re: For loop gets line from file but can't re-assign it.

#5 Post by Squashman » 08 Aug 2013 16:42

you dont want to be turning delayed expansion on and off like that inside the for loop. it does slow down your script. enable it before the loop. change the % to ! with your name variable.

bobluhrs
Posts: 5
Joined: 08 Aug 2013 13:24

Re: For loop gets line from file but can't re-assign it.

#6 Post by bobluhrs » 08 Aug 2013 18:18

thnx, i played with this and finally got one that works, maybe not best one...but it gets the commandline from the file, runs a start to open a window using the name contained within the commandline.

@echo off
setlocal EnableDelayedExpansion
for /f "tokens=* delims= " %%i in (output.txt) DO (

call :setWindowNameAndOpenWindow %%i

)

goto :eof

:setWindowNameAndOpenWindow
set name=%*
rem get last 34 chars
set name1=%name:~-34%
rem extract first 15 chars for machine name
rem echo %name1:~0,14%
set name2=%name1:~0,14%
echo %name2%
start "%name2%" %name%

:eof

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

Re: For loop gets line from file but can't re-assign it.

#7 Post by Squashman » 09 Aug 2013 06:00

Code: Select all

@echo off & setlocal EnableDelayedExpansion
for /F "delims=" %%i in (output.txt) DO (
set name=%%i
set name=!name:~-34,15!
)

bobluhrs
Posts: 5
Joined: 08 Aug 2013 13:24

Re: For loop gets line from file but can't re-assign it.

#8 Post by bobluhrs » 09 Aug 2013 11:48

aha, thanks, Squahman!!! I will give this a try. I see what you mean from the code better now. I like DOS batchfiles, just sometimes very hard to get something that seems so obvious. :)

Post Reply