Spotting a tab in a txt file?

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1417
Joined: Sun Nov 03, 2002 3:19 am

Spotting a tab in a txt file?

Post by Dorian (MJT support) » Sun Sep 23, 2012 6:17 pm

Hi guys and girls,

I'm copying and pasting text from pdfs and word documents into a notepad file, so i can parse all the data and make a set of ini files.

The data in the documents is in a table, and when i paste it into notepad, i get the raw text with no formatting.

I want to extract that raw text, and create an ini file, with the values in each line separated by a |

So, how do i use string commands to search for a tab, or a carriage return?

If there's a simpler way of extracting the data, I'd like to know about it, of course. But for now, my way should work.

Below is a stripped down version of a similar script I use to take names from names.txt, and make ini files. It reads a second file too, and combines them both, but I removed all that for the sake of simplicity.

Can I use

Code: Select all

VBEval>Replace("%linedash%","SEARCH","REPLACE"),linedash
to search and replace a tab or a CR?

Code: Select all

VBSTART
VBEND

// READ names.txt
Let>k=1
Label>start
ReadLn>c:\names.txt,k,line
If>line=ENDFILE,finish

VBEval>UCase("%line%"),linecaps
let>linedash=line

//CREATE OUTPUT

//LINE 1 - NAME WITH DASH
VBEval>LCase("%linedash%"),linedash
VBEval>Replace("%linedash%","SEARCH","REPLACE"),linedash
WriteLn>c:\ini-generated\%line%.txt,result,%linedash%

//LINE 2 - NAME CAPS
WriteLn>c:\ini-generated\%line%.txt,result,%linecaps%


//LINE 3 - 6 ARTIST URLS
WriteLn>c:\ini-generated\%line%.txt,result,

WriteLn>c:\ini-generated\%line%.txt,result,2012-%linedash%1.htm
WriteLn>c:\ini-generated\%line%.txt,result,2012-%linedash%2.htm
WriteLn>c:\ini-generated\%line%.txt,result,2012-%linedash%3.htm
WriteLn>c:\ini-generated\%line%.txt,result,2012-%linedash%4.htm
WriteLn>c:\ini-generated\%line%.txt,result,2012-%linedash%5.htm
WriteLn>c:\ini-generated\%line%.txt,result,2012-%linedash%6.htm
WriteLn>c:\ini-generated\%line%.txt,result,2012-%linedash%-biography.htm

WriteLn>c:\ini-generated\%line%.txt,result,

Let>k=k+1
Goto>start
Label>finish


User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1417
Joined: Sun Nov 03, 2002 3:19 am

Post by Dorian (MJT support) » Sun Sep 23, 2012 6:56 pm

Thanks to the search facility on this site, I figured it out. Here is a working example, in case any of you need it.

Code: Select all

VBSTART
VBEND

// READ tabtest.txt
Let>k=1
Label>start
ReadLn>c:\tabtest.txt,k,line
If>line=ENDFILE,finish

//replace tabs with |
VBEVal>Replace("%line%",vbTab,"|"),line

// or, as in the example i found, you could replace the tab with seven spaces
//VBEVal>Replace("%line%",vbTab,Space(7)),line


//write output
WriteLn>c:\tabout.txt,result,%line%

Let>k=k+1
Goto>start
Label>finish

executefile>c:\tabout.txt
Then I will replace any instances of "||" with "|", where there were multiple tabs.

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1417
Joined: Sun Nov 03, 2002 3:19 am

Post by Dorian (MJT support) » Sun Sep 23, 2012 7:36 pm

Just in case these extra little bits are of use to anyone, here's how it's progressed.

Code: Select all

VBSTART
VBEND

//delete old output file if it exists
iffileexists>c:\2011-tfcw\tabout.txt
DeleteFile>c:\2011-tfcw\tabout.txt
ELSE
ENDIF

// READ tabtest.txt
Let>k=1
Label>start
ReadLn>c:\2011-tfcw\tabtest.txt,k,line
If>line=ENDFILE,finish
If>line=##EOF##,finish

//replace tabs with |
VBEVal>Replace("%line%",vbTab,"|"),line

//remove up to ten multiples tabs
Let>mt=0
Repeat>mt
Let>mt=mt+1
VBEVal>Replace("%line%","||","|"),line
Until>mt,10

//remove trailing spaces
VBEVal>Replace("%line%"," |","|"),line

//check if the line contains more than 1 character, and write it if it does
len>line,linelen
if>linelen>1
//write output
WriteLn>c:\2011-tfcw\tabout.txt,result,%line%
else
endif


Let>k=k+1
Goto>start
Label>finish

executefile>c:\2011-tfcw\tabout.txt


This pretty much gives me a file i can use to read from another (unwritten) macro to write the data back into a table.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Mon Sep 24, 2012 8:06 am

You don't need to use VBScript:

StringReplace>line,TAB,|,line
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Dorian (MJT support)
Automation Wizard
Posts: 1417
Joined: Sun Nov 03, 2002 3:19 am

Post by Dorian (MJT support) » Wed Sep 26, 2012 6:41 pm

LOL, thanks Marcus.

Was that added into MS at some point?

I've been using the VBscript version for about six years, I think!

I really need to go through all the "new" commands, don't I?!?!?

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts