Help with reading file

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
btpoole
Posts: 15
Joined: 04 Sep 2015 06:28

Help with reading file

#1 Post by btpoole » 17 Sep 2015 09:36

I have simple text file with information like the following:

Jim:1
Tom:2
Sam:3

I have a script that will read each line a cut the string at the : storing each part in a variable with the following:

Code: Select all

for /f "tokens=1,2 delims=:" %%a in (sample.txt) DO (
findstr ":" sample.txt |cut -f1 -d ":" > name.txt
findstr ":" sample.txt |cut -f2 -d ":" >id.txt

set /p xname=<name.txt
set /p xid=<id.txt

echo %xname%
echo %xid%
)


The name.txt and id.txt gets create and populated right but I can't seem to the the xname and xid to populate. Am I missing something simple?
Thanks

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

Re: Help with reading file

#2 Post by Squashman » 17 Sep 2015 09:53

Not sure why you are doing what you are doing and why you continue to use the CUT command in all of your batch files. You are not even using the FOR variables at all which is what you should be doing.

Put this in a batch file and see what it does.

Code: Select all

for /f "tokens=1,2 delims=:" %%a in (sample.txt) DO (
     echo Name=%%a
     echo ID=%%b
)

btpoole
Posts: 15
Joined: 04 Sep 2015 06:28

Re: Help with reading file

#3 Post by btpoole » 17 Sep 2015 10:16

Thanks Squashman. I was using cut because I ran across it a while back and didn't know any better, but I see from your example an easy way of doing it.
Thanks again

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

Re: Help with reading file

#4 Post by Squashman » 17 Sep 2015 10:28

btpoole wrote:Thanks Squashman. I was using cut because I ran across it a while back and didn't know any better, but I see from your example an easy way of doing it.
Thanks again

I showed you in your previous thread that CUT was not needed.
You also have the FOR command perfectly programmed. You just didn't bother to use the variables the FOR command was creating for you. So I was confused as to why you were still trying to use the CUT command.

Post Reply