Computing.Net > Forums > Programming > how to remove \ from variable

how to remove \ from variable

Reply to Message Icon

Original Message
Name: newbie (by newtechie)
Date: September 18, 2007 at 15:27:06 Pacific
Subject: how to remove \ from variable
OS: windows server 2003
CPU/Ram: intel p4
Model/Manufacturer: dell
Comment:

Hi guys I'm very new to batch programming and am unable to find this:
i have managed to get a file location from a text file into a variable. However the file name is in the format c\:\\program files\\locationofprogram\\program.

I want to get this such that i can remove the extra '\' characters from this path, ie read c:\program files\locationofprogram\program. How do i do this in batch?? please help i'm going nuts trying to figure this one out


Report Offensive Message For Removal


Response Number 1
Name: tonysathre
Date: September 18, 2007 at 15:52:20 Pacific
Reply: (edit)

@echo off
for /f "tokens=1,3,4,5,7 delims=\" %%a in (test.txt) do (
echo %%a:\%%b\%%c\%%d
)

"Computer security." — Oxymoron


Report Offensive Follow Up For Removal

Response Number 2
Name: newbie (by newtechie)
Date: September 18, 2007 at 17:47:12 Pacific
Reply: (edit)

thanks for the response - works great
but now i'm wondering, if there is a way to automate this? ie. location could be longer/shorter with a different hierarchical structure...so hardcoding the formatting is kind of risky - is there a way to remove '\' with some kind of loop that compares the string charcter by character and concatenates?

any help is appreciated - thanks


Report Offensive Follow Up For Removal

Response Number 3
Name: Razor2.3
Date: September 18, 2007 at 17:56:17 Pacific
Reply: (edit)

set foo=c\:\\program files\\locationofprogram\\program
set foo=%foo:\:=:%
set foo=%foo:\\=\%
@echo %foo%


Report Offensive Follow Up For Removal

Response Number 4
Name: ghostdog
Date: September 18, 2007 at 19:33:44 Pacific
Reply: (edit)

though you can spend your time filtering out the extra slashes, my opinion is to tackle the problem at the source. Are you able to NOT generate file paths like that to the text file?


Report Offensive Follow Up For Removal

Response Number 5
Name: Mechanix2Go
Date: September 18, 2007 at 23:23:10 Pacific
Reply: (edit)

I'm with 3 & 4.


=====================================
If at first you don't succeed, you're about average.

M2



Report Offensive Follow Up For Removal


Response Number 6
Name: newbie (by newtechie)
Date: September 19, 2007 at 12:49:20 Pacific
Reply: (edit)

unfortunately - no
its written to in a read-only file by sepereate module..
but thanks for the responses

~newtechie


Report Offensive Follow Up For Removal

Response Number 7
Name: n1988
Date: October 12, 2007 at 08:36:56 Pacific
Reply: (edit)

I want to remove '$' from a $ variable.

I create a script, a.sh as below:

#!/usr/bin/ksh
a=`echo $1 | cut -d"\$" -f1`
b=`echo $1 | cut -d"\$" -f2`
echo $a$b

However, this script does not work as I expect

For example when I run the script as below:

a.sh abc$def.

The script displays 'abcabc' instead of 'abcdef'



Report Offensive Follow Up For Removal

Response Number 8
Name: tonysathre
Date: October 12, 2007 at 10:13:13 Pacific
Reply: (edit)

I suggest starting a new thread. You will get more responses.

"Computer security." — Oxymoron


Report Offensive Follow Up For Removal

Response Number 9
Name: Mechanix2Go
Date: October 13, 2007 at 01:30:42 Pacific
Reply: (edit)

Beside the obvious that shell scripts have little or nothing in common with winders.



=====================================
If at first you don't succeed, you're about average.

M2



Report Offensive Follow Up For Removal

Response Number 10
Name: ghostdog
Date: October 13, 2007 at 01:49:13 Pacific
Reply: (edit)

who knows he might be using cygwin or SFU...

@OP, $ is sensitive to the shell, when you pass the variable, escape it like this abc\$def.
should do the trick.


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: how to remove \ from variable

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge