Split file into multiple files

March 27, 2012 at 18:08:19
Specs: Win 7 Pro, 4.17Ghz 8GB ram

Currently Im using the script below to do this will take the id's and echo them into separate files but the last file has the last id + any thing below it.

@echo off
cls
set id=12345678
setlocal enabledelayedexpansion

for /f "tokens=*" %%1 in (input.txt) do (
    set inline=%%1
    if /i "!inline:~0,12!" == "ID= %id%" (
	set /a ctr+=1
       set outfile=%id%_!ctr!.txt
       )
       echo !inline!>>!outfile!
)

input file:

id=12345678-1
x=234
y=674
z=842
avg=746
c=924
id=12345678-2
x=235
y=673
z=848
avg=741
c=927
id=12345679
x=2355
y=6738
z=8489
avg=7415
c=9274

12345678_1.txt:
id=12345678-1
x=234
y=674
z=842
avg=746
c=924
12345678_2.txt
id=12345678-2
x=235
y=673
z=848
avg=741
c=927
id=12345679
x=2355
y=6738
z=8489
avg=7415
c=9274

Its not supposed to have the last id of 12345679 in that file

any suggestions?


See More: Split file into multiple files

Report •

#1
March 27, 2012 at 19:59:55
This works assuming your sections are always 6 lines long:

@echo off
setlocal enabledelayedexpansion
set #=0
set _=0
for /f "tokens=*" %%i in (
'type %1'
) do (
set /a _+=1
if !_! leq 5 (
>>newfile!#!.txt echo %%i
) else (
>>newfile!#!.txt echo %%i
set /a #+=1
set _=0
)
)

Use it like this:

script.bat inputfile.txt

Tony


Report •

#2
March 28, 2012 at 10:09:22
You keep asking variations of the same question. Tell us what you're trying to do, where's the start, and what's the end. Maybe we can consolidate a few steps.

How To Ask Questions The Smart Way


Report •

#3
March 28, 2012 at 10:21:54
Ok sorry. We have a machine that tests each product we make for accuracy. It puts all of its data into one big file.

Im trying to make a quick script that any of my employees can use where they type in the exact instance of the test they want and it will spit it out.

so the input file that the machine spits out looks like this:

input file:
id=12345678-1
x=234
y=674
z=842
avg=746
c=924
id=12345678-2
x=235
y=673
z=848
avg=741
c=927
id=12345679
x=2355
y=6738
z=8489
avg=7415
c=9274

and i want it to spit out everything with this id id=12345678 (what job it is) so it needs to have id=12345678-1 and id=12345678-2 but in separate files.

file1:

id=12345678-1
x=234
y=674
z=842
avg=746
c=924

file2:

id=12345678-2
x=235
y=673
z=848
avg=741
c=927

This makes it easier to see and make sure it is within our tolerances.
The script that I have at the top does this but the last file has the last id + every id below it. Note this file has thousands of these. I just showed you a little bit of it.


Report •

Related Solutions

#4
March 28, 2012 at 11:45:04
And given one of your previous posts, do you want to strip out the results from the original?

Honestly, it sounds easier to just write a script / simple program to check for anything outside of some predefined tolerance, but I'm willing to play along.

How To Ask Questions The Smart Way


Report •

#5
March 28, 2012 at 14:09:52
Try this:

@echo off & setlocal enabledelayedexpansion
if defined # set #=
set id=12345678
for /f "tokens=*" %%i in (
'type %1'
) do (
set _=%%i
if "!_:~0,2!"=="id" (
if "!_:~3,8!" neq "%id%" goto :eof
)
if /i "!_:~0,11!" == "id=%id%" (
set /a #+=1
set outfile=%id%_!#!.txt
)
>>!outfile! echo !_!
)


Report •

#6
March 28, 2012 at 18:01:47
Ill try it. It may be an issue with my text file ill try cutting some of the stuff out of it...

Report •

#7
March 28, 2012 at 18:30:21
Ok it does work thank you! Sorry my file had some "id" and some "ID" so it would have all the "ID" below it because it case sensitive.
so i just added another line to catch the capital ones.

Thank you very much!


Report •

#8
March 28, 2012 at 20:37:13
If you add the /i switch to the IF statements it does case insensitive comparisons.
Tony

Report •

#9
March 28, 2012 at 21:46:13
Oh yeah. I forgot about that. That would be easier to remove.

Report •

#10
March 31, 2012 at 19:39:23
so I have been testing this a little bit more and it only works if the strings that i want are right on top.

if you try to get id 12345679 it does not work.

any suggestions. I think it would be just one quick edit but I'm not quite sure how to do it.


Report •

#11
April 2, 2012 at 05:01:06
Try this:

@echo off & setlocal enabledelayedexpansion
if defined # set #=
set id=12345679
for /f "tokens=*" %%i in (
'type %1'
) do (
set _=%%i
if "!_:~0,2!"=="id" (
if "!_:~3,8!" neq "%id%" type nul
)
if /i "!_:~0,11!" == "id=%id%" (
set /a #+=1
set outfile=%id%_!#!.txt
)
>>!outfile! echo !_!
)


Report •

Ask Question