1

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.

6
  • Does the Desktop directory exist within Downloads? You might have meant echo sLinkFile = "%userprofile%\Desktop\ASKontrolpanel.lnk" >> %SCRIPT% Commented Jun 2, 2015 at 19:11
  • Doesn't work either - have tried that already (and now again). It seems to be a problem with my computer. <- Hopefully fixable Commented Jun 2, 2015 at 19:17
  • Salt this PowerShell one-liner to taste and see whether it gives you the same behavior. Commented Jun 2, 2015 at 19:20
  • I am a noob at VBS... So exactly where do I need to put it, and what do I need to edit? Commented Jun 2, 2015 at 19:30
  • 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()" Commented Jun 2, 2015 at 19:37

2 Answers 2

2

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
'*********************************************************************************
Sign up to request clarification or add additional context in comments.

1 Comment

That is not really what I am looking for... Your first answer up there ^ was great! I Just need to know where to put other destination paths than Desktop and I am all set to mark it answered.
1

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

4 Comments

That works... Suprisingly actually.... But still, my code should work perfectly fine - just doesn't on my pc. This does though... which is dang annoying lol. Any clue why yours work, and not mine?
Did you mean this code dosen't work for you only on your Windows 8 installed on your PC and works for another OS in differents machines or something else ?
This code, your code works perfectly fine for me on my computer (Windows 8), but the attached code in the main question up top ^ doesn't work on this PC, but seems to work on all others I have tried it on. Yours work though, but I have no clue what of it I can take out and make it my paths.... I want a simple little vbs code making a shortcut of xxxx to the xxxx destination. Though, I would really like to know why the code I have attached in the question ^ doesn't work on this pc... :/
Where do I put in the desitation path for the .lnk to be put..?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.