Solved Split a .txt file into a batch file

March 3, 2020 at 01:54:45
Specs: Windows 10
Hi, I'd like to make sure that when you drag your .txt on the .bat file, it opens the cdm and asks you how many splits you want.
And then it will create them in the same directory as the .txt files named: "part1", "part2", ...

exemple:

Thank you very much!


See More: Split a .txt file into a batch file


#1
March 3, 2020 at 02:19:05
no time yet,

but you can modify this one for now:

:: ------- begin trial #2
::by nbrane comupting.net
set brk=50
set fnam=myfile.txt
set fbase=myfile
set fext=.txt
if not defined fbase (
echo usage: %0 file_name.ext
echo file will be split into sets of 50 lines named with the same base-name
echo no file given
goto :eof
)
:: edited to fix my goof: ""call :xx fixed to: "" call :xx in following line
for /f "tokens=1* delims=[]" %%a in ('find /v /n ""^<%fnam%') do if "%%b" neq "" call :xx %%a "%%b"
PAUSE
goto :eof
:xx
set /a n=(%1-1)/brk+1
echo output to:fbase%%n%%fext%: --- content: %~2
>>%fbase%%n%%fext% echo.%~2
::-------------- end batch

specs: https://www.userbenchmark.com/UserR...


Report •

#2
March 13, 2020 at 01:05:12
✔ Best Answer
You can give a try for this batch script

@echo off
Title A batch splitting text Script by Hackoo 2020
Color 0A
set "Limit_Line_Counter=50" REM You can modify just this line
set "InputFile=%1"
If "%~x1" NEQ ".txt" Goto :Help
set "lineCounter=1"
set "filenameCounter=1"
SetLocal EnableDelayedExpansion

@for %%a in (%InputFile%) do (
	set "name=%%~na"
	set "extension=%%~xa"
	@for /L %%i in (1,1,100) Do (
		If Exist "!name!%%i!extension!" Del "!name!%%i!extension!"
	)
)

@for /f "tokens=*" %%a in (%InputFile%) do (
	set "splitFile=!name!!filenameCounter!!extension!"
	set /a lineCounter+=1
	if !lineCounter! GTR !Limit_Line_Counter! (
		set /a filenameCounter+=1
		set lineCounter=1
		echo Created !splitFile!
	)
echo %%a>>!splitFile!
)
Timeout /T 3 /nobreak>nul & Exit
::------------------------------------------------------------------
:Help
Color 0C & Mode 80,3
echo(
echo       Usage : Drag and Drop a txt file over this script:"%~nx0"  
Timeout /T 5 /nobreak>nul & Exit
::------------------------------------------------------------------

message edited by Hackoo


Report •
Related Solutions


Ask Question