How do I make a batch file be able to run on other computers

9

1

When I create a batch file on my computer to execute start c:\Users\________\Whatever\..., it works on my machine.

How can I get it to work on someone else's machine, since their username isn't my username?

konrad

Posted 2014-06-04T17:30:13.073

Reputation: 111

Answers

19

You can use the Environmental Variable %USERPROFILE%. This works from Windows XP and on, and automatically detects the drive letter, and the folder path, to the current user's profile folder.

start %USERPROFILE%\Whatever

Canadian Luke

Posted 2014-06-04T17:30:13.073

Reputation: 22 162

9

You can use the Windows environment variables. One you could use in this case would be %username%, which returns the current user's name.

ie:

start c:\users\%username%\Whatever

Additionally/alternatively, if you're specifically aiming for the user's profile folder (<drive>:\users\<username>) you can use the %userprofile% variable, which will return the full path to the current user's profile, which helps when targeting older OSs like XP (where it's <drive>:\document and settings\<username> by default), or where the profile folder has been moved to a different drive/location.

ie:

start %userprofile%\Whatever

There's several other variables in there that you may find useful in writing batches, I'd advise perusing the entire list. :)

Ƭᴇcʜιᴇ007

Posted 2014-06-04T17:30:13.073

Reputation: 103 763

2I'd upvote if it wasn't for my last two Windows systems having kept the user profiles in e:\users and d:\documents and settings... – user1686 – 2014-06-04T19:10:10.147

@grawity I tried to keep it generic by showing him the list of available variables, and not assuming they always want to target the user profile directory. But hey point taken, so I updated it to cover the %userprofile% variable as well. – Ƭᴇcʜιᴇ007 – 2014-06-04T19:21:49.580

Dude, you just stole my answer :P – Canadian Luke – 2014-06-04T19:21:52.990

Not stolen, just expanded on my answer to make it more full. Again, I tried to avoid making it specifically about the user profile directory originally, but apparently if I don't cover %userprofile% as well, then it's not a 'correct' answer. ;) – Ƭᴇcʜιᴇ007 – 2014-06-04T19:23:21.403

Don't worry, with the tongue sticking out in the comment, it's meant as a joke :) – Canadian Luke – 2014-06-04T19:25:46.603