-
Notifications
You must be signed in to change notification settings - Fork 0
/
split2.bat
112 lines (105 loc) · 2.72 KB
/
split2.bat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
@echo off
setLocal EnableDelayedExpansion
set limit=%1%
set /a lineCounter=0
set /a filenameCounter=1
set /a counterHdr=1
set /a forCounter=1
set /a fileCounter=1
set flagHdr1=1
set flagHdr2=1
set name=
set extension=
for %%a in (%2%) do (
set "name=%%~na"
set "extension=%%~xa"
)
rem gets headers and lastLine
for /f "tokens=*" %%a in (%2%) do (
if !counterHdr!==1 (
set hdr1=%%a
)
if !counterHdr!==2 (
set hdr2=%%a
)
rem a counter just to get the first two records (header1 and header2)
set /a counterHdr+=1
set lastLine=%%a
)
rem Total Records Found
rem 4 = two headers + lastline + 1
set /a totalRecords=!counterHdr!-4
rem cycles through the contents of the file
for /f "tokens=*" %%a in (%2%) do (
rem sets the name for the splitted file
set splitFile=!name!-part!filenameCounter!!extension!
rem if there is only 1 record
rem it dumps every thing into the new file
if !totalRecords!==1 (
echo %%a>>!splitFile!
)
rem if there is more than 1 record
if !totalRecords! gtr 1 (
rem builds the first split file
if !fileCounter!==1 (
rem skips first two lines because that's where the headers are
if !forCounter! gtr 2 (
if !lineCounter! lss !limit! (
if !flagHdr1!==1 if !flagHdr2!==1 (
rem adds headers for the first splitted file
echo !hdr1! >> !splitFile!
echo !hdr2! >> !splitFile!
set flagHdr1=0
set flagHdr2=0
)
if !flagHdr1!==0 if !flagHdr2!==0 (
rem adds records to the first splitted file
echo %%a>>!splitFile!
set /a lineCounter+=1
)
) else (
rem adds lastLine to the first splitted file
echo !lastLine!>>!splitFile!
rem echo Created !splitFile!.
set /a fileCounter+=1
set /a filenameCounter+=1
set lineCounter=0
rem saves current cycle record to a new variable
rem the next file will receive the value of firstRecordNextFile
set firstRecordNextFile=%%a
set flagHdr1=1
set flagHdr2=1
)
)
rem will create more split files
) else (
rem sets a newLimit
rem this way it will not add one more record in the new file
set /a newlimit=!limit!-1
if !lineCounter! lss !newlimit! (
if !flagHdr1!==1 if !flagHdr2!==1 (
echo !hdr1! >> !splitFile!
echo !hdr2! >> !splitFile!
echo !firstRecordNextFile! >> !splitFile!
set flagHdr1=0
set flagHdr2=0
)
if !flagHdr1!==0 if !flagHdr2!==0 (
echo %%a>>!splitFile!
set /a lineCounter+=1
)
) else (
echo !lastLine!>>!splitFile!
rem echo Created !splitFile!.
set /a fileCounter+=1
set /a filenameCounter+=1
set lineCounter=0
set firstRecordNextFile=%%a
set flagHdr1=1
set flagHdr2=1
)
)
)
rem increments forCounter in each cycle
set /a forCounter+=1
)