load a txt file and break up a string

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Posts: 31
Joined: 23 Apr 2020 13:05

load a txt file and break up a string

#1 Post by rasil » 15 May 2021 13:38


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:


Code: Select all


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!

Posts: 179
Joined: 06 May 2020 10:14

Re: load a txt file and break up a string

#2 Post by T3RRY » 16 May 2021 03:05

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

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?

Post Reply