Solved

Cutting up a base file into x parts

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now