Solved

Cutting up a base file into x parts

Posted on 2014-03-15
1
242 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 85

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

724 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