 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
theungod
Joined: 05 Aug 2009 Posts: 25
|
Posted: Tue Feb 02, 2010 2:49 pm Post subject: keeping only parts of a document...? |
|
|
I need to figure out how to turn a document that looks like this:
| Quote: | Audio J Information
Date Sent: 01/22/10
Date Printed: 01/21/10
INVOICE CUSTOMER NAME # OF START # OF
# SPOTS DATE COPIES
KABC
855645 BIKE SHOP 1 02/12/10 1
856434 ELECTRIC WORK TESTING 3 02/08/10 1
Total Invoices for KABC 2 Total Spots 4 MP3 SPOTS EMAILED TO: pop thatguy@pop.com & pop3@pop.com
KAY
897940 ROOFING INCORPORATED 1 02/12/10 1
898816 JAMES GALVANIZING 3 02/08/10 1
INCORPORATED
899258 SANFORD BARBERS 2 02/11/10 1
899819 RHINESTONE RESTAURANT 3 02/08/10 1
Total Invoices for KAY 4 Total Spots 9 MP3 SPOTS EMAILED TO: frank@sho3.com
KABEA
899194 PERIWINKLE P U D 8 02/08/10 1
Total Invoices for KABEA 1 Total Spots 8 MP3 SPOTS EMAILED TO: pop |
And have a script turn it into this and save it to the clipboard, so that I can use it to do Make Directory and create folders on my hard drive named accordingly:
| Quote: | | KABC-0208 KAY-0208 KABEA-0208 |
Where the 0208 would be a variable I supply.
Anyone have any ideas? |
|
| Back to top |
|
 |
SoLong&Thx4AllTheFish
Joined: 27 May 2007 Posts: 4999
|
Posted: Tue Feb 02, 2010 3:10 pm Post subject: |
|
|
Is it a fixed format, e.g you always need the 7th 20th etc line of each file? Of any line that is composed of a single word? Tell us the format/pattern and we can point you in the right direction which may be a simple parsing loop or some regexp magic. _________________ AHK Wiki FAQ
TF : Text files & strings lib, TF Forum |
|
| Back to top |
|
 |
theungod
Joined: 05 Aug 2009 Posts: 25
|
Posted: Tue Feb 02, 2010 3:25 pm Post subject: |
|
|
| hugov wrote: | | Is it a fixed format, e.g you always need the 7th 20th etc line of each file? Of any line that is composed of a single word? Tell us the format/pattern and we can point you in the right direction which may be a simple parsing loop or some regexp magic. |
The only constant is this: the lines I want to keep are comprised of one word that is between 3 and 5 characters in length. Does that help? |
|
| Back to top |
|
 |
