編輯:代碼修改答覆意見
@echo off
setlocal EnableDelayedExpansion
for /F %%a in ('find /C /V "" ^< input.txt') do set "lines=%%a"
set /P "sets=There are %lines% lines, enter number of sets: "
set /A linesPerSet=lines/sets
del set*-?.csv 2> NUL
rem Initialize the indicators of all sets
for /L %%s in (1,1,%sets%) do set "set[%%s]=1"
rem Activate the first set
set "set=1"
set "set[1]=0"
set "line=0"
for /F "delims=" %%a in (input.txt) do (
rem Check all sets and distribute the lines accordingly
for /L %%s in (1,1,%sets%) do if !set[%%s]! equ 0 (
rem Output this line to first file of this set
>> set%%s-A.csv echo %%a
) else (
rem Output this line to second file of this set
>> set%%s-B.csv echo %%a
)
rem Check if the first file of the set must change
set /A line+=1, lineMODlinesPerSet=line%%linesPerSet
if !lineMODlinesPerSet! equ 0 (
rem Change the first file of set
set /A set+=1
for /L %%s in (1,1,!sets!) do set /A set[%%s]=set-%%s
)
)
輸出:
C:\> test.bat
There are 10 lines, enter number of sets: 5
C:\> type set*.csv
set1-A.csv
1,1
2,2
set1-B.csv
3,3
4,4
5,5
6,6
7,7
8,8
9,9
10,10
set2-A.csv
3,3
4,4
set2-B.csv
1,1
2,2
5,5
6,6
7,7
8,8
9,9
10,10
set3-A.csv
5,5
6,6
set3-B.csv
1,1
2,2
3,3
4,4
7,7
8,8
9,9
10,10
set4-A.csv
7,7
8,8
set4-B.csv
1,1
2,2
3,3
4,4
5,5
6,6
9,9
10,10
set5-A.csv
9,9
10,10
set5-B.csv
1,1
2,2
3,3
4,4
5,5
6,6
7,7
8,8
我仍然感到困惑,你如何提出對這些分裂。更明確。 – SomethingDark
請包含確切的輸入文件和所需的輸出。 – vonPryz
你想在這裏使用什麼語言....批處理或PowerShell?你有沒有嘗試過?很高興看到_something_,所以我們可以幫助你改進它。 – Matt