How to edit shortcut properties where shortcut nam

2019-04-12 20:31发布

问题:

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.

回答1:

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


回答2:

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>