sinkfaze
Joined: 18 Mar 2008 Posts: 5043 Location: the tunnel(?=light)
|
Posted: Tue Feb 02, 2010 4:02 pm Post subject: |
|
|
| theungod wrote: | | The only constant is this: the lines I want to keep are comprised of one word that is between 3 and 5 characters in length. Does that help? |
Are there instances where the line may be smaller than three characters? If not we can probably narrow the criteria even further:
| Code: | var=
(
Audio J Information
Date Sent: 01/22/10
Date Printed: 01/21/10
INVOICE CUSTOMER NAME # OF START # OF
# SPOTS DATE COPIES
KABC
855645 BIKE SHOP 1 02/12/10 1
856434 ELECTRIC WORK TESTING 3 02/08/10 1
Total Invoices for KABC 2 Total Spots 4 MP3 SPOTS EMAILED TO: pop thatguy@pop.com & pop3@pop.com
KAY
897940 ROOFING INCORPORATED 1 02/12/10 1
898816 JAMES GALVANIZING 3 02/08/10 1
INCORPORATED
899258 SANFORD BARBERS 2 02/11/10 1
899819 RHINESTONE RESTAURANT 3 02/08/10 1
Total Invoices for KAY 4 Total Spots 9 MP3 SPOTS EMAILED TO: frank@sho3.com
KABEA
899194 PERIWINKLE P U D 8 02/08/10 1
Total Invoices for KABEA 1 Total Spots 8 MP3 SPOTS EMAILED TO: pop
)
i=1
InputBox, cOut, Custom Output, Please enter a custom amount., , 200, 125
if !cOut {
MsgBox, 48, Warning, No value was entered, please try again.
return
}
Loop, Parse, var, `n, `r
{
if RegExMatch(A_LoopField,"^[A-Z]+$",match) {
if (StrLen(A_LoopField) <= 5)
m%i%:=match, i++
}
}
i=1
MsgBox % m1 "-" cOut A_Space
. m2 "-" cOut A_Space
. m3 "-" cOut
return |
EDIT: If the key string is always going to be located on a line after "Total Invoices for" a while loop will work as well:
| Code: | var=
(
Audio J Information
Date Sent: 01/22/10
Date Printed: 01/21/10
INVOICE CUSTOMER NAME # OF START # OF
# SPOTS DATE COPIES
KABC
855645 BIKE SHOP 1 02/12/10 1
856434 ELECTRIC WORK TESTING 3 02/08/10 1
Total Invoices for KABC 2 Total Spots 4 MP3 SPOTS EMAILED TO: pop thatguy@pop.com & pop3@pop.com
KAY
897940 ROOFING INCORPORATED 1 02/12/10 1
898816 JAMES GALVANIZING 3 02/08/10 1
INCORPORATED
899258 SANFORD BARBERS 2 02/11/10 1
899819 RHINESTONE RESTAURANT 3 02/08/10 1
Total Invoices for KAY 4 Total Spots 9 MP3 SPOTS EMAILED TO: frank@sho3.com
KABEA
899194 PERIWINKLE P U D 8 02/08/10 1
Total Invoices for KABEA 1 Total Spots 8 MP3 SPOTS EMAILED TO: pop
)
Pos=1
InputBox, cOut, Custom Output, Please enter a custom amount., , 200, 125
if !cOut {
MsgBox, 48, Warning, No value was entered, please try again.
return
}
While Pos:=RegExMatch(var,"Total Invoices for ([A-Z]+)",m,Pos+StrLen(m))
res.=((A_Index=1) ? "" : A_Space) m1 "-" cOut
MsgBox % res
VarSetCapacity(res,0)
return |
_________________ Try Quick Search for Autohotkey or see the tutorial for newbies. |
|
| Back to top |
|
 |
SoLong&Thx4AllTheFish
Joined: 27 May 2007 Posts: 4999
|
|
| Back to top |
|
 |
theungod
Joined: 05 Aug 2009 Posts: 25
|
Posted: Tue Feb 02, 2010 4:27 pm Post subject: |
|
|
| Quote: | | If the key string is always going to be located on a line after "Total Invoices for" a while loop will work as well: |
Good idea! You are correct. I tried to use that code, though, and got an error saying invalid variable name, so I took the "." out of the res variable name and replaced it with a ":" and it works. But it only gives me one "item" in the messagebox, and doesn't save the entire string to the clipboard, so I can just use clipboard =%res% but, how do I get all of the KABD-0208 KAY-0208 KABC-0208, etc etc into there? Sometimes there are hundreds, more than just 3. |
|
| Back to top |
|
 |
sinkfaze
Joined: 18 Mar 2008 Posts: 5043 Location: the tunnel(?=light)
|
|
| Back to top |
|
 |
theungod
Joined: 05 Aug 2009 Posts: 25
|
Posted: Tue Feb 02, 2010 5:01 pm Post subject: |
|
|
| sinkfaze wrote: | | That code works fine as is on my end, are you using the latest version of AutoHotkey? |
I am now, I was using an older version. And now it works.
The only problem I have now is that when I put in the actual text for the 'var', its wayyy too long for AHK to process (error: Continuation section too long). There's hundreds and hundreds of lines. Is there a way the 'var' can be an entire text file, using the FileRead command or something? |
|
| Back to top |
|
 |
