Use of environment variable for username in a shortcut (Win 10-1909)

Page 3 of 4 FirstFirst 1234 LastLast

  1. Posts : 7,606
    Windows 10 Home 20H2
       #21

    What is your application?
      My Computer


  2. Posts : 110
    Windows 10 Pro 64
    Thread Starter
       #22

    Matthew Wai said:
    What is your application?
    Tradestation (Easylanguage)
      My Computer


  3. Posts : 7,606
    Windows 10 Home 20H2
       #23

    Your application does not recognize environment variables.
      My Computer


  4. Posts : 17,013
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #24

    Atom1 said:
    Sorry, I was not clear about where the shortcut-username problem occurs.
    This is from my application:
    string BatFileShtCut("C:\Users\Stan\Desktop\PosnPushAlert.lnk");
    I am here defining the path for the shortcut file. If I replace "Stan" with %username%, it does not work.
    Are you saying that a shortcut you make manually does work?

    Denis
      My Computer


  5. Posts : 110
    Windows 10 Pro 64
    Thread Starter
       #25

    Matthew Wai said:
    Your application does not recognize environment variables.
    It is able to put environment variables in path for other scripts, and they work. It only doesn't work when I try to use an environment variable in place of username, when I'm defining a path for a shortcut.
    var: //string BatFileShtCut("C:\Users\%username%\Desktop\PosnPushAlert.lnk"); string BatFileShtCut("C:\Users\Stan\Desktop\PosnPushAlert.lnk");
    The above will not work if I use the version with the environment variable in place of the actual user name.

    When I run this from EasyLanguage:
    //TEST
    Code:
    method void OnButtonClick2(Object sender, EventArgs args ) begin		BatFile1 = FilePath+"PosnPushAlert"+".bat";		FileDelete(BatFile1);		Bat1Text = "Powershell.exe -executionpolicy remotesigned -File C:\Users\%UserName%\Desktop\PushOverPS.ps1 ";		Bat1Text += doublequote+DTS()+" This is a test. "+doublequote;		FileWrite(BatFile1,Bat1Text);		if Elsystem.io.File.Exists(BatFile1) then environment.start(BatFileShtCut);		Textbox1.Text = "OK";	end;
    This works where I'm writing %username% to a .bat file and then running the .bat file, but if I try to invoke the shortcut using an environment variable in the path instead of the username, it does not work, so you are right!!!

    Thanks.

    - - - Updated - - -

    I apologize for sending everyone on a wild goose chase here. I now see what was happening. End of story.
    Thanks to all.
      My Computer


  6. Posts : 72
    Win 10 Pro 19043.1237 (X64) (21H1)
       #26

    Atom1 said:
    I now see what was happening. End of story
    Oh heck no.....curious followers want to know
      My Computer


  7. Posts : 110
    Windows 10 Pro 64
    Thread Starter
       #27

    So, just a p.s. on this. I just moved the shortcut to a path that does not have username in it and... problem solved!!!
      My Computer


  8. Posts : 7,606
    Windows 10 Home 20H2
       #28

    What a simple solution!
      My Computer


  9. Posts : 1,201
    11 Home
       #29

    BubbaW said:
    Oh heck no.....curious followers want to know
      My Computers


  10. Posts : 989
    Microsoft Windows 10 Home
       #30

    If you create a shortcut with the Create Shortcut wizard, any environmental variables in the Target path will be evaluated when the shortcut is created, leaving you with a static Target.

    To create a shortcut with environmental variables that are evaluated when the shortcut is launched, you have to create the shortcut programatically.

    PowerShell:

    Code:
    $wshell        = New-Object -ComObject wscript.shell
    $sc            = $wshell.CreateShortcut("c:\Users\Keith\mytest.lnk")
    $sc.targetPath = '%USERPROFILE%\Sandbox'
    $sc.save()
    Properties of MyTest.lnk after creation:
    Use of environment variable for username in a shortcut  (Win 10-1909)-screenshot-924-.png
    Last edited by KeithM; 08 Mar 2021 at 14:51.
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd
All times are GMT -5. The time now is 16:54.
Find Us




Windows 10 Forums