1.

Solve : How to escape from ampersand.?

Answer»

Is there any way to REPLACE the occurance of the ampersand character in a string in Dos?
Say I want to replace "&" with the string ampersand.
I've tried:
CODE: [Select]set str=%str:&=ampersand%

And it doesn't work. The string is truncated at the position of the ampersand and I get an error.they say that in DOS, you use carrot ^ to escape. but if you can use tools like sed (see my sig)
Code: [Select]C:\test>more file.txt
this is a line with & blah

C:\test>sed "s/\&/AMPER/" file.txt
this is a line with amper blah

Quote

they say that in DOS, you use carrot ^ to escape. but if you can use tools like sed (see my sig)
Code:

C:\test>more file.txt
this is a line with & blah

C:\test>sed "s/\&/amper/" file.txt
this is a line with amper blah
This requires an external utility which I was hoping top avoid, besides I don't want to change the occurrences of & in an entire file only a string in a batch file.
(but thanks for your reply anyway) Try this

Code: [Select]set "str=%str:&=ampersand%"

Hi-
Quote
Code:

set "str=%str:&=ampersand%"

Yes this works ,but I guess I should mention that I'm still working on getting the target to shortcuts on the desktop
and, while my batch file works for .lnk files, it also works for .URL files on the desktop as well..
Except for if the .URL file's target contains an ampersand.
Then the target returned from DecodeShortCut.vbs is truncated, so the substitution fails because there is no & to replace.

Code: [Select]@echo off
Set SHORTCUT=%~1
echo set WshShell = WScript.CreateObject("WScript.Shell")>DecodeShortCut.vbs
echo set Lnk = WshShell.CreateShortcut(WScript.Arguments.Unnamed(0))>>DecodeShortCut.vbs
echo wscript.Echo Lnk.TargetPath>>DecodeShortCut.vbs
set vbscript=C:\WINDOWS\system32\cscript DecodeShortCut.vbs
For /f "delims=" %%T in ( ' %vbscript% "%Shortcut%" ' ) do set target=%%T
del DecodeShortCut.vbs
Echo Shortcut %shortcut%
Echo Target %target%
set target=%target:?=questionmark%
set "target=%target:&%=ampersand%"
Echo Target %target%>target.txtIs there some way to catch this?
Like for example:
Code: [Select]@echo off
Set Shortcut=%~1
echo set WshShell = WScript.CreateObject("WScript.Shell")>DecodeShortCut.vbs
echo set Lnk = WshShell.CreateShortcut(WScript.Arguments.Unnamed(0))>>DecodeShortCut.vbs
set targetpath=Lnk.TargetPath
set "targetpath=%targetpath:&%=ampersand%"
echo wscript.Echo targetpath>>DecodeShortCut.vbs
set vbscript=C:\WINDOWS\system32\cscript DecodeShortCut.vbs
For /f "delims=" %%T in ( ' %vbscript% "%Shortcut%" ' ) do set target=%%T
del DecodeShortCut.vbs
Echo Shortcut %shortcut%
Echo Target %target%
set target=%target:?=questionmark%
set "target=%target:&%=ampersand%"
Echo Target %target%>target.txtDo you think this might work?Quote
Do you think this might work?
No I tested it and it does not work.
Somehow the call to:
Code: [Select]echo wscript.Echo Lnk.TargetPath>>DecodeShortCut.vbs
returns a string which is already truncated at the position of the &!!Quote from: nubia on April 12, 2009, 11:25:46 AM
No I tested it and it does not work.
Somehow the call to:
Code: [Select]echo wscript.Echo Lnk.TargetPath>>DecodeShortCut.vbs
returns a string which is already truncated at the position of the &!!

You cannot mix batch and vbs code in that way.
Quote
You cannot mix batch and vbs code in that way.
I see...so is it possible to add a line to DecodeShortCut.vbs that will do the replace?
I'm sorry but I know less of vbs code than I do of batch (which is, sad to say , not much).DecodeShortcut.vbs

Code: [Select]
Set WshShell = WScript.CreateObject("WScript.Shell")
shortcut = WScript.Arguments.Unnamed(0)
Set Lnk = WshShell.CreateShortcut(shortcut)
Target = Lnk.TargetPath
If InStr(Target,"&")>0 Then
Wscript.echo Replace (Target,"&","AMPERSAND")
Else
Wscript.echo Target
End If
set Lnk = Nothing


Batch file to use it

Code: [Select]
@echo off
Set Shortcut=%~1
set vbscript=C:\WINDOWS\system32\cscript DecodeShortCut.vbs
For /f "delims=" %%T in ( ' %vbscript% "%Shortcut%" ' ) do set target=%%T
Echo Shortcut %shortcut%
Echo Target %target%
Echo Target %target%>target.txt

Quote from: Dias de verano on April 12, 2009, 12:47:51 AM
Try this

Code: [Select]set "str=%str:&=ampersand%"


Would that work for precent signs too?


Discussion

No Comment Found