Batch file to split text file into multiple files in same directory on Win Svr 2012 using NO 3rd Party Apps

bearwires

Prominent
Jun 6, 2017
1
0
510
I am hoping the wonderful experts here can assist me with my problem. I have found this site extremely useful in the past in solving other issues I have had but I have searched this site (and many others) and tried all the answers similar to my problem but cant seem to get any of them to work for this particular problem. I am not a coder but have dabbled with other code and got them to work for other issues.

I need to split a text file containing keywords(1 kw per line, no blank lines) into multiple text files within the same directory, each with 300 lines (except last text file if total input lines not exactly divisible by 300). The input file will NEVER be larger than 100MB and is typically always less than 30MB.

Ideally, I then need the input file deleted once it has been split and all split text files moved to another directory (there are no other text files to worry about in the original directory) I need it to be a bat file or vbs script called via bat file.

input file:

keyword-file.txt

output files:

keyword-file_1.txt (300 lines)
keyword-file_2.txt (300 lines)
keyword-file_3.txt (300 lines)
etc


To clarify requirements the above:

1) Split input text file (<100MB) into smaller text files, each with 300 lines
2) Delete input text file
3) Move all split text files to another specified directory
 
Solution
Hi, I copied this from [another page] and made a few minor modifications and got it mostly working. It definitely needs more work (would be nice if parameter 1 was the filename). I can't help you to fix it up as I just don't know that much about batch variables and parameters, etc. I don't understand most of it to be honest.

I did test it and got it working on a 500 line file, you should make a backup of your text files and test it before considering it safe. Also, it is slow!

It does not delete the original source file, nor does it move any files to any other folders. That would be up to you to figure out.

@ECHO OFF
SETLOCAL
SET /a fcount=199
SET /a llimit=300
SET /a lcount=%llimit%
FOR /f "usebackqdelims=" %%a IN...

gardenman

Splendid
Moderator
Hi, I copied this from [another page] and made a few minor modifications and got it mostly working. It definitely needs more work (would be nice if parameter 1 was the filename). I can't help you to fix it up as I just don't know that much about batch variables and parameters, etc. I don't understand most of it to be honest.

I did test it and got it working on a 500 line file, you should make a backup of your text files and test it before considering it safe. Also, it is slow!

It does not delete the original source file, nor does it move any files to any other folders. That would be up to you to figure out.

@ECHO OFF
SETLOCAL
SET /a fcount=199
SET /a llimit=300
SET /a lcount=%llimit%
FOR /f "usebackqdelims=" %%a IN ("keyword-file.txt") DO (
CALL :select
>>"%sourcedir%\file$$.txt" ECHO(%%a
)
SET /a lcount=%llimit%
:select
SET /a lcount+=1
IF %lcount% lss %llimit% GOTO :EOF
SET /a lcount=0
SET /a fcount+=1
MOVE /y "%sourcedir%\file$$.txt" "keyword-file_%fcount:~-2%.txt" >NUL 2>nul
GOTO :EOF
 
Solution

TRENDING THREADS