Search code examples
batch-filedos

How to create a setting that changes the batch copy command based on what your username is?


I have a very simple batch command that goes like this:

xcopy "T:\UserPreferences.xml" "C:\Users\USERNAME\AppData\UserPreferences.xml" /y

So it is pulling a file from the mapped network T drive to the local Users folder. However, because the username for each person is different, it means people on my network cannot run the command without adjusting the command to fit their local machine.

Is there a way to create a variable so that the local folder location is adjusted based on the user who is running it?


Solution

  • You shouldn't be using C:\Users at all.

    Copy the file to "%APPDATA%\UserPreferences.xml" instead. The environmental variable APPDATA points to the logged in user's AppData folder, which may not always be located on drive C:. For instance, when roaming profiles are enabled, the APPDATA folder is located on an administrator-specified network drive where it's available from other systems on the network/domain.