-2

There are two files: "list.txt" and new "newlist.txt".

Please tell me the script that can copy random lines from one file to another.

The number of lines to copy is also random (in the specified range):

set min=1
set max=100
set /a numberoflines=%random%%%(max-min+1)+min
3
  • 2
    Hi! Stack Overflow exists to help programmers solve problems they encounter. This means you have to do something yourself and if you have a specific problem, which you cannot solve by googling, then you should ask here. You cannot just ask people to do your job for you. Commented Dec 20, 2015 at 16:26
  • In the community of thousands of questions like mine. stackoverflow.com/questions/15042909/… stackoverflow.com/questions/14435375/… I still want to believe that there are good people and advise me any solution. Commented Dec 20, 2015 at 16:47
  • 1
    This site has changed a lot since 2013 and what was once considered an acceptable question may not treated as such today. If either of those questions had been asked today, they would both be downvoted. Commented Dec 20, 2015 at 17:27

2 Answers 2

0

This should work:

@echo off
setlocal EnableDelayedExpansion
type nul >newlist.txt
set min=1
set max=100
set /a numberoflines=%random%%%(max-min+1)+min
set /a cnt=0
for /f %%a in ('type "list.txt"^|find "" /v /c') do set /a cnt=%%a
FOR /L %%G IN (1,1,%numberoflines%) DO (
  set /a "linenumber=!random!%%%cnt%"
  set "read=1"
  set "line=-1"
  for /F "usebackq delims=" %%i in ("list.txt") do (
    set /a "line=!line!+1
    if !line! equ !linenumber! echo %%i >>newlist.txt
  )
)

Note that I put a type nul >newlist.txt at the start to clear newlist.txt before copying. If you only want to add the lines to the file, you should remove it.

Sign up to request clarification or add additional context in comments.

4 Comments

This might copy the same line multiple times, as linenumber may contain duplicate numbers theoughout the for /L loop iterations...
The question doesn't say anything about not copying lines multiple times. If you have less lines in a file than the max number of lines to copy than that'll happen anyway.
Oh, thank you good Sir! You helped me a lot! I am very grateful to you!
Duplicates necessarily can be removed in any additional line of a code, but I can't ask for more, you already help me a lot.
-1

Although you did not show any efforts on solving the task, I decided to provide a script as it sounded quite challenging to me; so here is the code that should do what you want:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "MIN=1"   & rem // minimum number of lines to copy
set "MAX=100" & rem // maximum number of lines to copy
set "NODUPS=" & rem /* set this to non-empty value to avoid duplicates */
for /F "delims=" %%C in ('^< "list.txt" find /C /V ""') do set /A "COUNT=%%C"
if defined NODUPS (
    if %COUNT% GTR %MAX% (
        set /A "NUMBER=%RANDOM%%%(MAX-MIN+1)+MIN"
    ) else if %COUNT% GEQ %MIN% (
        set /A "NUMBER=%RANDOM%%%(COUNT-MIN+1)+MIN"
    ) else set /A "NUMBER=COUNT"
) else (
    set /A "NUMBER=%RANDOM%%%(MAX-MIN+1)+MIN"
)
call :GENRAND RND_NUM %NUMBER% %COUNT% %NODUPS%
> "lines.tmp" (
    for /F "delims=" %%L in ('findstr /N /R "^" "list.txt"') do (
        setlocal DisableDelayedExpansion
        for /F "tokens=1 delims=:" %%N in ("%%L") do set /A "LIN_NUM=%%N"
        set "LINE=%%L"
        setlocal EnableDelayedExpansion
        set /A "INDEX=0"
        for %%I in (!RND_NUM!) do (
            set /A "INDEX+=1" & set "PADDED=0000!INDEX!"
            if %%I EQU !LIN_NUM! (
                echo(!PADDED:~-5!:!LINE:*:=!
            )
        )
        endlocal
        endlocal
    )
)
setlocal DisableDelayedExpansion
> "newlist.txt" (
    for /F "delims=" %%L in ('sort "lines.tmp"') do (
        set "LINE=%%L"
        setlocal EnableDelayedExpansion
        echo(!LINE:*:=!
        endlocal
    )
)
endlocal
endlocal
del /Q "lines.tmp"
exit /B

:GENRAND RND_NUM %NUMBER% %COUNT%
set "%1="
if not "%~4"=="" goto :NODUPS
for /L %%I in (1,1,%2) do (
    set /A "RND_ITEM=!RANDOM!%%%3+1"
    set "%1=!%1! !RND_ITEM!"
)
goto :HALT
:NODUPS
for /L %%I in (1,1,%3) do (
    set /A "RND_ITEM=!RANDOM!%%%3+1"
    set /A "RND[!RND_ITEM!_%%I]=%%I"
)
set /A "INDEX=0"
for /F "tokens=2 delims==" %%J in ('set RND[') do (
    set /A "INDEX+=1"
    set "%1=!%1! %%J"
    if !INDEX! GEQ %2 goto :HALT
)
:HALT
exit /B

The random lines may contain duplicates unless you set variable NODUPS to a non-empty value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.