Solved Why my batch file selecting more than one line of text?

October 26, 2016 at 10:33:39
Specs: Windows 7
http://www.computing.net/howtos/sho...

On this post above, i'm having an issue. My batch files has {as the delimiter. There are two instances of that character in the text it's pulling from. Rather than echoing, it is supposed to be Setting a variable to the text that it pulls. It does this, but then it sets the variable again to the second one. I need it to stop at the first and not over write the first variable it receives. It basically just Sets the variable twice and I need the data from the first variable.

for /f "tokens=2 delims={" %%a in (%X%.txt) do Set ID=%%a Set TPM=%%b
echo %ID%
echo %TPM%

TPM isn't getting set. I really don't even need TPM, I just tried that %%b after relating it to this article. It looks like it would work if I was echoing it, but not if i'm setting.

message edited by scionave


See More: Why my batch file selecting more than one line of text?

Reply ↓  Report •


#1
October 26, 2016 at 13:00:14
✔ Best Answer
for /f "tokens=2 delims={" %%a in (%X%.txt) do (
Set ID=%%a
Set TPM=%%b
goto :break
)
:break
echo %ID%
echo %TPM%

Like this?

Adding a break in the loop prevents it from looping

%%b is empty because you have only specified one token, in order to have %%b not be empty, you'd need something like
tokens=1,2 or tokens=2,3 or tokens=1,* (basically specified two or more tokens)


Don't worry if plan A fails, there are 25 more letters in the alphabet ;)

message edited by RainBawZ


Reply ↓  Report •

#2
October 26, 2016 at 13:12:50
For some reason, when I do this, The ID variable gets set to "ID=(the giant number)}"
and TPM gets set to "%b"
I'm not sure why the actual part "ID=" is being added to the variable.

message edited by scionave


Reply ↓  Report •

#3
October 26, 2016 at 13:15:28
I edited my reply, it should explain why. If you didn't need the TPM variable, you don't really have do anything about it.

Don't worry if plan A fails, there are 25 more letters in the alphabet ;)


Reply ↓  Report •

Related Solutions

#4
October 26, 2016 at 13:21:25
The only reason I created the TPM variable is because I was trying to figure out somewhere to put the remaining data. If it looks like this :
for /f "tokens=2 delims={" %%a in (%X%.txt) do Set ID=%%a

It Sets the variable once to the text I want, and then it overwrites the original variable to the second delimiter which is odd since i only as you said specified the 2 token.


For example:
if i'm having it read hostlist.txt
it has two things in the text file that have { as a starting token.

It reads the hostlist.txt and in the command line, it shows

Set ID=(the number string i'm looking for)

Set ID=(the number string that I don't want)

Echo=(the number string it last added and not the one I want)

message edited by scionave


Reply ↓  Report •

#5
October 26, 2016 at 13:34:47
for /f "tokens=2 delims={" %%a in (%X%.txt) do (
        Set ID=%%a
        goto :break
)
:break

You still need the break in there, otherwise it'll keep changing the value.

Don't worry if plan A fails, there are 25 more letters in the alphabet ;)


Reply ↓  Report •

#6
October 26, 2016 at 19:41:46
It looks like maybe the misunderstanding is here:
"it has two things in the text file that have { as a starting token."
"things" may be interpreted as tokens on one line, but it could also mean two different lines, and only the first token, and you want only the first token from the first line? A sample or proximity of the file would be very helpful.
Meantime, maybe just use "find" to locate the first instance, then take the second token:
for /f "tokens=2 delims={" %%a in ('find "{"^<%x%.txt') do (
set out=%%a
goto :display
)
:display
echo data extracted: %out%

Since enclosures are not typically used without the end-closure, you might need to add
} to the tokens to keep extra unwanted data from being included:
for /f "tokens=2 delims={}" %%a in ('find "{"^<%x%.txt') do ( etc...
but that's your call

message edited by nbrane


Reply ↓  Report •

#7
October 28, 2016 at 06:07:55
The file it is reading looks like this:
BitLocker Drive Encryption: Configuration Tool version 6.1.7601
Copyright (C) Microsoft Corporation. All rights reserved.

Computer Name: COMPUTER-NAME-D01

Volume C: [DRIVE_C]
All Key Protectors

Numerical Password:
ID: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
Password:
010101-010101-010101-010101-010101-010101-010101-010101

TPM:
ID: {YYYYYYYY-YYYY-YYYY-YYYY-YYYYYYYYYYYY}

The personal information has been overwritten with y's, x's, and 01s.


Reply ↓  Report •

#8
October 28, 2016 at 06:11:59
I think the break is working. I need to test it on different files, but I think that got me working. Thank you.

Reply ↓  Report •


Ask Question