Splitting text file

Help with writing and playing macros

Splitting text file

Postby OmegaNine » Mon Sep 11, 2006 10:09 am

I am looking for a program i can use to split up text files between patterns. Like

Code: Select all
-*-
Line 1
Line 2
-*-
Line 3
Line 4


I would want lines 1 and 2 in one file, and line 2 and 3 in another...Using "-*-" to tell it where to break up the files.

Is there A) an easy way to do this or B) a macro already writen for this?
User avatar
OmegaNine
Newbie
 
Posts: 3
Joined: Sun Sep 10, 2006 11:00 pm

Re: Splitting text file

Postby Mofi » Mon Sep 11, 2006 10:47 am

The following macro should do the job according to your example. The macro property Continue if a Find with Replace not found must be checked for this macro.

InsertMode
ColumnModeOff
HexOff
Bottom
IfColNum 1
Else
"
"
EndIf
Top
Clipboard 9
Loop
StartSelect
Find Select "-*-"
IfSel
Key HOME
EndSelect
IfSel
Copy
NewFile
Paste
NextWindow
EndIf
Key DOWN ARROW
Else
SelectToBottom
IfSel
Copy
NewFile
Paste
NextWindow
EndIf
EndSelect
Top
ExitLoop
EndIf
EndLoop
ClearClipboard
Clipboard 0

The macro would be much easier if the source file would look like:

Line 1
Line 2
-*-
Line 3
Line 4
-*-
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4042
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Splitting text file

Postby OmegaNine » Mon Sep 11, 2006 10:50 am

Looks like its wroking, then after getting about half way it tells me it cant creat temp file. anyway i could save those out as it comes along? (I just got this program, i know nothing)

And I can format it so that it works that way (With the -*- at the end of the text)
User avatar
OmegaNine
Newbie
 
Posts: 3
Joined: Sun Sep 10, 2006 11:00 pm

Re: Splitting text file

Postby Mofi » Mon Sep 11, 2006 11:17 am

Well, the macro would be only easier with -*- at end of the text if there is never something like that:

Line 1
Line 2
-*-
-*-
Line 3
Line 4

2 -*- in sequence and no -*- at the end of the file. The macro as is works for all 3 examples. So no need to change the format of the source file.

I have never seen a problem with temp file creation. How many temp files are created during macro execution? Maybe it would be better to also automatically save and close the new files with an automatically increasing number in the filename.
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4042
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna

Re: Splitting text file

Postby OmegaNine » Mon Sep 11, 2006 11:20 am

It made a little over 100 then pooped out. I think your right though, if there was a way to save and close the files as they where being created it would work flawlessly
User avatar
OmegaNine
Newbie
 
Posts: 3
Joined: Sun Sep 10, 2006 11:00 pm

Re: Splitting text file

Postby Mofi » Mon Sep 11, 2006 3:21 pm

Okay! For auto saving the new files with an increasing number you must FIRST create my universal CountUp macro. The source code with description can be found at counter.

THEN modify the existing macro with this new code. You have to adapt the red highlighted filename with path. If you think you will not produce more than 999 files, you can modify all 0000 to 000.

InsertMode
ColumnModeOff
HexOff
Bottom
IfColNum 1
Else
"
"
EndIf
Top
NewFile
"0000"
SelectAll
Clipboard 8
Copy
CloseFile NoSave
Clipboard 9
Loop
StartSelect
Find Select "-*-"
IfSel
Key HOME
EndSelect
IfSel
Copy
Key DOWN ARROW
NewFile
Paste
Top
"F:\Temp\Test_0000.tmp
"
Key UP ARROW
Find "0000"
Clipboard 8
PlayMacro 1 "CountUp"
Key HOME
StartSelect
Key END
Clipboard 9
Copy
EndSelect
DeleteLine
SaveAs "^c"
CloseFile
Else
Key DOWN ARROW
EndIf
Else
SelectToBottom
IfSel
Copy
NewFile
Paste
Top
"F:\Temp\Test_0000.tmp
"
Key UP ARROW
Find "0000"
Clipboard 8
PlayMacro 1 "CountUp"
Key HOME
StartSelect
Key END
Clipboard 9
Copy
EndSelect
DeleteLine
SaveAs "^c"
CloseFile
EndIf
EndSelect
Top
ExitLoop
EndIf
EndLoop
Clipboard 9
ClearClipboard
Clipboard 8
ClearClipboard
Clipboard 0
User avatar
Mofi
Grand Master
Grand Master
 
Posts: 4042
Joined: Thu Jul 29, 2004 11:00 pm
Location: Vienna


Return to Macros