2017-04-12 86 views
0

所以我有一個文本文件,我讀它包含多行文字。目前它包含「成功」並失敗。Windows批處理腳本隨機順序插入到文本文件

這是我的代碼。

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 
) 

基本上我想輸出是隨機是從1映射ERRORS.TXT的話 - 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 
+0

你提的問題不夠清晰,以提供一個有用的解決方案,請嘗試改寫/改善它通過使用的編輯工具。 – Compo

+0

感謝編輯! –

+0

我沒有改善你的問題。您在發佈的代碼中使用的文件「Errors.txt」似乎只包含兩行,第1行:「成功」,第2行:「失敗」。 1 - 50是如何相關的?你想成爲你的最終目標是什麼? – Compo

回答

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 
+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 
+0

謝謝。這正是我所需要的。 –

+0

此方法不會使用其餘行中的「錯誤」文件中的行;只是在前兩行... – Aacini

+0

噢,我現在才注意到它。以下三項不是來自.txt。它來自循環。謝謝您的幫助! –