Generating an error in vbscript

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
fthomas
Pro Scripter
Posts: 91
Joined: Fri Oct 03, 2008 6:40 pm

Generating an error in vbscript

Post by fthomas » Mon Oct 06, 2008 9:07 pm

Good day,

Have a good one for you all.

I am running the line in my code:

VBEval>Trim("%line%"),line_trimmed

To trim off leading and lagging spaces if they are there on strings.

Now, if I put in the string:

Focus on important points like how your product can make life and things easier and more enjoyable. Include compelling subject lines in the email. As much as possible, avoid using the word "free" because there are still older spam filters that dumps those kind of contents into the junk before even anyone reading them first. Convince those who signed up for your free reports that they will be missing something big if they do not avail of your products and services.

VB throws an error: Microsoft VBScript compilation error :1006 Expected')' Line2, column 196

Now, I did find out if I remove the ""s around the word free in the string, problem is solved. But I don't want to remove the quotes.

Do I have any opinions? Or do I have to remove quotes?

Thanks!

Frank.

fthomas
Pro Scripter
Posts: 91
Joined: Fri Oct 03, 2008 6:40 pm

Fixed

Post by fthomas » Mon Oct 06, 2008 10:41 pm

After several hours of searching...

Problem solved.

In case another comes to this problem, vbscript can delimit a double quote in a string by putting a second double quote beside it.

So " becomes "" in the string.

So, I simply added the following command, just before the vbscript statement:

StringReplace>line,","",line

After vb completes the ascii command, it automatically strips out the other " for some reason and everything is back to what it was.

:oops: Phew.

F.

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

Post by Marcus Tettmar » Tue Oct 07, 2008 5:07 am

VB, VBA, VBScript etc use the " char to delimit a string. Therefore a single quote char inside the string will confuse it - it will think it's the end of the string and the characters after it are now not terminated correctly. So, as you have found, you double quote quotes. It's a VB thing.

http://www.google.com/search?q=VB+quote ... +in+string
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

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

fthomas
Pro Scripter
Posts: 91
Joined: Fri Oct 03, 2008 6:40 pm

Thank you Marcus

Post by fthomas » Tue Oct 07, 2008 3:00 pm

On the same topic, I keep running into control character problems within scripts. Is there an exhaustive example list for this?

Please let me explain with my latest problem. I am writing a script that looks at text files and formats them to an acceptable format for another program.

ReadLn>file_name,1,line
WriteLn>modded_filename,result_write,{Title:%line%}

In the code above, a line of text, (title), is extracted from one file to be written to another one.

Here's what I start with, and this is what I want to finish this in the modded file:

Start:
This is the title of my article

Finish:
{Title:This is the title of my article}

Now, the code above works, but it throws an error, "Unknown Identifier: Title"

Thanks!

Frank.

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

Post by Marcus Tettmar » Tue Oct 07, 2008 5:34 pm

{ ... } are delimiters for complex expressions. Rather like quote chars are VBScript string delimiters and therefore need to be double quoted if placed inside strings, you need to do the following:

WriteLn>modded_filename,result_write,{"{Title:" + %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?

fthomas
Pro Scripter
Posts: 91
Joined: Fri Oct 03, 2008 6:40 pm

Post by fthomas » Tue Oct 07, 2008 10:54 pm

Marcus,

Thanks for the reply, but now MS is thowing the following errors with your recommendation:

Line: 107 Unknown Identifier A
Line: 107 Error Parsing Expression
Line: 107 G Not Appropiate

Line 107 is:
WriteLn>modded_filename,result_write,{"{Title:" + %line% + "}"}


I did a bit of 'hack' for a better word and it throws no errors:
let>line=\{Title:%line%\}
StringReplace>line,\,,line
WriteLn>modded_filename,result_write,line

But I'd still like to find out what the heck I'm doing wrong.

Frank.

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

Post by Marcus Tettmar » Wed Oct 08, 2008 7:47 am

This works fine for me:

Let>line=TEST
Let>output={"{Title:" + %line% + "}"}
WriteLn>modded_filename,result_write,output
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

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

fthomas
Pro Scripter
Posts: 91
Joined: Fri Oct 03, 2008 6:40 pm

Post by fthomas » Wed Oct 08, 2008 9:18 pm

Again, I'm finding big issues with analysing strings with a double quote in it.

I'm performing a letter by letter check, looking for capital letters in the line. The idea behind the command below is to extract each character to analysis it:
let>char_string={copy(%line%,%line_char%,1)}
Now, if this command finds a " character, it throws an error:

"Incorrectly formed parameters"
"Invalid parameters to COPY"
"A not appropiate"

This is the data this command is being feed by %line%:
"Andropause Mystery: Unraveling Truths about the Male Menopause", the enlightenment of men's predicament on aging
I'm about to throw up my hands! I've already had other exceptions like this because of a { or " in a string and it's driving me batty.

Is there any way to have MS not translate these characters in a string into command characters?

What the heck am I doing wrong????? :x

Frank.

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

Post by Marcus Tettmar » Wed Oct 08, 2008 9:28 pm

As previously stated, " is the string delimiter. So you need to double quote it. { ... } denotes a complex expression, so any string starting and ending as such would need special treatment.

You would have exactly the same issue when using Microsoft VB, VBScript or VBA in any Microsoft Office application. Or for that matter ANY other programming language.

So, replace all occurences of " with "". Here's a loop that reads each character in turn from a string containing quotes and } chars:

Code: Select all

Let>line=a"GheYTudjeYaotds"rfd}YYFkJJG"|*(&(*SIEddde
StringReplace>line,","",line

Length>line,theLen
Let>k=0
Repeat>k
 Let>k=k+1
 //MidStr>line,k,1,char_string
 let>char_string={copy(%line%,%k%,1)}
 Message>char_string
Until>k=theLen
Notice my commented out MidStr line. That's a native MacroScript function which cares not about quotes. So an alternative, which does NOT need any special escaping to the string is:

Code: Select all

Let>line=a"GheYTudjeYaotds"rfd}YYFkJJG"|*(&(*SIEddde

