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.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Albert MN.
  • 713
  • 1
  • 16
  • 33
  • Does the Desktop directory exist within Downloads? You might have meant `echo sLinkFile = "%userprofile%\Desktop\ASKontrolpanel.lnk" >> %SCRIPT%` – rojo Jun 02 '15 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 – Albert MN. Jun 02 '15 at 19:17
  • [Salt this PowerShell one-liner to taste](http://stackoverflow.com/a/30029955/1683264) and see whether it gives you the same behavior. – rojo Jun 02 '15 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? – Albert MN. Jun 02 '15 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()"` – rojo Jun 02 '15 at 19:37
  • I honestly do not think my problem can be solved with more code... as mine is perfectly fine, and works on every pc - but mine for some reason. I am kind of looking for a setting or something... – Albert MN. Jun 02 '15 at 19:54

2 Answers2

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
'*********************************************************************************
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • 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. – Albert MN. Jun 04 '15 at 12:24
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
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • 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? – Albert MN. Jun 03 '15 at 09:00
  • 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 ? – Hackoo Jun 03 '15 at 11:19
  • 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... :/ – Albert MN. Jun 03 '15 at 11:21
  • Where do I put in the desitation path for the .lnk to be put..? – Albert MN. Jun 03 '15 at 14:22