aaffe
Joined: 17 May 2007 Posts: 1002 Location: Germany - Deutschland
|
Posted: Tue Feb 02, 2010 5:14 pm Post subject: |
|
|
You could try to set #MaxMem:
| Quote: | #MaxMem
--------------------------------------------------------------------------------
Sets the maximum capacity of each variable to the specified number of megabytes.
#MaxMem Megabytes
|
|
|
| Back to top |
|
 |
Lemming
Joined: 20 Dec 2005 Posts: 165 Location: Malaysia
|
Posted: Tue Feb 02, 2010 5:49 pm Post subject: simplified version |
|
|
I've simplified sinkfaze's script and added the actual "make dir" code. Plus, this version will handle any number of matches, not just 3.
A few notes:
- This script creates folders in the same place it is located. So don't run it on the desktop or you may suddenly find dozens of folders crowding your desktop.
- You really should have error-handling code during user input. Do not allow chars which cannot be used in directory names.
- This script does not handle duplicate matches.
- Use FileRead to read text files.
| Code: | SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
var=
(
Audio J Information
Date Sent: 01/22/10
Date Printed: 01/21/10
INVOICE CUSTOMER NAME # OF START # OF
# SPOTS DATE COPIES
KABC
855645 BIKE SHOP 1 02/12/10 1
856434 ELECTRIC WORK TESTING 3 02/08/10 1
Total Invoices for KABC 2 Total Spots 4 MP3 SPOTS EMAILED TO: pop thatguy@pop.com & pop3@pop.com
KAY
897940 ROOFING INCORPORATED 1 02/12/10 1
898816 JAMES GALVANIZING 3 02/08/10 1
INCORPORATED
899258 SANFORD BARBERS 2 02/11/10 1
899819 RHINESTONE RESTAURANT 3 02/08/10 1
Total Invoices for KAY 4 Total Spots 9 MP3 SPOTS EMAILED TO: frank@sho3.com
KABEA
899194 PERIWINKLE P U D 8 02/08/10 1
Total Invoices for KABEA 1 Total Spots 8 MP3 SPOTS EMAILED TO: pop
)
FolderList =
InputBox, cOut, Custom Output, Please enter a custom amount., , 200, 125
if !cOut {
MsgBox, 48, Warning, No value was entered, please try again.
return
}
Loop, Parse, var, `n, `r
{
if RegExMatch(A_LoopField, "^[A-Z]{3,5}$" , match )
{
FolderList = %FolderList%%match%-%cOut%`r`n
FileCreateDir, %match%-%cOut% ; creates folders like KABEA-0208
}
}
MsgBox The following folders were created: `n%FolderList%
return |
| theungod wrote: |
Good idea! You are correct. I tried to use that code, though, and got an error saying invalid variable name, so I took the "." out of the res variable name and replaced it with a ":" and it works. But it only gives me one "item" in the messagebox, and doesn't save the entire string to the clipboard, so I can just use clipboard =%res% but, how do I get all of the KABD-0208 KAY-0208 KABC-0208, etc etc into there? Sometimes there are hundreds, more than just 3. |
|
|
| Back to top |
|
 |
sinkfaze
Joined: 18 Mar 2008 Posts: 5043 Location: the tunnel(?=light)
|
Posted: Tue Feb 02, 2010 5:51 pm Post subject: |
|
|
A hybrid of my last two answers will probably work:
| Code: | InputBox, cOut, Custom Output, Please enter a custom amount., , 200, 125
if !cOut {
MsgBox, 48, Warning, No value was entered, please try again.
return
}
Loop, Read, << some file path >>
if RegExMatch(A_LoopReadLine,"Total Invoices for ([A-Z]+)",m)
res.=m1 "-" cOut A_Space
MsgBox % SubStr(res,1,StrLen(res)-1) ; trims off the last space
VarSetCapacity(res,0)
return |
_________________ Try Quick Search for Autohotkey or see the tutorial for newbies. |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|