-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
AndersonJ
  • 3
  • 1
  • 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. – zvone Dec 20 '15 at 16:26
  • In the community of thousands of questions like mine. http://stackoverflow.com/questions/15042909/extract-part-of-a-text-file-using-batch-dos http://stackoverflow.com/questions/14435375/batch-script-to-copy-lines-from-one-text-file-to-another-base-on-id I still want to believe that there are good people and advise me any solution. – AndersonJ Dec 20 '15 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. – SomethingDark Dec 20 '15 at 17:27

2 Answers2

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.

Dennis van Gils
  • 3,487
  • 2
  • 14
  • 35
  • This might copy the same line multiple times, as `linenumber` may contain duplicate numbers theoughout the `for /L` loop iterations... – aschipfl Dec 20 '15 at 19:21
  • 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. – Dennis van Gils Dec 20 '15 at 19:36
  • Oh, thank you good Sir! You helped me a lot! I am very grateful to you! – AndersonJ Dec 20 '15 at 19:43
  • 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. – AndersonJ Dec 20 '15 at 19:48
-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.

aschipfl
  • 33,626
  • 12
  • 54
  • 99