Length>line,theLen
Let>k=0
Repeat>k
 Let>k=k+1
 MidStr>line,k,1,char_string
 Message>char_string
Until>k=theLen
Note in the second example there is no need for the StringReplace as MidStr does not care about string delimiters.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

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

fthomas
Pro Scripter
Posts: 91
Joined: Fri Oct 03, 2008 6:40 pm

Post by fthomas » Wed Oct 08, 2008 10:02 pm

I did perform a replacement go from " to "" a few lines earlier. If I perform this action again, a vbscript code a little lower falls over then.

My only solution was to perform a replace in the script from " to '.


I realize that there is a learning curve here and if it's any consulation, It's frustrating for me too!

I wish that the MS language didn't have so many quirks around variables. For instance, I had a line that said somthing like:
Let>ThisVariable=ThatVariable
So, if ThatVariable was equal to Howdy, Then ThisVariable is also equal to Howdy.

I needed to add a space after ThatVariable, so I changed the command to:
Let>ThisVariable=ThatVariable <--with a space at the end.
So, MS may make a ton of sense, and the language may be the best on the planet, but for me, it's like watching a game on TV where I don't know the rules to the game.

So, I keep falling into troubles. Previously, your online help suggested that I read the variables section in the manual, but I didn't still get it.

Frank.
Last edited by fthomas on Thu Oct 09, 2008 1:27 am, edited 2 times in total.

fthomas
Pro Scripter
Posts: 91
Joined: Fri Oct 03, 2008 6:40 pm

Post by fthomas » Wed Oct 08, 2008 10:12 pm

Here's the program that i'm fighting with. Maybe you can tell me all the errors in my ways. Or not.

Code: Select all

VBSTART
VBEND

//-----------------------------------------------------
//Get directory where *.txt files are located
//-----------------------------------------------------
Let>INPUT_BROWSE=2
Input>txt_dir,Select desired directory,C:\test_articles
let>txt_zip_dir=txt_dir
ConCat>txt_dir,\*.txt
GetFileList>txt_dir,collected_files
Separate>collected_files,;,file_names
if>file_names_count=0,script_finished
let>fn_count=0
Repeat>fn_count
let>fn_count=fn_count+1
let>file_name=file_names_%fn_count%

//beginning of modifying text files
//
//makes sure original file is copied to a .old extension for protection
//let>file_name=C:\test_articles\title spans two lines and has line breaks.txt
StringReplace>file_name,.txt,.old,file_name_old
copy>file_name,file_name_old

//Begin process of stripping off spaces, keywords, and post word counts.
//----------------------------------------------------------------------
let>line_num=1
let>is_capitals=0
label>start
ReadLn>file_name,line_num,line
if>line=##EOF##,finish

//strip off spaces on front and rear of strings
StringReplace>line,",',line
VBEval>Trim("%line%"),line_trimmed
let>line=line_trimmed

//Analysis first string for keywords (no caps)
if>line_num=1
     let>line_char=0
     //analysis line character by character
	 repeat>line_char
	   let>line_char=line_char+1
	   //MessageModal>%line% %line_num%
       //StringReplace>line,",,line
	   let>char_string={copy(%line%,%line_char%,1)}
	   //Check to see if at end of line (no character)
	   if>char_string<VBEval>Asc("%char_string%"),asciiresult

			//check to see if between A-Z
			if>asciiresult>64
			     if>asciiresult<91>is_capitals=1
				 endif
			endif

		else
		    let>line_char=-1
		endif
	until>line_char=-1

//remove old version of modified file if present
     let>modded_filename=file_name
	 ConCat>modded_filename,.modified
	 IfFileExists>modded_filename
          DeleteFile>modded_filename
     endif

endif

//Write the first line to the modified file if it is not keywords otherwise drop the line.
     if>is_capitals=0
	      let>is_capitals=1
	      let>line_num=2
	 else
		  let>line_test1=pos("Word",%line%)
		  let>line_test2=pos("PPP",%line%)
		  if>line_test1=2,skipping_line
		  if>line_test2=1,skipping_line
		  //result_write is not used below
		  WriteLn>modded_filename,result_write,line
          label>skipping_line
	 endif
