AutoHotkey Community

It is currently May 27th, 2012, 2:08 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 11 posts ] 
Author Message
PostPosted: February 2nd, 2010, 3:49 pm 
Offline

Joined: August 5th, 2009, 4:13 pm
Posts: 25
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?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 2nd, 2010, 4:10 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
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 FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 2nd, 2010, 4:25 pm 
Offline

Joined: August 5th, 2009, 4:13 pm
Posts: 25
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?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 2nd, 2010, 5:02 pm 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5480
Location: the tunnel(?=light)
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

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 2nd, 2010, 5:15 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
Why not use {min,max} as in {3,5} to determine the length? Also grep would be useful here 8)

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 2nd, 2010, 5:27 pm 
Offline

Joined: August 5th, 2009, 4:13 pm
Posts: 25
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 2nd, 2010, 5:51 pm 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5480
Location: the tunnel(?=light)
That code works fine as is on my end, are you using the latest version of AutoHotkey?

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 2nd, 2010, 6:01 pm 
Offline

Joined: August 5th, 2009, 4:13 pm
Posts: 25
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?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 2nd, 2010, 6:14 pm 
Offline

Joined: May 17th, 2007, 12:07 pm
Posts: 1004
Location: Germany - Deutschland
You could try to set #MaxMem:
Quote:
#MaxMem

--------------------------------------------------------------------------------

Sets the maximum capacity of each variable to the specified number of megabytes.

#MaxMem Megabytes


Report this post
Top
 Profile  
Reply with quote  
 Post subject: simplified version
PostPosted: February 2nd, 2010, 6:49 pm 
Offline

Joined: December 20th, 2005, 4:15 am
Posts: 165
Location: Malaysia
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 2nd, 2010, 6:51 pm 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5480
Location: the tunnel(?=light)
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

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], nimda, poserpro, rbrtryn, sjc1000 and 13 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group