13

I'm trying to move a .vbs file with a .movefile line, and I want to give the .vbs file to my friends, but in order for it to work I would have to know what their usernames are.

What would I use to make the .vbs file know their usernames. Example: "C:\users\username\desktop"

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
zDead
  • 159
  • 2
  • 2
  • 4

4 Answers4

35

To get the username of the person currently logged in:

strUser = CreateObject("WScript.Network").UserName
Bond
  • 16,071
  • 6
  • 30
  • 53
5

In VBScript you can get the path to the current user's desktop folder via the SpecialFolders collection:

WScript.Echo CreateObject("WScript.Shell").SpecialFolders("Desktop")
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
4

Use:

Set wshShell = CreateObject("WScript.Shell")

strName = wshShell.ExpandEnvironmentStrings("%USERNAME%")

That would store the username in the string "strName', so it would work like this:

x = messagebox("Hello ") + strName, 1, strName)

Or in your case:

x = messagebox("C:\Users\" + strName + "\Desktop\name", 1, "User name in C:\ directory below"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    Is this a troll answer? And/or generated by a bot? Not actually tested? The result is likely syntax errors (can somebody confirm? Or otherwise). E.g., the parentheses are not balanced at all. – Peter Mortensen Oct 12 '22 at 22:57
0

Copy and paste this code:

Set wshShell = CreateObject( "WScript.Shell" )
User = wshShell.ExpandEnvironmentStrings( "%USERNAME%" )
WScript.Echo "User: " & strUserName
Gladius125
  • 17
  • 3
  • 1
    Is it not covered by [this answer](https://stackoverflow.com/a/43198013/692942), other than `WScript.Echo` to output the variable what extra value does this offer? You could have just as easily amended the existing answer to include this extra line. – user692942 Sep 29 '20 at 16:14
  • [Another answer](https://stackoverflow.com/questions/13620748/msgbox-vs-msgbox-in-vbscript/59669761#59669761) was completely bogus, so the confidence is not high. Though the referenced answer to this question could also be bogus (more or less random code that does not compile?). – Peter Mortensen Oct 12 '22 at 23:03