[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Cutting up a base file into x parts

Posted on 2014-03-15
1
Medium Priority
?
244 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 2000 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

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

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

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…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
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.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

649 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