0

I have a bat file to run some Python code similar to this:

@echo off
set /P State=                         Select State:
set /P Year=                          Select Year:

"C:\Users\MYNUMBER\OneDrive - Company\filepath\python.exe" "C:\Users\MYNUMBER\OneDrive - Company\filepath\tst.py" %State% %Year% 
pause

I want different people at my organization to be able to run this file without any issue, but currently the path specifies my User number (called MYNUMBER).

What is the best way to make this bat file runnable for anyone in the company?

8
  • 1
    Can't you use %USERNAME%? Commented Jun 7 at 4:46
  • How exactly would I include this in the string though?
    – Jwem93
    Commented Jun 7 at 5:00
  • Your "user number" is your username (that it's a number is incidental). Commented Jun 7 at 5:03
  • 1
    In your script, replace MYNUMBER with %USERNAME%
    – Berend
    Commented Jun 7 at 6:05
  • 1
    Just to make you aware, this was also posted over on StackOverflow. Jwem93, please do not do that, this site was a better choice IMO.
    – Compo
    Commented Jun 7 at 15:48

1 Answer 1

1

There are several environment variables that you can use. Pick the one that works for you. Of course, every user should have python.exe in their OneDrive folder.

"C:\Users\%USERNAME%\OneDrive - Company\filepath\python.exe"

"%USERPROFILE%\OneDrive - Company\filepath\python.exe"

"%ONEDRIVE%\filepath\python.exe"

2
  • Do not assume that the user's profile directory will always be C:\Users\%USERNAME%. Neither on personal systems (it won't work after account rename, it won't work for many people logging in with a MS account) nor on corporate systems (various mess like username.domain will happen; and again, accounts get renamed and shuffled around). That's why %USERPROFILE% exists. Commented Jun 7 at 6:44
  • 2
    @grawity_u1686 That's exactly why I gave more options. %ONEDRIVE% also does not work in all cases
    – Berend
    Commented Jun 7 at 6:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .