using ebasic to replace all instances of "\" in a string with "\\"?

48 views
Skip to first unread message

jlar...@utk.edu

unread,
Oct 12, 2017, 1:29:34 PM10/12/17
to E-Prime

Hi everyone --


I'm using FileCopy to move files around between the local computer and shared network drives.

So I might want something that looks like this:
FileCopy "c:\path\file.txt", "s:\path\file.txt"

One challenge is that I seem to need to use "\\" to specify the path when I'm working with shared network drives. So it ends up looking like:
FileCopy "c:\path\file.txt", "s:\\path\\file.txt"


This works just fine. The problem is that everything is very dynamic and paths are specified with variables that all originate with CurDir$, which contains a bunch of instances of "\".

My question is this: How can I use Ebasic commands to replace all instances of "\" within a given variable with "\\"? I've started playing around with Mid$ and I think I could work it out, but it seems like there's got to be an easier way.

Thanks in advance.


--Jeff

David McFarlane

unread,
Oct 12, 2017, 1:46:11 PM10/12/17
to e-p...@googlegroups.com
Jeff,

The Mid$ routine does seem to be the way to go for this. See that topic
in the E-Basic Help.

You might also look into InStr(), Item$(), and ItemCount().

BTW, as long as you need to replace those backslashes (\) anyway, you
might as well replace them with forward slashes (/) for the path
separator in E-Prime.

For those who don't already know, The backslash character works as an
"escape" character in E-Basic, see en.wikipedia.org/wiki/Backslash . As
explained there, to escape the backslash character and get a literal
backslash, you must use a double backslash. But a forward slash works
in most environments as a path separator, and does not need escaping.

-- David McFarlane

Larsen, Jeff T

unread,
Oct 12, 2017, 1:54:22 PM10/12/17
to e-p...@googlegroups.com
Thanks a lot, David. I had also toyed around with InStr(), but I haven’t ever stumbled upon Item$(). I suspect this will simplify finding the backslashes. — Jeff
**********************
Jeff T. Larsen, Ph.D.
Professor of Psychology
University of Tennessee, Knoxville
jeff....@utk.edu  /    865-974-9967


--
You received this message because you are subscribed to a topic in the Google Groups "E-Prime" group.
To unsubscribe from this group and all its topics, send an email to e-prime+u...@googlegroups.com.
To post to this group, send email to e-p...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jlar...@utk.edu

unread,
Oct 12, 2017, 5:16:03 PM10/12/17
to E-Prime
Hi everyone -- I figured it out. As David suspected, mid$ came in handy. -- Jeff

Dim original$
Dim BeforeSlash$
Dim AfterSlash$
Dim revision$
Dim posn As Integer

'make original$ equal to current directory
original$ = curdir$

'initialize revision$
revision$ = original$

For posn = len(original$) To 1 Step -1                            'step backwards from end of original$ to the beginning, one character at a time
    If mid$(original$,posn,1) = Basic.PathSeparator$ Then        'note: Basic.PathSeparator = "\"
        BeforeSlash$ = left$(revision$,posn-1)                    'string containing text preceding the slash
        AfterSlash$ = right$(revision$,len(revision$) - posn)    'string containing text after the slash
        revision$   = BeforeSlash$ & "//" & AfterSlash$            'concatenation
    End If
Next posn
msgbox original$ & " --> " & revision$
End
Reply all
Reply to author
Forward
0 new messages