Hi there

I have a script that is getting data from a mssql server and putting it
into a mysql serever.

One of the things i get is a windows username, wich is written
DOMAIN\username. The problem is the backslash that needs to be escaped.

This can be done using replace.

Replace("DOAMIN\username","\","\\") works just fine.

But, the script is using a variable, so its not DOMAIN\username but
UserName(i) wich is running in a loop.

replace(UserName(i),"\","\\") gives me "Invalid use of NULL: 'replace'"

replace("UserName(i)","\","\\") gives me UserName(i) everywhere.

How do i use replace on a variable?

/Kasper

Re: Replace function on a variable? by Pegasus

Pegasus
Mon Jul 07 06:27:53 CDT 2008


"Kasper Nordal Lund" <fake@usenet.com> wrote in message
news:uJ9Qs5A4IHA.4488@TK2MSFTNGP03.phx.gbl...
> Hi there
>
> I have a script that is getting data from a mssql server and putting it
> into a mysql serever.
>
> One of the things i get is a windows username, wich is written
> DOMAIN\username. The problem is the backslash that needs to be escaped.
>
> This can be done using replace.
>
> Replace("DOAMIN\username","\","\\") works just fine.
>
> But, the script is using a variable, so its not DOMAIN\username but
> UserName(i) wich is running in a loop.
>
> replace(UserName(i),"\","\\") gives me "Invalid use of NULL: 'replace'"
>
> replace("UserName(i)","\","\\") gives me UserName(i) everywhere.
>
> How do i use replace on a variable?
>
> /Kasper

The statement
x = Replace("DOAMIN\username","\","\\")
will set x to "DOAMIN\\username" (in spite of the spelling error).

The statement
x = replace(UserName(i),"\","\\")
will set x to the variable UserName(i), with all single backslashes replaced
by double backslashes, provided that UserName(i) exists and that the index
variable is within lbound and ubound of the array UserName. Did you test
these conditions with this simple statement?
wscript.echo "i=" & i & ", UserName(i)=" & UserName(i)

The statement
x = replace("UserName(i)","\","\\")
does exactly the same thing as your first example.



RE: Replace function on a variable? by OldPedant

OldPedant
Mon Jul 07 19:23:05 CDT 2008

> replace(UserName(i),"\","\\") gives me "Invalid use of NULL: 'replace'"

which of course means that, for at least one value of your variable
i
the value of
UserName(i)
is null.

You could always do
If Not IsNull( UserName(i) Then
UserName(i) = Replace( UserName(i), "\", "\\" )
End If

But a simpler way, if you don't care whether the value is NULL or just a
blank string, is to do
UserName(i) = Replace( "" & UserName(i), "\", "\\" )

This works because VBScript will happily append a NULL or EMPTY value to
*ANY* string (even a zero-length blank string) without changing the value of
that string. It's a happy side effect of the way the & [concatenation]
operator works.

>
> replace("UserName(i)","\","\\") gives me UserName(i) everywhere.
>
> How do i use replace on a variable?
>
> /Kasper
>