Random replacement of files from the list ( batch-file)

Solution 1:

@echo off

setlocal & set "_dir=%Temp%\Test\Target\."

for /f %%f in ('type example.txt^|find/v /c ""')do set /a "_max=%%~f-1"
set /a "_line=%random% * (-%_max% - %_max% + 1) / 32768 + %_max%"

if %_line% neq 0 set "_skip=skip^=%_line:-=%"

for /f %_skip%^usebackq^ ^tokens^=*^ ^delims^= %%i in (
    "example.txt")do move/y "%%~i" "%_dir%" & endlocal & exit /b

It is the case to get the path of your file to be copied, using a random draw in range 1 and total number of lines in your text file.

1. Use the range -/+total number of lines -1: 100 lines == range [-100-1] - [+100-1]

2. Use this value to skip=? the previous lines, setting the skip= resulting number, and use it in For /f loop.

3. If 0 is the sorted one, the variable to skip is not set and does not interfere, making the first item to be obtained, no line is skipped.

4. Regardless of whether the lines are (0 or other value up to a maximum of -1), use the line/file %%~i immediately exits the loop.


An option to remove the row a obtained in random...

@echo off

setlocal & set "_dir=%Temp%\Test\Target\."
for /f %%f in ('type ".\example.txt" ^| find/v /c ""
')do if %%~f geq 1 (set /a "_max=%%~f-1")else goto :eof

set /a "_line=%random% * (-%_max% - %_max% + 1) / 32768 + %_max%"
 
if %_line% neq 0 set "_skip=skip^=%_line:-=%"
for /f %_skip%^usebackq^ ^tokens^=*^ ^delims^= %%i in (
     "example.txt")do set "_file=%%~i" && goto %:^)
    
%:^)
findstr /v /c:"%_file%" <".\example.txt" >"%temp%\example.txt"
move /y "%_file%" "%_dir%" && move/y "%temp%\example.txt" ".\example.txt"
endlocal & goto :eof