Solved

Cutting up a base file into x parts

Posted on 2014-03-15
1
239 Views
Last Modified: 2014-03-16
Hi,

   I have a base file of approx. 300K records. I want to write a batch file that can cut up this file into x portions in sequential order. The x is provided as a input parameter to the batch. We can name the output files as batch<1-x>.txt

Sample base file contains the following contents:
"\\server1\file1"
''\\Server2\file2"
etc

How do I do this?
0
Comment
Question by:LuckyLucks
1 Comment
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 39931621
Try it with this:
@echo off
setlocal enabledelayedexpansion
REM Source file to break down:
set SourceFile=D:\Temp\base.txt
REM File names to create; "-<Index>.<Extension>" will be added automatically
set TargetBaseName=D:\Temp\batch
for %%a in ("%SourceFile%") do set TargetExt=%%~xa
if "%~1"=="" (
	set TotalFiles=
	set /p TotalFiles=Enter the number of files to create from '%SourceFile%': 
) else (
	set TotalFiles=%~1
)
if "%TotalFiles%"=="" goto :eof
for /f "delims=" %%a in ('find.exe /c /v "" "%SourceFile%"') do set Find=%%a
for %%a in (%Find%) do set TotalLines=%%a
echo Total lines to process: %TotalLines%
set /a BreakCount = TotalLines / TotalFiles
set /a Remainder = TotalLines %% TotalFiles
if %Remainder% neq 0 set /a BreakCount += 1
echo Number of lines in each file: %BreakCount%
echo Processing ...
set /a LineCount = 0
set /a FileCount = 1
echo [!FileCount!/!TotalFiles!] "%TargetBaseName%-!FileCount!%TargetExt%"
if exist "%TargetBaseName%-!FileCount!%TargetExt%" del "%TargetBaseName%-!FileCount!%TargetExt%"
for /f "usebackq delims=" %%a in ("%SourceFile%") do (
	set /a LineCount += 1
	>>"%TargetBaseName%-!FileCount!%TargetExt%" echo %%a
	if !LineCount! geq %BreakCount% (
		set /a LineCount = 0
		set /a FileCount += 1
		if exist "%TargetBaseName%-!FileCount!%TargetExt%" del "%TargetBaseName%-!FileCount!%TargetExt%"
		echo [!FileCount!/!TotalFiles!] "%TargetBaseName%-!FileCount!%TargetExt%"
	)
)
echo Done.

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question