Solved

Cutting up a base file into x parts

Posted on 2014-03-15
1
241 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 84

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

734 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