Announcement

How to split large file into small files on Windows

Break large file into small files - techiners


 INTRODUCTION


Often during development, we require working with flat files which are really large files and at times its really difficult to open these files, and if opened, mostly crashes the application software opening it. Even to process such a large files is not feasible by the system that we develop. The solution is to break such a large file into number of small files and process those small files.

How to break a large file into small files without opening it with fixed number of records in each file?


CODE


Below is the code for that works in Windows. This is just a simple batch file which you can save and all you need to change is just a LargeFileName and SmallFileName. SmallFileName will get auto incremented and files will be created at the location where the batch file is saved.

@echo off

setlocal ENABLEDELAYEDEXPANSION

REM Edit this value to change the name of the file that needs splitting. Include the extension.

SET LFN=LargeFileName.csv

REM Edit this value to change the number of lines per file.

SET LPF=200

REM Edit this value to change the name of each short file. It will be followed by a number indicating where it is in the list.

SET SFN=SmallFileName

REM Do not change beyond this line.

SET SFX=%BFN:~-3%

SET /A LineNum=0

SET /A FileNum=1

For /F "delims==" %%l in (%BFN%) Do (

SET /A LineNum+=1

echo %%l >> %SFN%!FileNum!.%SFX%

if !LineNum! EQU !LPF! (
SET /A LineNum=0
SET /A FileNum+=1
)

)

endlocal

Pause


CONCLUSION

This is just a simple post which came across a development task recently. So, thought of sharing with you techies. Please do share with others with a smile on your face because sharing is caring and spread the smile of coding among all techies.

Happy Coding!

No comments: