I need a batch application
- main objective : split .csv files into seperate files. with a row limit.
- the source folder contains many .CSV files.
- each main file has 2 header rows. and the split files should have the same 2 header rows.
- there must must a parameter in the code to set the rows limit in the split file.
- there must be a parameter in the code for the source folder (containing 1 or more .csv files)
- there must be a paramater in the code for target folder (to contain the split files)
- each split file with have the same name like the source file witn addition of the "number" out of all splitted files (example: source file ABC.CSV with 3 rows. row limit = 2. traget files: ABC_1_of_2.csv, ABC 2_of_2.csv)
This code worked for a single file
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set _FILE="C:\Users\USER\Desktop\Split\MAINFILE.CSV" & rem // (first command line argument is input file)
set /A "_LIMIT=100" & rem // (number of records or rows per output file)
rem // Split file name:
set NAME=c:\users\USER\Desktop\Split\Splitted\MAINFILE & rem // (path and file name)
set EXT=.csv & rem // (file name extension)
rem // Determine number of lines excluding header:
for /F %%I in ('^< "%_FILE%" find /V /C ""') do set /A "COUNT=%%I-1"
rem // Split file into multiple ones:
setlocal EnableDelayedExpansion
rem // Read file once:
< "!_FILE!" (
rem // Read header (first line):
set /P HEADER1=""
set /P HEADER2=""
rem // Calculate number of output files:
set /A "DIV=(COUNT-1)/_LIMIT+3"
rem // Iterate over output files:
for /L %%J in (1,1,!DIV!) do (
rem // Write an output file:
> "!NAME!_%%J_of_!DIV!!EXT!" (
rem // Write header:
echo/!HEADER1!
echo/!HEADER2!
rem // Write as many lines as specified:
for /L %%I in (1,1,%_LIMIT%) do (
set "LINE=" & set /P LINE=""
if defined LINE echo/!LINE!
)
)
)
)
endlocal
endlocal
exit /B
but when I tried to move for taking all the files in the folder I'm stuck code is not working
setlocal EnableExtensions DisableDelayedExpansion
Set /A "Location=C:\USER\"
For /R "C:\USER\Source\" %%f in (*.csv) do (
rem // Define constants here:
set _FILE=%%f & rem // (first command line argument is input file)
set /A "_LIMIT=20" & rem // (number of records or rows per output file)
rem // Split file name:
set "NAME=C:\USER\Splitted\%%~nf" & rem // (path and file name)
set EXT=.csv & rem // (file name extension)
timeout /t 3 /nobreak
rem // Determine number of lines excluding header:
for /F %%I in ('^< "%_FILE%" find /V /C ""') do set /A "COUNT=%%I-2"
rem // Split file into multiple ones:
setlocal EnableDelayedExpansion
rem // Read file once:
< "!_FILE!" (
rem // Read header (first line):
set /P HEADER1=""
set /P HEADER2=""
rem // Calculate number of output files:
set /A "_DIV=(COUNT-1)/_LIMIT+1"
Echo !_DIV!
rem // Iterate over output files:
for /L %%J in (1,1,!DIV!) do (
rem // Write an output file:
> "!NAME!_%%J_of_!_DIV!!EXT!" (
rem // Write header:
echo/!HEADER1!
echo/!HEADER2!
rem // Write as many lines as specified:
for /L %%I in (1,1,%_LIMIT%) do (
set "LINE=" & set /P LINE=""
if defined LINE echo/!LINE!
)
)
)
)
endlocal
)
endlocal
exit /B
Please help.
current status - only one empty file created - header rows only. File named - Book2_1_of_1.csv See runtime in the output attachment.
Thanks