endif

let>line_num=line_num+1
goto>start
Label>finish
DeleteFile>file_name
MoveFile>modded_filename,file_name
//----------------------------------------------------------------------
//Check to see if line2 is an extension of title (less then 20 characters)
//concate second line to first if it is more then 1 and less then 20 characters
//----------------------------------------------------------------------
let>line_num=1
ReadLn>file_name,line_num,line
ReadLn>file_name,2,line_two
Length>line_two,line_two_length
if>{(%line_two_length%>1) AND (%line_two_length%<20>line, %line_two%
	 //result_write is not used below
	 WriteLn>modded_filename,result_write,line
else
     WriteLn>modded_filename,result_write,line
	 WriteLn>modded_filename,result_write,line_two
endif
let>line_num=3
label>start_part2
ReadLn>file_name,line_num,line
if>line=##EOF##,finish_part2
WriteLn>modded_filename,result_write,line
let>line_num=line_num+1
Goto>start_part2
Label>finish_part2
DeleteFile>file_name
MoveFile>modded_filename,file_name
//------------------------------------------------------------------

//Rewrite title with {}'s and remove space below title if it's there
//let>line_num=1
ReadLn>file_name,1,line
//MessageModal>line
ReadLn>file_name,2,line_two
//MessageModal>line_two
let>line=\{Title:%line%\}
StringReplace>line,\,,line
WriteLn>modded_filename,result_write,line
Length>line_two,line_two_length
if>line_two_length>3
     MessageModal>line two was seen to be more then 3 characters long
	 //MessageModal>(%length(line_two)%)
	 WriteLn>modded_filename,result_write,line_two
endif
let>line_num=3
label>start_part3
     //MessageModal>line_num
	 ReadLn>file_name,line_num,line
     //Messagemodal>line
	 //wait>2
	 if>line=##EOF##,finish_part3
     WriteLn>modded_filename,result_write,line
	 let>line_num=line_num+1
Goto>start_part3
label>finish_part3
DeleteFile>file_name
MoveFile>modded_filename,file_name
//------------------------------------------------------------------

//Stripping off the excessive CRLF's from lines that do not have
//a period(45dec), question mark(63dec), colon(58dec) or
//explanation mark(33dec) at the end.

//starting from line 2, I will start to analysis the lines for
//the end character. When I don't find it, I will concate the
//string to the next string.
//
//should also try to make sure a blank line follows??
ReadLn>file_name,1,line
WriteLn>modded_filename,result_write,line

let>new_line={""}
let>line_num=2
Label>start_part4
ReadLn>file_name,line_num,line
if>line=##EOF##,finish_part4
//MessageModal>line
Length>line,line_length
//MessageModal>line_length
if>line_length=0
	 WriteLn>modded_filename,result_write,line
	 let>line_num=line_num+1
	 goto>start_part4
endif
MidStr>line,line_length,1,last_char
//MessageModal>last_char
ConCat>new_line,%line% 
//MessageModal>new_line
if>last_char="
     //check for blank line following.
     let>temp_line_num=line_num+1
	 ReadLn>file_name,temp_line_num,line
	 Length>line,line_length
	 if>line_length=0
	      WriteLn>modded_filename,result_write,new_line
	      let>new_line={""}
	 endif
else
     if>{(%last_char%=".") OR (%last_char%="!") OR (%last_char%="?") OR (%last_char%=":")}
          WriteLn>modded_filename,result_write,new_line
	      let>new_line={""}
	      //MessageModal>Line written to file as complete!
     else
          //check for blank line following.
          let>temp_line_num=line_num+1
	      ReadLn>file_name,temp_line_num,line
	      Length>line,line_length
	      if>line_length=0
	           WriteLn>modded_filename,result_write,new_line
	           let>new_line={""}
	      endif
     endif
endif

let>line_num=line_num+1
goto>start_part4
Label>finish_part4
DeleteFile>file_name
MoveFile>modded_filename,file_name

//looping to grab next file_name and process it
Until>fn_count,file_names_count
Label>script_finished

//compress the new *.txt's to the zipfile - articles.zip
//----------------------------------------------------------------------------
//let>txt_zip_dir=txt_dir
//ConCat>txt_dir,\*.txt
zip_add>(%txt_zip_dir%".zip"),txt_dir,9,0,r
Label>dead_end


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

Post by Marcus Tettmar » Tue Oct 14, 2008 3:18 pm

Did you see my last response, posted 8 minutes before your last? Did it help? If not please call me and we'll do a desktop visit and I can help you debug your script.
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

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

fthomas
Pro Scripter
Posts: 91
Joined: Fri Oct 03, 2008 6:40 pm

Post by fthomas » Tue Oct 14, 2008 9:16 pm

Thanks,

I did completely miss your last comment. It does simplify the task and eliminates the possible use of vb code. What's the old saying, when you need to deal with a nail, everything can look like a hammer.

I'm going to review my code more, but I would seek the pleasure of going through my script with you. I'm sure you'll be able to answer a lot of my unasked questions as well.

I'll pop you off an email.

Thanks,

Frank

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