Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - How to concatenate a literal string and a variable value?

Reply
 
Old 10-02-2008   #1 (permalink)
Tony Bansten


 
 

How to concatenate a literal string and a variable value?

Assume I want to concatenate a literal text value and a variable value for a function parameter
then the following does NOT work:

objWorkbook.SaveAs("D:\work\v1_" + Filename)

The variable "Filename" is filled with a valid value.

So how do I otherwise concatenate two parts?

Tony


My System SpecsSystem Spec
Old 10-02-2008   #2 (permalink)
Pegasus \(MVP\)


 
 

Re: How to concatenate a literal string and a variable value?


"Tony Bansten" <tonytony@xxxxxx> wrote in message
news:48e51d7f$0$6614$9b4e6d93@xxxxxx-online.net...
Quote:

> Assume I want to concatenate a literal text value and a variable value for
> a function parameter
> then the following does NOT work:
>
> objWorkbook.SaveAs("D:\work\v1_" + Filename)
>
> The variable "Filename" is filled with a valid value.
>
> So how do I otherwise concatenate two parts?
>
> Tony
>
Use the ampersand ("&") concatenator.


My System SpecsSystem Spec
Old 10-02-2008   #3 (permalink)
sloan


 
 

Re: How to concatenate a literal string and a variable value?



What is the object type for "FileName"

Do you have
OPTION EXPLICIT ON
OPTION STRICT ON

or do you have

dim Filename
(which means dim Filename as object)
............

or do you have ?
dim fileName as string

Are you using VB.NET?

If you're not, then why are you posting to a VB.NET newsgroup
(microsoft.public.dotnet.languages.vb)?





"Tony Bansten" <tonytony@xxxxxx> wrote in message
news:48e51d7f$0$6614$9b4e6d93@xxxxxx-online.net...
Quote:

> Assume I want to concatenate a literal text value and a variable value for
> a function parameter
> then the following does NOT work:
>
> objWorkbook.SaveAs("D:\work\v1_" + Filename)
>
> The variable "Filename" is filled with a valid value.
>
> So how do I otherwise concatenate two parts?
>
> Tony
>

My System SpecsSystem Spec
Old 10-02-2008   #4 (permalink)
Richard Mueller [MVP]


 
 

Re: How to concatenate a literal string and a variable value?


"Tony Bansten" <tonytony@xxxxxx> wrote in message
news:48e51d7f$0$6614$9b4e6d93@xxxxxx-online.net...
Quote:

> Assume I want to concatenate a literal text value and a variable value for
> a function parameter
> then the following does NOT work:
>
> objWorkbook.SaveAs("D:\work\v1_" + Filename)
>
> The variable "Filename" is filled with a valid value.
>
> So how do I otherwise concatenate two parts?
>
> Tony
>
You should use the "&" operator to concatenate string values, but "+" can
work. If fails if the values are numeric, in which case it performs
addition, which is why "&" is preferred.

It may be the result of the concatenation is invalid. That is, if Filename =
"test.txt", the result of the concatenation will be "D:\work\v1_test.txt". I
would troubleshoot with code similar to:

strFile = "D:\work\v1_" & Filename
Wscript.Echo strFile
objWorkbook.SaveAs(strFile)

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


My System SpecsSystem Spec
Old 10-02-2008   #5 (permalink)
Herfried K. Wagner [MVP]


 
 

Re: How to concatenate a literal string and a variable value?

"Tony Bansten" <tonytony@xxxxxx> schrieb:
Quote:

> Assume I want to concatenate a literal text value and a variable value for
> a function parameter
> then the following does NOT work:
>
> objWorkbook.SaveAs("D:\work\v1_" + Filename)
>
> The variable "Filename" is filled with a valid value.
Your code should work (at least if the first parameter of the 'SaveAs'
method should be passed 'ByVal'), but note that VB has its dedicated string
concatenation operator '&'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

My System SpecsSystem Spec
Old 10-02-2008   #6 (permalink)
Ken Halter


 
 

Re: How to concatenate a literal string and a variable value?

"Tony Bansten" <tonytony@xxxxxx> wrote in message
news:48e51d7f$0$6614$9b4e6d93@xxxxxx-online.net...
Quote:

> Assume I want to concatenate a literal text value and a variable value for
> a function parameter
> then the following does NOT work:
>
> objWorkbook.SaveAs("D:\work\v1_" + Filename)
That looks VBAish... if it were VB Classic, the "preferred" way to
concatenate strings would be to use an '&' symbol...

ThisStringVar = "D:\work\v1_" & Filename

But... sloan's right... if you're using that "thing" called vb.net (even
though the ".net" was removed from the name years ago, just adding to the
confusion.net), you'll find VBA or VB groups to be far more helpful.



My System SpecsSystem Spec
Old 10-03-2008   #7 (permalink)
Al Dunbar


 
 

Re: How to concatenate a literal string and a variable value?


"Ken Halter" <Ken_Halter@xxxxxx_Sparingly_Hotmail.com> wrote in message
news:%23BvscaPJJHA.4936@xxxxxx
Quote:

> "Tony Bansten" <tonytony@xxxxxx> wrote in message
> news:48e51d7f$0$6614$9b4e6d93@xxxxxx-online.net...
Quote:

>> Assume I want to concatenate a literal text value and a variable value
>> for a function parameter
>> then the following does NOT work:
>>
>> objWorkbook.SaveAs("D:\work\v1_" + Filename)
>
> That looks VBAish... if it were VB Classic, the "preferred" way to
> concatenate strings would be to use an '&' symbol...
>
> ThisStringVar = "D:\work\v1_" & Filename
>
> But... sloan's right... if you're using that "thing" called vb.net (even
> though the ".net" was removed from the name years ago, just adding to the
> confusion.net), you'll find VBA or VB groups to be far more helpful.
IMHO, this is a good argument for using FSO methods to construct valid file
paths instead of manually doing it with string manipulation techniques,
i.e.:

set fso = createobject("scripting.filesystemobject")
ThisStringVar = fso.buildpath( "D:\work\v1_", Filename )

I suspect that this would resolve the OP's problem, as the resulting value
(for a filename of "theFile.txt") would be this:

"D:\work\v1_\theFile.txt"

Instead of the value produced by his original code:

"D:\work\v1_theFile.txt"

At least, it is my understanding that if "theFile.txt" is the filename, it
must be preceded by a backslash to separate it from the parent folder
portion of the path.

/Al


My System SpecsSystem Spec
Old 10-03-2008   #8 (permalink)
Tom Lavedas


 
 

Re: How to concatenate a literal string and a variable value?

On Oct 3, 12:11*am, "Al Dunbar" <AlanD...@xxxxxx> wrote:
Quote:

> "Ken Halter" <Ken_Halter@xxxxxx_Sparingly_Hotmail.com> wrote in message
>
> news:%23BvscaPJJHA.4936@xxxxxx
>
>
>
Quote:

> > "Tony Bansten" <tonyt...@xxxxxx> wrote in message
> >news:48e51d7f$0$6614$9b4e6d93@xxxxxx-online.net...
Quote:

> >> Assume I want to concatenate a literal text value and a variable value
> >> for a function parameter
> >> then the following does NOT work:
>
Quote:
Quote:

> >> objWorkbook.SaveAs("D:\work\v1_" + Filename)
>
Quote:

> > That looks VBAish... if it were VB Classic, the "preferred" way to
> > concatenate strings would be to use an '&' symbol...
>
Quote:

> > ThisStringVar = "D:\work\v1_" & Filename
>
Quote:

> > But... *sloan's right... if you're using that "thing" called vb.net (even
> > though the ".net" was removed from the name years ago, just adding to the
> > confusion.net), you'll find VBA or VB groups to be far more helpful.
>
> IMHO, this is a good argument for using FSO methods to construct valid file
> paths instead of manually doing it with string manipulation techniques,
> i.e.:
>
> * * set fso = createobject("scripting.filesystemobject")
> * * ThisStringVar = fso.buildpath( "D:\work\v1_", Filename )
>
> I suspect that this would resolve the OP's problem, as the resulting value
> (for a filename of "theFile.txt") would be this:
>
> * * "D:\work\v1_\theFile.txt"
>
> Instead of the value produced by his original code:
>
> * * "D:\work\v1_theFile.txt"
>
> At least, it is my understanding that if "theFile.txt" is the filename, it
> must be preceded by a backslash to separate it from the parent folder
> portion of the path.
>
> /Al
Your argument sounds good, unless the OP really is after changing the
file's original name in the SaveAs process (which is how I read the
the need - though the OP is decidedly mute on this issue).

It occurs to me that without the content of Filename and the
subsequent concatenation to examine, plus the all too common &
uninformative 'does NOT work' declaration of the problem, anything
could be happening. For example, Filename might not contain the
correct file extension. In that case, I believe the SaveAs would
throw an error that might be misconstrued as a 'failure to
concatenate' when in fact its a file type error.

Without more info from the OP, it's just pure speculation on our part
to try to say what's wrong (because the original code is NOT the
problem).

Tom Lavedas
===========
http://members.cox.net/tglbatch/wsh/
My System SpecsSystem Spec
Old 10-03-2008   #9 (permalink)
Chris Dunaway


 
 

Re: How to concatenate a literal string and a variable value?

On Oct 2, 2:14 pm, tonyt...@xxxxxx (Tony Bansten) wrote:
Quote:

> Assume I want to concatenate a literal text value and a variable value for a function parameter
> then the following does NOT work:
>
> objWorkbook.SaveAs("D:\work\v1_" + Filename)
>
> The variable "Filename" is filled with a valid value.
>
> So how do I otherwise concatenate two parts?
>
> Tony
Don't use the + operator to concatenate strings in VB.Net, use the &
operator. If you use +, VB may try to coerce one or both of the
operands, possibly yielding unexpected results.

Also, when working with paths, use the Path.Combine method to build
paths:

objWorkbook.SaveAs(Path.Combine("D:\work", "v1_" & Filename))

Ditto on sloan's questions about Option Strict and Option Explicit.

Chris
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Concatenate text string and text in variable with no space between PowerShell
Concatenate text string and text in variable with no space between VB Script
variable evaluation in a string PowerShell
Find a string within a variable string PowerShell
Subject: using a Variable with/in a string PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46