Page 1 of 1

load a txt file and break up a string

Posted: 15 May 2021 13:38
by rasil
Hi,

I have a txt file containing a lot of information about clients and their room numbers formatted like this [NAME:ROOM-NUMBER] so the name and the room number is broken up by a colon. is there a way to display this in 2 different variables? so for example:

Data.txt

Code: Select all

jonson:138
Massison:124
Elias:125
Ruby:118

What the batch file displays:

Code: Select all

Name: jonson
Room number: 138
And if this is achievable is there a way to mark what has already been displayed? so if jonson's information was showed in the txt file next to jonson it would put a simple - then the next time it would read it would skip jonson and go to the next one, so every time the txt file is read it would show a different person's information.

Thanks Rasil!

Re: load a txt file and break up a string

Posted: 16 May 2021 03:05
by T3RRY
It is a fairly trivial thing to use a For loop with delims option to split a string.

The below is a batch script that reads itself for the information required.

Code: Select all

@Echo off
 Set LF=^


:; above empty lines required for 'LF' linefeed definition.

:; Delayed expansion required to expand 'LF' variable
 Setlocal EnableDelayedExpansion

 For /f "Tokens=1,2 delims=#:" %%G in ('findstr /BC:":#" "%~f0"')Do Echo(Name: %%G!LF!Room Number: %%H
goto :eof

:#jonson:138
:#Massison:124
:#Elias:125
:#Ruby:118
To apply the same to your source file, modify the for loop like so:
( assuming only the desired information contains ':' )

Code: Select all

 For /f "Tokens=1,2 delims=:" %%G in ('findstr /C:":" "YOURSOURCEDOCSPATH.EXT"')Do Echo(Name: %%G!LF!Room Number: %%H
 
As for the second part of your question, I find your intent and meaning to be unclear.

What is the use case / method by which the data is intended to be processed? Can you provide more detail on what steps you want to take with the information?