Windows批处理脚本随机插入文本文件的顺序

时间:2017-04-12 09:26:10

标签: windows batch-file

所以我有一个我读过的文本文件,其中包含多行文字。目前它包含“成功”并失败。

这是我的代码。

setlocal enabledelayedexpansion

for /l %%x in (1, 1, 50) do (
for /f "tokens=*" %%a in (D:\Errors.txt) do echo %%x %%a >>D:\list.txt
)

基本上我要输出的是随机的Errors.txt中的单词,它是从1 - 50映射的。

目前这是我的代码输出,并不是随机的。此外,编号重复,我猜是由于循环。任何帮助将不胜感激。谢谢!

1 Successful 
1 Failed 
2 Successful 
2 Failed 
3 Successful 
3 Failed 
4 Successful 
4 Failed 
5 Successful 
5 Failed 
6 Successful 
6 Failed 
7 Successful 
7 Failed 

基本上最终目标应该是这样的。成功/失败随机应用,而数字1 - 50是一致的。

1 Successful 
2 Failed
3 Failed
4 Successful
5 Successful
6 Successful
7 Failed

2 个答案:

答案 0 :(得分:0)

@echo off
setlocal EnableDelayedExpansion
set n=0
(
  REM read from file
  for /f "tokens=*" %%a in (D:\Errors.txt) do (
    set /a n+=1
    echo !n! %%a 
  )
  REM for the rest until 50:
  for /l %%a in (!n!,1,49) do (
    set /a n+=1
    set /a x=!random! %% 2
    if !x!==0 (
      echo !n! successful
    ) else (
      echo !n! failed
    )
  )
)>D:\list.txt
type D:\list.txt

答案 1 :(得分:0)

此方法适用于Errors.txt文件中的任意行数:

@echo off
setlocal EnableDelayedExpansion

(

rem Load the lines from the file
set n=0
for /F "delims=" %%a in (D:\Errors.txt) do (
   set "line[!n!]=%%a"
   set /A n+=1
   echo !n! %%a
)

rem Repeat same lines the rest of times in random order
set /A nP1=n+1
for /L %%i in (!nP1!,1,50) do (
   set /A i=!random! %% n
   call echo %%i %%line[!i!]%%
)

) > D:\list.txt