Hey,good evening everyone,i need your help.
I have a text file with 20 lines. How do I randomly display a column in one of these lines?
and when i press any key,it display all the contents of that row.
(The text has 5 columns per line)
How do I randomly display a column in one of the lines?
Moderator: DosItHelp
Re: How do I randomly display a column in one of the lines?
What does "column" mean? How are columns separated in your text file? Or do they have a fixed length?
Steffen
Steffen
Re: How do I randomly display a column in one of the lines?
aGerman wrote:What does "column" mean? How are columns separated in your text file? Or do they have a fixed length?
Steffen
HI Steffen
The columns separated by the space bar.It looks like this :
Code: Select all
cold HX 16 aa bb
water HQ 16 aa bc
safe HGS\HES\HG6A\HE2 12-16 aa bc
white HC4A 14 aa bc
thin HG3 12 aa bc
double HS2 10-16 aa bc
change HC6 10-16 aa bc
raise HC5 10-14 aa bc
hot HB 10 aa bc
forever HD1 10 aa bc
walk HA 10-12 aa bc
machine QF3 10 aa bc
Re: How do I randomly display a column in one of the lines?
Okay. In this case that code might be working.
Steffen
Code: Select all
@echo off &setlocal
set "fname=test.txt"
set "columns=5"
for /f %%i in ('type "%fname%"^|find /c /v ""') do (
set /a "rndtokens=%random% %% (%%i * columns) + 1, skiplines=rndtokens / columns, cnttokens=rndtokens %% columns"
)
if %cnttokens% equ 0 set /a "skiplines-=1, cnttokens=columns"
<"%fname%" (
for /l %%i in (1 1 %skiplines%) do set /p "="
set "line=" &set /p "line="
)
for /f "tokens=%cnttokens%" %%i in ("%line%") do echo %%i
pause
echo %line%
pause
Steffen
Re: How do I randomly display a column in one of the lines?
aGerman wrote:Okay. In this case that code might be working.
Steffen
Perfect, Steffen! Thanks very much.
would like to tell me why my own batfile can not work ?
Code: Select all
@echo off
cd /d %~dp0
setlocal enabledelayedexpansion
set file=test.txt
for /f "delims=" %%a in ('type "%file%"') do set /a Line+=1
set /a Line=!random!%%!Line!
if not !Line! equ 0 set skip=skip=!Line!
set /a Line+=1
for /f "%skip% tokens=1-5" %%a in ('type "%file%"') do (
set /a col=!random!%%5+1
set C1=%%~a
set C2=%%~b
set C3=%%~c
set C4=%%~d
set C5=%%~e
call echo;!Line! line and !col! column: %%C!col!%%
pause
echo the line is:
echo %%~a %%~b %%~c %%~d %%~e
goto :End
)
:End
pause