making shortcut with .vbs compared to .cmd - %USERPROFILE% help needed

Discussion in 'Scripting' started by tnx, Jan 27, 2016.

  1. tnx

    tnx MDL Expert

    Sep 2, 2008
    1,695
    267
    60
    #1 tnx, Jan 27, 2016
    Last edited by a moderator: Apr 20, 2017
    Hi all.

    I have been messing about with a couple of scripts to make a shortcut on my desktop.

    I have been using xxmklink.exe and this works pretty well. Though I would like to learn how to do it via .vbs.

    I have this .cmd
    Code:
    @echo off
    %system% xxmklink.exe %USERPROFILE%\Desktop\Documents.lnk %USERPROFILE%\Documents > nul
    
    Using the %USERPROFILE% allows me to add this into my $OEM$ and run via runonce

    Works a treat.

    I know how to make a .vbs to make a shortcut with this script

    Code:
    Option Explicit
    Dim obj, nLink
    
    Set obj = CreateObject("wscript.shell")
    
    Set nLink = obj.CreateShortcut("C:\Users\Malc's\Desktop\Documents.lnk")
    
    nLink.TargetPath = "C:\Users\Malc's\Documents"
    nLink.Description = "My Documents"
    nLink.IconLocation = "C:\windows\MyIcons.dll,6"
    nLink.Save
    As you can see I have to write the path using the systems name. Trouble is I don't always install onto a PC called "Malc's"

    I also have this script
    Code:
    Set objShell = CreateObject("Wscript.Shell")
    strProfile = objShell.ExpandEnvironmentStrings("%userprofile%")
    Wscript.Echo strProfile 
    which displays the current user.

    What I would like to do is make my .vbs script use %userprofile% instead of an actual name. Know what I mean ?

    Spent the last hour or so getting very frustrated trying to learn how to write a script. kept getting errors. :wallbash:

    so

    :flag:

    Please help.

    :wiggle:
     
  2. LiteOS

    LiteOS Windowizer

    Mar 7, 2014
    2,204
    978
    90
    Can i change exist lnk file using this ?
     
  3. wk-952

    wk-952 MDL Member

    Sep 2, 2012
    116
    287
    10
    #3 wk-952, Jan 27, 2016
    Last edited by a moderator: Apr 20, 2017
    @tnx

    Hope this is what you're looking for:
    Code:
    Option Explicit
    Dim objShell, strProfile, strWinDir, nLink
    
    Set objShell = CreateObject("Wscript.Shell")
    
    strProfile = objShell.ExpandEnvironmentStrings("%userprofile%")
    strWinDir = objShell.ExpandEnvironmentStrings("%windir%")
    
    Set nLink = objShell.CreateShortcut(strProfile & "\Desktop\Documents.lnk")
    
    nLink.TargetPath = strProfile & "\Documents"
    nLink.Description = "My Documents"
    nLink.IconLocation = strWinDir & "\MyIcons.dll,6"
    nLink.Save
    
    
    Your whole problem was just string concatenation i guess, which is done using the ampersand char '&'.
    Tell me the results.

    EDIT: also here's a detailed list of environment variables:
    http://ss64.com/nt/syntax-variables.html
     
  4. Flipp3r

    Flipp3r MDL Expert

    Feb 11, 2009
    1,964
    907
    60
    #4 Flipp3r, Jan 27, 2016
    Last edited by a moderator: Apr 20, 2017
    I think you need to retrieve it like so:
    Code:
    usrProfile = WshS.ExpandEnvironmentStrings("%UserProfile%")
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  5. tnx

    tnx MDL Expert

    Sep 2, 2008
    1,695
    267
    60
    Just got in from work, coffee time then sleep.

    Thanks for the replies guys, can't wait to test this out later.
    Will report back later on..

    :tea:
     
  6. tnx

    tnx MDL Expert

    Sep 2, 2008
    1,695
    267
    60
    #6 tnx, Jan 28, 2016
    Last edited by a moderator: Apr 20, 2017
    (OP)

    That is brilliant. Worked a treat...

    :spoton:

    :cheers:
     
  7. tnx

    tnx MDL Expert

    Sep 2, 2008
    1,695
    267
    60
    #7 tnx, Jan 28, 2016
    Last edited by a moderator: Apr 20, 2017
    (OP)
    That .vbs is beautifull.

    Amended it to create two desktop shortcuts.

    Code:
    Option Explicit
    Dim objShell, strProfile, strWinDir, nLink , nLink2
    
    Set objShell = CreateObject("Wscript.Shell")
    
    strProfile = objShell.ExpandEnvironmentStrings("%userprofile%")
    strWinDir = objShell.ExpandEnvironmentStrings("%windir%")
    
    Set nLink = objShell.CreateShortcut(strProfile & "\Desktop\Documents.lnk")
    Set nLink2 = objShell.CreateShortcut(strProfile & "\Desktop\Pictures.lnk")
    
    nLink.TargetPath = strProfile & "\Documents"
    nLink.workingdirectory = strProfile & "\Documents"
    nLink.Description = "My Documents"
    nLink.WindowStyle = 3
    nLink.IconLocation = strWinDir & "\MyIcons.dll,1"
    nLink.Save
    
    nLink2.TargetPath = strProfile & "\Pictures"
    nLink2.workingdirectory = strProfile & "\Pictures"
    nLink2.Description = "My Pictures"
    nLink2.WindowStyle = 3
    nLink.IconLocation = strWinDir & "\MyIcons.dll,2"
    nLink2.Save
    Worked really well.

    So I guess expanding it to create as many shortcuts as I wish is possible.

    @wk-952
    :worthy:
     
  8. tnx

    tnx MDL Expert

    Sep 2, 2008
    1,695
    267
    60
    #8 tnx, Jan 28, 2016
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Thought I would edit it a little further, give the nLink a bit more meaning.

    Code:
    Option Explicit
    Dim objShell, strProfile, strWinDir, Desktop , Pictures
    
    Set objShell = CreateObject("Wscript.Shell")
    
    strProfile = objShell.ExpandEnvironmentStrings("%userprofile%")
    strWinDir = objShell.ExpandEnvironmentStrings("%windir%")
    
    Set Desktop = objShell.CreateShortcut(strProfile & "\Desktop\Documents.lnk")
    Set Pictures = objShell.CreateShortcut(strProfile & "\Desktop\Pictures.lnk")
    
    Desktop.TargetPath = strProfile & "\Documents"
    Desktop.workingdirectory = strProfile & "\Documents"
    Desktop.Description = "My Documents"
    Desktop.WindowStyle = 3
    Desktop.IconLocation = strWinDir & "\MyIcons.dll,1"
    Desktop.Save
    
    Pictures.TargetPath = strProfile & "\Pictures"
    Pictures.workingdirectory = strProfile & "\Pictures"
    Pictures.Description = "My Pictures"
    Pictures.WindowStyle = 3
    Desktop.IconLocation = strWinDir & "\MyIcons.dll,2"
    Pictures.Save
    works really well.

    Just gunna add this to my OOBE script via runonce and test the results.
     
  9. tnx

    tnx MDL Expert

    Sep 2, 2008
    1,695
    267
    60
    #9 tnx, Jan 28, 2016
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Just tested it and it worked quite well.
    The shortcuts did appear on my desktop at first boot but I made a small error and the pictures shortcut icon was not right.

    :thinking:

    Code:
    Option Explicit
    Dim objShell, strProfile, strWinDir, Desktop , Pictures
    
    Set objShell = CreateObject("Wscript.Shell")
    
    strProfile = objShell.ExpandEnvironmentStrings("%userprofile%")
    strWinDir = objShell.ExpandEnvironmentStrings("%windir%")
    
    Set Desktop = objShell.CreateShortcut(strProfile & "\Desktop\Documents.lnk")
    Set Pictures = objShell.CreateShortcut(strProfile & "\Desktop\Pictures.lnk")
    
    Desktop.TargetPath = strProfile & "\Documents"
    Desktop.workingdirectory = strProfile & "\Documents"
    Desktop.Description = "My Documents"
    Desktop.WindowStyle = 3
    Desktop.IconLocation = strWinDir & "\MyIcons.dll,1"
    Desktop.Save
    
    Pictures.TargetPath = strProfile & "\Pictures"
    Pictures.workingdirectory = strProfile & "\Pictures"
    Pictures.Description = "My Pictures"
    Pictures.WindowStyle = 3
    Desktop.IconLocation = strWinDir & "\MyIcons.dll,2"
    Pictures.Save
    tut tut tut... :doh:
     
  10. tnx

    tnx MDL Expert

    Sep 2, 2008
    1,695
    267
    60
    @wk-952

    I am going to have a go at making a folder via .vbs using "%userprofile%. If I get stuck and I probably will. Can you help me out please ?

    :eek:
     
  11. wk-952

    wk-952 MDL Member

    Sep 2, 2012
    116
    287
    10
    Well i'm a complete beginner in VB scripts, i'm honestly only interested in batch,
    so you should probably ask someone of more knowledge than me.

    But if it's something i know, i will surely help you immediately, but again don't expect much from me in VB scripts.
    My apologies if i disappointed you.
     
  12. tnx

    tnx MDL Expert

    Sep 2, 2008
    1,695
    267
    60
    #12 tnx, Jan 28, 2016
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Took me a while but I got it...:clap:

    Rather pleased with myself. :icecream:

    Code:
    Option Explicit
    Dim objShell, fso, APPsFolder 
    
    Set objShell = WScript.CreateObject("Wscript.Shell")
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    APPsFolder = objShell.ExpandEnvironmentStrings("%userprofile%") 
    
    fso.CreateFolder(APPsFolder & "\APPs")
    
     
  13. tnx

    tnx MDL Expert

    Sep 2, 2008
    1,695
    267
    60
    #13 tnx, Jan 28, 2016
    Last edited by a moderator: Apr 20, 2017
    (OP)
    Editing a bit further to make my desired folder and create a shortcut for that folder in "SendTo"

    Works like a charm..

    Code:
    Option Explicit
    Dim objShell, fso, APPsFolder, APPsShortcut, MyIconDLLFile
    
    Set objShell = WScript.CreateObject("Wscript.Shell")
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    APPsFolder = objShell.ExpandEnvironmentStrings("%userprofile%") 
    MyIconDLLFile = objShell.ExpandEnvironmentStrings("%SystemRoot%")
    
    fso.CreateFolder(APPsFolder & "\APPs")
    
    Set APPsShortcut = objShell.CreateShortcut(APPsFolder & "\AppData\Roaming\Microsoft\Windows\SendTo\APPs.lnk")
    
    APPsShortcut.workingdirectory = APPsFolder & "\APPs"
    APPsShortcut.TargetPath = APPsFolder & "\APPs"
    APPsShortcut.Description = "APPs Folder Shortcut"
    APPsShortcut.WindowStyle = 3
    APPsShortcut.IconLocation = MyIconDLLFile & "\MyIcons.dll,6"
    APPsShortcut.Save
    
    :cool: