n7Epsilon Posted October 1, 2006 Posted October 1, 2006 (edited) 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.txtJohnDoeTesterI 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 %%iThis gave all the lines in separate Echo commands.So I tried this:FOR /F "tokens=1" %%i IN (Names.txt) Do Echo %%iand 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 October 1, 2006 by n7Epsilon
Yzöwl Posted October 1, 2006 Posted October 1, 2006 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.
n7Epsilon Posted October 1, 2006 Author Posted October 1, 2006 (edited) @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 followinggreenyellowredblueAnd my batch file is as follows:@Echo Offsetlocal enableextensions enabledelayedexpansionset 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%.endlocalThe 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 October 1, 2006 by n7Epsilon
jaclaz Posted October 1, 2006 Posted October 1, 2006 (edited) If you can "write" the text file as you wish, the problem can be solved like this:1:green2:yellow3:red4:blueFIND /V /N "thisisanabsurdstringthatwontbeinthefile" colours.txtwill output:---------- COLOURS.TXT[1]green[2]yellow[3]red[4]blueso you can doSet choice=3FOR /F "tokens=2 delims=]" %%A IN ('FIND /V /N "thisisanabsurdstringthatwontbeinthefile" colours.txt ^| FIND "%choice%"') DO ECHO %%AYou should get the idea....jaclazP.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.txthttp://www.robvanderwoude.com/files/random_nt2.txt Edited October 1, 2006 by jaclaz
n7Epsilon Posted October 1, 2006 Author Posted October 1, 2006 Thanks it works perfectly 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:startset /a RndNum=%Random:~-1%+1set /a RndNum=%RndNum%%Random:~-2%:decreaseIf %RndNum% LSS 600 goto continueset /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:continueEcho Number: %RndNum%
Yzöwl Posted October 1, 2006 Posted October 1, 2006 (edited) 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 spacesn-line.cmd@ECHO OFF &SETLOCAL ENABLEEXTENSIONSECHO/%1|FINDSTR [0-9].* >NUL 2>&1||GOTO :EOFIF NOT EXIST "%~2" GOTO :EOF SET /A "LINENUM=%1-1"MORE /E +%LINENUM% "%~2">%TEMP%\MYTEMP.TXTSET /P N-LINE=<%TEMP%\MYTEMP.TXTECHO/%N-LINE% &&DEL %TEMP%\MYTEMP.TXTENDLOCAL &GOTO :EOF Edited October 1, 2006 by Yzöwl
n7Epsilon Posted October 1, 2006 Author Posted October 1, 2006 (edited) Thank you Yzowl ! , 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 October 1, 2006 by n7Epsilon
LLXX Posted October 2, 2006 Posted October 2, 2006 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 ).But the easy way is to just use %random%, the environment variable that returns a pseudo-random value each time it's accessed!
n7Epsilon Posted October 2, 2006 Author Posted October 2, 2006 (edited) 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 October 2, 2006 by n7Epsilon
Yzöwl Posted October 2, 2006 Posted October 2, 2006 Just for fun then...lottery.cmd@ECHO OFF&MODE 17,9&COLOR E5&TITLE LOTTERY&SETLOCAL ENABLEEXTENSIONSSET COUNT=0TYPE NUL>T_.EMPECHO/TONIGHTS NUMBERS:STARTITSET /A R=1+49*%RANDOM%/32768FINDSTR/X "\<%R%\>"<T_.EMP>NUL 2>&1||(SET /A "COUNT+=1"&ECHO/%R%>>T_.EMP)IF %COUNT% LSS 6 GOTO STARTITTYPE T_.EMPECHO/ANY KEY TO CLOSEPAUSE>NULDEL T_.EMPENDLOCAL&GOTO :EOF
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now