Solved Batch file to split ***HELP***

July 20, 2011 at 14:48:30
Specs: Windows XP
Hello!
I'm new with batch programming, i need a batch to split a file, my file is called camp856*.dat please review below example:

ship|1|9251230000|9251230000|08|00|||||||||Pflug Packaging
order|1|9251230000|9251230000|08|103706|0820786761|||||000000200000008711
load|1|9251230000|9251230000|08|1|103706|000000200000008711|
ship|1|9251230000|9251230000|08|00|||||||||Pflug Packaging
order|1|9251230000|9251230000|08|103706|0820768644|||||000000200000019373
load|1|9251230000|9251230000|08|1|103706|000000200000019373|
I need one file for every time a new ship record is found. Also the new files need to be named camp856 then maybe timestamp or something unique in a dat format (sorry if this is a pain it is for EDI use)
I appreciate any help with this!
Thanks :)


See More: Batch file to split ***HELP***

Report •


✔ Best Answer
July 25, 2011 at 20:04:00
Sorry, little bit of delay. OMG my boss wants me to do some actual work, the kind of thing she says I get paid for!....

Boss gone for rest of day - yippee!

Apologies, I didn't test the updated script as I should have done, boss was having a bad day and harassing us underlings. Please test the following:

@echo off
cls
setlocal enabledelayedexpansion

set nbr=0
for /f "tokens=*" %%a in ('dir /a-d /b camp856*.dat') do (
    for /f "tokens=1* delims=^|" %%1 in (%%a) do (
        set line=%%1^|%%2
        if /i [%%1] equ [ship] (
           set /a nbr+=1&&>camp856_!nbr!.dat echo !line!
               ) else (
                  echo !line! >>camp856_!nbr!.dat
    )
  )
)

Now at least I've got to look busy!! Don't hesitate to get back if you have more problems.


Please come back & tell us if your problem is resolved.



#1
July 20, 2011 at 18:33:10
Is the .dat file in text format i.e. can it be opened using Notepad or some other plain text editor?


Please come back & tell us if your problem is resolved.


Report •

#2
July 22, 2011 at 10:51:15
It is in a text format

Report •

#3
July 22, 2011 at 17:58:31
Here's just one solution - untested.

Good luck

@echo off
cls
setlocal enabledelayedexpansion

set nbr=0
for /f "tokens=*" %%1 in (camp856.dat) do (
    for /f "tokens=1* delims=^|" %%A in ("%%1") do (
        if /i [%%A] equ [ship] (
           set /a nbr+=1&&>camp856_!nbr!.dat echo %%1
               ) else (
                  echo %%1 >>camp856_!nbr!.dat
    )
  )
)


Please come back & tell us if your problem is resolved.


Report •

Related Solutions

#4
July 25, 2011 at 08:40:54
Thanks!

Report •

#5
July 25, 2011 at 12:35:15
Hello Wahine!

I actually found what was wrong it it works great!! only one thing when i tried to run multiple files and changed the

for /f "tokens=*" %%1 in (camp856*.dat) do (

to take all the files that start with camp856 and brake them down it is not doing it could you please help me out?


Report •

#6
July 25, 2011 at 14:44:34
Test this:

@echo off
cls
setlocal enabledelayedexpansion

set nbr=0
for /f "tokens=*" %%a in ('dir /a-d /b camp856*.dat') do (
    for /f "tokens=1* delims=^|" %%1 in (%%a) do (
        if /i [%%1] equ [ship] (
           set /a nbr+=1&&>camp856_!nbr!.dat echo %%1
               ) else (
                  echo %%1 >>camp856_!nbr!.dat
    )
  )
)


Please come back & tell us if your problem is resolved.


Report •

#7
July 25, 2011 at 16:59:58
Your great!!!! Thank you soo soo much :)

Report •

#8
July 25, 2011 at 17:17:41
Ok, i might have spoken too soon :( once i opened the file it shows like this:


ship
order
load
order
load
order

I need the entire segment:

ship|1|9251230000|9251230000|08|00|||||||||Pflug Packaging
order|1|9251230000|9251230000|08|103706|0820786761|||||000000200000008711
load|1|9251230000|9251230000|08|1|103706|000000200000008711|

I promise this is the last issue o.0


Report •

#9
July 25, 2011 at 20:04:00
✔ Best Answer
Sorry, little bit of delay. OMG my boss wants me to do some actual work, the kind of thing she says I get paid for!....

Boss gone for rest of day - yippee!

Apologies, I didn't test the updated script as I should have done, boss was having a bad day and harassing us underlings. Please test the following:

@echo off
cls
setlocal enabledelayedexpansion

set nbr=0
for /f "tokens=*" %%a in ('dir /a-d /b camp856*.dat') do (
    for /f "tokens=1* delims=^|" %%1 in (%%a) do (
        set line=%%1^|%%2
        if /i [%%1] equ [ship] (
           set /a nbr+=1&&>camp856_!nbr!.dat echo !line!
               ) else (
                  echo !line! >>camp856_!nbr!.dat
    )
  )
)

Now at least I've got to look busy!! Don't hesitate to get back if you have more problems.


Please come back & tell us if your problem is resolved.


Report •

#10
July 26, 2011 at 07:46:40
Ok now for sure this is closed! yey!!! i really appreciate all your work on this.

PS. If you have free time i'll make sure to keep posting questions :) i'm super new with batch and i'm supposed to get so much done it's not even funny...again thank you!!


Report •

#11
July 26, 2011 at 13:15:26

Whew, that was a marathon due to my oversights. Thanks for coming back to report your success(?), hope we meet up again.

Post away, there are many willing helpers on this forum most of them with vastly more knowledge than I have, you may be lucky and have a real guru answer any query you may post.

All the best.

W.


Please come back & tell us if your problem is resolved.


Report •


Ask Question