1
votes

My Windows 8 PC doesn't seem to want to create Shortcuts using VBScript. The problem is, that it somehow can't be saved. This seems to be a problem only my PC has.

I am trying to create a temporary VBScript using Batch, the output I get is:

C:\Users\ALBERT~1\AppData\Local\Temp\11338-3520-31784-27073.vbs(5, 1) WshShortcut.Save: Shortcut "C:\Users\AlbertMøller\Downloads\Desktop\ASKontrolpanel.lnk" could not be saved.

This is my VBScript code:

set SCRIPT="%TEMP%\%RANDOM%-%RANDOM%-%RANDOM%-%RANDOM%.vbs"
echo Set oWS = WScript.CreateObject("WScript.Shell") >> %SCRIPT%
echo sLinkFile = "Desktop\ASKontrolpanel.lnk" >> %SCRIPT%
echo Set oLink = oWS.CreateShortcut(sLinkFile) >> %SCRIPT%
echo oLink.TargetPath = "%ProgramData%\AutoShutdown\AutomaticShutdown.bat" >> %SCRIPT%
echo oLink.Save >> %SCRIPT%
cscript /nologo %SCRIPT%
del %SCRIPT%

It seems to work on all other computers I have tested it on, which is 3. I try to open it with Administrator rights, but it still doesn't work.

2
Does the Desktop directory exist within Downloads? You might have meant echo sLinkFile = "%userprofile%\Desktop\ASKontrolpanel.lnk" >> %SCRIPT%rojo
Doesn't work either - have tried that already (and now again). It seems to be a problem with my computer. <- Hopefully fixableAlbert MN.
Salt this PowerShell one-liner to taste and see whether it gives you the same behavior.rojo
I am a noob at VBS... So exactly where do I need to put it, and what do I need to edit?Albert MN.
Replace everything between and including set Script and del %SCRIPT% (basically the whole damn thing) with the following: powershell "$s=(New-Object -COM WScript.Shell).CreateShortcut('%userprofile%\Desktop\ASKontrolpanel.lnk');$s.TargetPath='%ProgramData%\AutoShutdown\AutomaticShutdown.bat';$s.Save()"rojo

2 Answers

2
votes

This Vbscript can create a shortcut on your desktop asking you if you want to shutdown the computer or not.

Option Explicit
Dim MyScriptPath 
MyScriptPath = WScript.ScriptFullName
Call Shortcut(MyScriptPath,"Shutdown the computer")
Call AskQuestion()
'**********************************************************************************************
Sub Shortcut(PathApplication,Name)
    Dim objShell,DesktopPath,objShortCut,MyTab
    Set objShell = CreateObject("WScript.Shell")
    MyTab = Split(PathApplication,"\")
    If Name = "" Then
        Name = MyTab(UBound(MyTab))
    End if
    DesktopPath = objShell.SpecialFolders("Desktop")
    Set objShortCut = objShell.CreateShortcut(DesktopPath & "\" & Name & ".lnk")
    objShortCut.TargetPath = Dblquote(PathApplication)
    ObjShortCut.IconLocation = "%SystemRoot%\system32\SHELL32.dll,-28"
    objShortCut.Save
End Sub
'**********************************************************************************************
Sub AskQuestion()
    Dim Question,Msg,Title
    Title = "Shutdown the computer"
    Msg = "Are you sure to shutdown the computer now ?"& Vbcr &_
    "If yes, then click [YES] button "& Vbcr &_
    "If not, then click [NO] button"
    Question = MsgBox (Msg,VbYesNo+VbQuestion,Title)
    If Question = VbYes then
        Call Run_Shutdown(30)
    else
        WScript.Quit()
    End if
End Sub
'**********************************************************************************************
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'**********************************************************************************************
Sub Run_Shutdown(N)
    Dim ws,Command,Execution
    Set ws = CreateObject("wscript.Shell")
    Command = "Cmd /c Shutdown -s -t "& N &" -c "& DblQuote("Save your work because your PC will shut down in "& N &" seconds")
    Execution = ws.run(Command,0,True)
End sub
'**********************************************************************************************

EDIT : 04/06/2015

In this example you should replace "e:\Temp\t.bat" by your Path to your batch file and replace "Hackoo" for the name of the shortcut.

Call Shortcut("e:\Temp\t.bat","Hackoo")
'*********************************************************************************
Sub Shortcut(PathApplication,Name)
    Dim objShell,DesktopPath,objShortCut,MyTab
    Set objShell = CreateObject("WScript.Shell")
    MyTab = Split(PathApplication,"\")
    If Name = "" Then
        Name = MyTab(UBound(MyTab))
    End if
    DesktopPath = objShell.SpecialFolders("Desktop")
    Set objShortCut = objShell.CreateShortcut(DesktopPath & "\" & Name & ".lnk")
    objShortCut.TargetPath = Dblquote(PathApplication)
    ObjShortCut.IconLocation = "%SystemRoot%\system32\SHELL32.dll,-25"
    objShortCut.Save
End Sub
'*********************************************************************************
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'*********************************************************************************
1
votes

Try this code and tell me if it works or not for you ?

@echo off
mode con cols=87 lines=5 & color 9B
Title Generer le vbscript pour la creation du raccourci sur le bureau by Hackoo
echo Generer le vbscript pour la creation du raccourci
Timeout /T 2 > Nul
(
echo Call Shortcut("%windir%\system32\calc.exe","Calculatrice"^)
echo ^'**********************************************************************************************^)
echo Sub Shortcut(CheminApplication,Nom^)
echo    Dim objShell,DesktopPath,objShortCut,MyTab
echo    Set objShell = CreateObject("WScript.Shell"^)
echo    MyTab = Split^(CheminApplication,"\"^)
echo    If Nom = "" Then
echo    Nom = MyTab(UBound^(MyTab^)^)
echo    End if
echo    DesktopPath = objShell.SpecialFolders("Desktop"^)
echo    Set objShortCut = objShell.CreateShortcut(DesktopPath ^& "\" ^& Nom ^& ".lnk"^)
echo    objShortCut.TargetPath = Dblquote^(CheminApplication^)
echo    ObjShortCut.IconLocation = "Winver.exe,0"
echo    objShortCut.Save
echo End Sub
echo ^'**********************************************************************************************
echo ^'Fonction pour ajouter les doubles quotes dans une variable
echo Function DblQuote(Str^)
echo    DblQuote = Chr(34^) ^& Str ^& Chr^(34^)
echo End Function
echo ^'**********************************************************************************************
) > Shortcutme.vbs
echo Execution du vbscript pour la creation du raccourci de la calculatrice sur le bureau
Start /Wait Shortcutme.vbs
echo Suppression du Vbscript
Del Shortcutme.vbs
echo Termine
Pause > Nul