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
For loop gets line from file but can't re-assign it.
Moderator: DosItHelp
For loop gets line from file but can't re-assign it.
Last edited by bobluhrs on 08 Aug 2013 15:31, edited 4 times in total.
Re: For loop gets line from file but can't re-assign it.
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.
If all that code is inside the closing parenthesis then you need to use delayed expansion and reference your variables with exclamation points.
Re: For loop gets line from file but can't re-assign it.
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
So enableDelayedExpansion and use Excl. pts,
tnx,
will try this and let you know..
bob
Re: For loop gets line from file but can't re-assign it.
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
)
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.
Re: For loop gets line from file but can't re-assign it.
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.
Re: For loop gets line from file but can't re-assign it.
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
@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
Re: For loop gets line from file but can't re-assign it.
Code: Select all
@echo off & setlocal EnableDelayedExpansion
for /F "delims=" %%i in (output.txt) DO (
set name=%%i
set name=!name:~-34,15!
)
Re: For loop gets line from file but can't re-assign it.
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. 
