By Chris Diggs | May 21, 2018
Scenario:
I have a large file that I want to seperate into many different files based on a common string. I want to run this tool on my Windows machine. The output files should be sent to a different folder.
Windows Batch file:
On your Windows machine, create a folder. Copy the file that you want to seperate into many files into the folder. Create a new file with a .bat extension (Windows Bacth file).
split.bat
@echo off setlocal enableextensions enabledelayedexpansion set "file=%1" set "pattern=%2" set /a cnt=1 REM GET DATE/TIME TO CREATE NEW DIR for /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b) for /f "tokens=1-3 delims=/:" %%a in ('echo %time%') do (set mytime=%%a-%%b-%%c) set dir=%mydate%-%mytime% if not exist %dir% mkdir %dir% for /F "tokens=*" %%A in (%file%) do ( set "line=%%A" set "pattern=%pattern%" if "!line:%pattern%=!"=="!line!" ( rem echo .... pattern not found ) else ( set /a cnt+=1 rem echo .... pattern found ) echo %%A >> %dir%/%file%_!cnt! ) endlocal
Keep in mind, a new directory will be created in the folder. It will have the format
yyyy-mm-dd-HH-MM-ss.ss
of the date and time of executing split.bat.
Usage
How to use the Windows batch file:
split.bat <input_file> <common_string>
Example Input/Outout
Input file: test.txt
this,is,an,example testing;1,2,3 453245,54352, 2465,
Command ran:
split.bat test.txt 24
Three output files produced:
Output file 1: 2018-05-21-11-06-12.38/test.txt_1
this,is,an,example testing;1,2,3
Output file 2: 2018-05-21-11-06-12.38/test.txt_2
453245,54352,
Output file 3: 2018-05-21-11-06-12.38/test.txt_3
2465,