Computing.Net > Forums > Programming > Batch File:Replace text in variable

Batch File:Replace text in variable

Reply to Message Icon

Original Message
Name: cool_andy
Date: April 26, 2007 at 06:37:16 Pacific
Subject: Batch File:Replace text in variable
OS: Windows XP
CPU/Ram: 512
Model/Manufacturer: Acer
Comment:

How can I replace a character in a textfile or in a variable using a batch file?

e.g. first line in textfile
= $/Andrew Project:

If I store this in a variable:
set /P directory = < file.txt

I would then like to replace the colon : with a a forward slash / so it becomes:
$/Andrew Project/

This way can I use the directory variable and file variables from the textfile for visual source safe commands (ss history and ss pin etc).

incorrect directory because of colon! :
$/Andrew Project:defs.h

correct directory after replacing text
$/Andrew Project/defs.h

Thank you

Andrew Ashcroft


Report Offensive Message For Removal


Response Number 1
Name: ghostdog
Date: April 26, 2007 at 08:06:44 Pacific
Reply: (edit)

C:\>set var== $/Andrew Project:
C:\>echo %var:~0,-1%
= $/Andrew Project
C:\>set newpath=%var:~0,-1%/
C:\>echo %newpath%
= $/Andrew Project/

If you can afford to have GNU sed or awk, you can do string substitution easily.

[code]
C:\>echo = $/Andrew Project: | sed "s/:/\//"
= $/Andrew Project/
[/code]



Report Offensive Follow Up For Removal

Response Number 2
Name: IVO
Date: April 26, 2007 at 09:38:12 Pacific
Reply: (edit)

Set directory=%directory::=/%


Report Offensive Follow Up For Removal

Response Number 3
Name: cool_andy
Date: April 26, 2007 at 12:11:23 Pacific
Reply: (edit)

Thank you that seems useful. Even better if I can use Unix commands, I will try that tommorow.


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: Batch File:Replace text in 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