Skip to content

Instantly share code, notes, and snippets.

@davidtolsma
Created November 25, 2010 16:16
Show Gist options
  • Save davidtolsma/715586 to your computer and use it in GitHub Desktop.
Save davidtolsma/715586 to your computer and use it in GitHub Desktop.
Split a text file into multiple files at a specific line size
@echo OFF
cls
TITLE SPLIT FILE
set originalFile=%1
set splitLine=%2
:: Where I'm running this script from
set thisScriptPath=%~dp0
:: set to splitLine to create the initial file or wipe clean if it exists
set currentLineNumber=%splitLine%
set newFileNumber=1
set outputFile=%originalFile%_%newFileNumber%.txt
if "%originalFile%"=="" GOTO :ENVIRONMENTNOTSET
if "%splitLine%"=="" GOTO :ENVIRONMENTNOTSET
set outputFileAndPath=%thisScriptPath%%outputDirectory%\%outputFile%
:: Red
set backgroundColor=4
:: white
set foregroundColor=7
echo WORKING ...
:: Set the background and foreground color to show that it is working
color %backgroundColor%%foregroundColor%
:: Look for .prt files recursively in all folders
FOR /F %%i in (%originalFile%) DO CALL :SPLIT_FILE %%i
cls
echo DONE... Splitting File: %originalFile%
:: Sets the background and foreground color back to standard
color 07
GOTO :QUIT
::~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
:SPLIT_FILE
set lineInFile=%*
if %currentLineNumber%==%splitLine% CALL :CREATE_NEW_FILE
set /A currentLineNumber+=1
:: Populate the new file
echo %lineInFile% >> %outputFile%
GOTO :EOF
:CREATEDIR
set argument=%*
:: If the folder does not exist this will create it
if not exist %thisScriptLocation%\%argument% md %argument%
GOTO :EOF
:CREATE_NEW_FILE
:: Reset line number
set currentLineNumber=0
set outputFile=%originalFile%_%newFileNumber%
:: Wipe existing file out if it exists
echo. > %outputFile%
:: Incriment the File number by 1
set /A newFileNumber+=1
cls
echo WORKING ... Creating: %outputFile%
GOTO :EOF
:ENVIRONMENTNOTSET
echo.
echo MISSING REQUIRED ARGUMENTS
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
echo USAGE: %~0 originalFile outputDirectory splitLine
echo EXAMPLE: %~0 items.txt 6000 output
echo.
echo.
echo YOU PROVIDED:
echo.
if "%originalFile%"=="" (ECHO inputFile= *MISSING*) ELSE (ECHO %originalFile%)
if "%splitLine%"=="" (ECHO outputFile= *MISSING*) ELSE (ECHO %splitLine%)
if "%outputDirectory%"=="" (ECHO outputFile= [OPTIONAL]) ELSE (ECHO %outputDirectory%)
echo.
:QUIT
:: Set the Toolbar back to CMD
title %comspec%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment