3

I am trying to edit the properties of the shortcut using batch script. But the short cut name includes a character ® hence when i run the changeproperties.bat file it fails to read the file name correctly. I am able to do the same task via poweshell. My powershell script has belwo line and it works

    $shortCut = ("$desktop\testapp®.lnk")
    $shell = New-Object -COM WScript.Shell
    $shortcut = $shell.CreateShortcut($shortCut)  ## Open the lnk
    $shortcut.TargetPath = "C:\Users\Public\newtarget.bat"
    $shortCut.Save()

The machine where i will run this will not have permission to run powershell. Hence trying to write similar bat file.

    echo Set oWS = WScript.CreateObject("WScript.Shell") >> %SCRIPT%
    echo sLinkFile = "%USERPROFILE%\Desktop\testapp®©.lnk" >> %SCRIPT%
    echo Set oLink = oWS.CreateShortcut(sLinkFile) >> %SCRIPT%
    echo oLink.TargetPath = "C:\Users\Public\newtarget.bat" >> %SCRIPT%
    echo oLink.Save >> %SCRIPT%

But this is not working. When i run it creates shortcut with extra added special character like this testapp©.

How to correct this.

GAP
  • 395
  • 1
  • 5
  • 18

2 Answers2

3

Using a Batch + VBScript hybrid solution would let you circumvent any Batch codepage issues, as well as removing the need to echo your VBScript code to a secondary script.

<!-- : Begin batch script
@echo off
cscript //nologo "%~f0?.wsf"
exit /b

----- Begin wsf script --->
<job><script language="VBScript">
    Set oWS = WScript.CreateObject("WScript.Shell")
    userProfile = oWS.Environment("Process").Item("USERPROFILE")
    sLinkFile = userProfile & "\Desktop\testapp®.lnk"
    Set oLink = oWS.CreateShortcut(sLinkFile)
    oLink.TargetPath = "C:\Users\Public\newtarget.bat"
    oLink.Save
</script></job>
Community
  • 1
  • 1
rojo
  • 24,000
  • 5
  • 55
  • 101
  • Thank you for the script. First one had the same issue. Hackoo's script did the job for me. – GAP Apr 06 '16 at 19:31
  • @Gopichandan Yeah, I tested it and found the same issue, so I just removed it. Glad Hackoo's solution worked for you though! – rojo Apr 06 '16 at 19:31
2

You should first save your file with Notepad++ with ANSI Then execute this code and it will works for you.

@echo off
(   
    echo Set oWS = CreateObject("WScript.Shell"^)
    echo sLinkFile = "%USERPROFILE%\Desktop\testapp®.lnk"
    echo Set oLink = oWS.CreateShortcut(sLinkFile^)
    echo oLink.TargetPath = "C:\Users\Public\newtarget.bat"
    echo oLink.Save
)>%tmp%\%~n0.vbs
cscript /nologo %tmp%\%~n0.vbs
Hackoo
  • 18,337
  • 3
  • 40
  • 70