Jump to content

How to read certain line from a text file


n7Epsilon

Recommended Posts

Hi, I have a small batch scripting question...

I have a list of names in a text file. I want to somehow use the FOR command to read just 1 line and allow separating into tokens ... etc.

Example of Names.txt

John

Doe

Tester

I tried this (assuming names list is names.txt and that the names don't have any spaces in them)

FOR /F %%i IN (Names.txt) Do Echo %%i

This gave all the lines in separate Echo commands.

So I tried this:

FOR /F "tokens=1" %%i IN (Names.txt) Do Echo %%i

and it gave the same output !

I know of the delims option but how can I make Windows understand to use the CRLF as a delimiter so I can retrieve the lines by token number ? or is there some other way to do it ?

Edited by n7Epsilon
Link to comment
Share on other sites


Could you please try to explain better what you are trying to achieve.

Are you wishing to grab a specifically numbered line, a particular name, a particular first name /surname etc.

Try to give a full example, without trying to hide things. I'm sure your task isn't that unique that someone wishes to steal it and sell it on for a small fortune!

The tokens have no effect without specifying a delimiter, tokens=1 will use the default space delimiter unless specified otherwise. If no token is specified, then only the first token using the default space delimiter will be returned. This is the reason why both your examples are the same.

Link to comment
Share on other sites

@Yzowl: Thanks for the help, let me explain it better.

Here's a clear example to what I mean:

- Imagine a file called colours.txt, which contains the following

green

yellow

red

blue

And my batch file is as follows:

@Echo Off

setlocal enableextensions enabledelayedexpansion

set randomnumber=3

(for command here to set Colour to red by reading the 3rd line of colours.txt)

Echo The colour of this car is %Colour%.

endlocal

The problem is in what for command would look like.

I would also be nice if I could find a way to generate random numbers within a range I can set.

I have to use a program called random.exe to do that but a native way to do it without it would be great.

Edited by n7Epsilon
Link to comment
Share on other sites

If you can "write" the text file as you wish, the problem can be solved like this:

1:green

2:yellow

3:red

4:blue

FIND /V /N "thisisanabsurdstringthatwontbeinthefile" colours.txt

will output:

---------- COLOURS.TXT

[1]green

[2]yellow

[3]red

[4]blue

so you can do


Set choice=3
FOR /F "tokens=2 delims=]" %%A IN ('FIND /V /N "thisisanabsurdstringthatwontbeinthefile" colours.txt ^| FIND "%choice%"') DO ECHO %%A

You should get the idea....

jaclaz

P.S.: Real "random" number generation is very difficult, but a "pseudo-random" is relatively easy:

http://www.robvanderwoude.com/

http://www.robvanderwoude.com/files/random_nt.txt

http://www.robvanderwoude.com/files/random_nt2.txt

Edited by jaclaz
Link to comment
Share on other sites

Thanks it works perfectly :thumbup although a bit slow (due to the execution of 2 FIND commands for each query)

btw, I made up my own random generation algorithm (the following makes a number between 0 and 600):

@Echo off

:start

set /a RndNum=%Random:~-1%+1

set /a RndNum=%RndNum%%Random:~-2%

:decrease

If %RndNum% LSS 600 goto continue

set /a RndNum=%RndNum%-(%RndNum%/4)

If %Random:~-1% LEQ 5 set /a RndNum=%RndNum%-(%RndNum%/5)

If %Random:~-1% GEQ 7 set /a RndNum=%RndNum%-10%Random:~-1%

If %Random:~-1% LEQ 2 set RndNum=%RndNum:~-2%

goto decrease

:continue

Echo Number: %RndNum%

Link to comment
Share on other sites

Here is a batch file which takes two parameters.

The first parameter is a number.

This number will be the line number of the text you wish to retrieve.

The second parameter is a filename.

This will be the file you wish to retrieve the text from and should be quoted if it's name /path contains spaces

n-line.cmd

@ECHO OFF &SETLOCAL ENABLEEXTENSIONS
ECHO/%1|FINDSTR [0-9].* >NUL 2>&1||GOTO :EOF
IF NOT EXIST "%~2" GOTO :EOF
SET /A "LINENUM=%1-1"
MORE /E +%LINENUM% "%~2">%TEMP%\MYTEMP.TXT
SET /P N-LINE=<%TEMP%\MYTEMP.TXT
ECHO/%N-LINE% &&DEL %TEMP%\MYTEMP.TXT
ENDLOCAL &GOTO :EOF

Edited by Yzöwl
Link to comment
Share on other sites

Thank you Yzowl ! :thumbup , your method is so much faster moreso after removing the verification findstr command and it is also much more CPU friendly (when executed in a never ending loop)!

Edited by n7Epsilon
Link to comment
Share on other sites

Wow, I love the ingenuity (?) of the users here... writing pRNGs with batch files, certainly something I haven't seen before (I have seen a crude hex editor written in a batch file though :lol: ).

But the easy way is to just use %random%, the environment variable that returns a pseudo-random value each time it's accessed! :thumbup

Link to comment
Share on other sites

True, but then you can't set a range of numbers to make the random number in, hence all the loops...etc.

I do actually access the %random% variable several times, I also found a utility somewhere called random.exe which allows me to do exactly what I want but I'll just stick with this for now...

(and probably will re-write this in C++ when I learn enough since depending on a 23 MB framework for such a simple task is not justifiable (writing in C#))

Edited by n7Epsilon
Link to comment
Share on other sites

Just for fun then...

lottery.cmd

@ECHO OFF&MODE 17,9&COLOR E5&TITLE LOTTERY&SETLOCAL ENABLEEXTENSIONS
SET COUNT=0
TYPE NUL>T_.EMP
ECHO/TONIGHTS NUMBERS
:STARTIT
SET /A R=1+49*%RANDOM%/32768
FINDSTR/X "\<%R%\>"<T_.EMP>NUL 2>&1||(SET /A "COUNT+=1"&ECHO/%R%>>T_.EMP)
IF %COUNT% LSS 6 GOTO STARTIT
TYPE T_.EMP
ECHO/ANY KEY TO CLOSE
PAUSE>NUL
DEL T_.EMP
ENDLOCAL&GOTO :EOF

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...