Page 1 of 1

How to divide a list evenly.

Posted: 05 Nov 2008 18:49
by mesowetodit
Hi folks,

Searched for "split", "divide", "separate" and couldn't find anything close to what i need. I'm new to the DOS environment and have only written some minor scripts at work. I've found a ton of small apps that can do what i want, but I work for a gov place, and they don't allow anything to be downloaded or installed if its not fed/state approved.

What i basically need is to split a list of hostnames evenly across 2 or 4 separate files.

So, for example, here's the main list(its in the 100's right now, and growing):

mainlist.txt:

test1server
test2server
test3server
test4server
test5server
test6server
test7server
test8server
test9server
test10server
test11server
test12server

== i like to split it up evenly across the board, best that we can, into 2 or 4 files...for example:

list1.txt:
test1server
test2server
test3server

list2.txt:
test4server
test5server
test6server

list3.txt:
test7server
test8server
test9server

list4.txt:
test10server
test11server
test12server

== I've looked at the "for /f" function...but didn't have the skills to figure it out, so glad i found this place.

Thanks in advance, hope yall can help.

Posted: 12 Nov 2008 11:44
by mesowetodit
Got my solution from another source...thought I share what i got help on here...does exactly what i needed. Hope this helps someone.

==========================================
Change these 3 set to suit your needs:

Set File=C:\AT\File
Set Mainfile=C:\AT\mainlist.txt
Set NoFiles=4

Will only clear out existing fileX.txt that is required for the script.

So if you go from 6 files to 4 files you'll need to manually delete file5.txt and file6.txt or change the first for command to always check from file1.txt to file10.txt: Change the 10 to whatever number you like just remember it takes a little while to go through all the numbers.

FOR /l %%a in (1,1,10) DO IF EXIST "%File%%%a.txt" DEL "%File%%%a.txt"

Code: Select all

SETLOCAL ENABLEDELAYEDEXPANSION
Set File=C:\AT\File
Set Mainfile=C:\AT\mainlist.txt
Set NoFiles=4
 
FOR /l %%a in (1,1,%NoFiles%) DO IF EXIST "%File%%%a.txt" DEL "%File%%%a.txt"
 
FOR /f %%a in ('Type "%Mainfile%" ^| FIND /v /c ""') DO Set NoInList=%%a
 
Set /a NoPerFile=%NoInList% / %NoFiles%
 
Set /a FilesWith1More=%NoInList% - (%NoPerFile% * %NoFiles)
 
Set CurrentFileNo=1
Set CurrentMainFileNo=1
If %FilesWith1More% NEQ 0 Set NextListWith1Extra=1
 
FOR /f "Delims=" %%a in ('Type "%Mainfile%"') DO (
    ECHO %%a>>"%File%!CurrentFileNo!.txt"
 
    Set /a CurrentMainFileNo+=1
    IF !CurrentMainFileNo! GTR %NoPerFile% (
        IF !CurrentFileNo! == !NextListWith1Extra! (
            Set /a NextListWith1Extra+=1
            If !NextListWith1Extra! GTR %FilesWith1More% Set NextListWith1Extra=0
        ) ELSE (
            SET /a CurrentFileNo+=1
            SET CurrentMainFileNo=1
        )
    )
)
 
@FOR /l %%a in (1,1,%NoFiles%) DO @ECHO Created "%File%%%a.txt"

Posted: 23 Nov 2008 00:50
by DosItHelp
mesowetodit,

A bit more compact:

Code: Select all

Setlocal Enabledelayedexpansion 
Set "File=C:\AT\File"
Set "Mainfile=C:\AT\mainlist.txt"
Set "NoFiles=4"

Del /Q "%File%*.txt">NUL

For /F %%A in ('"Type "%Mainfile%"|Tind "" /v /c"') do Set /A "NoInList=%%A"

Set /A "Index=0,NoPerFile=(NoInList+NoFiles-1)/NoFiles"

For /F "Usebackq Delims=" %%A in ("%Mainfile%") do (
    set /A "CurrentFileNo=1+Index/NoPerFile,Index+=1"
    echo.%%A>>"%File%!CurrentFileNo!.txt"
)

Hope this is useful.

:wink: