Need a batch file to copy a file from a network share based on a list of usernames

Here's the scenario:

I have a list of users in a text file with 5 digit user ID's, for example


On a network share, I have a list of these corresponding users, and they each have a file, with a .ini extension... for example


I need a batch script that can copy down the correct ini file depending on which user is currently logged into the machine.  So, if user abc12 logs in, the abc12.ini file gets copied to c:\temp.  I plan on putting this script in startup, so the appropriate file gets copied down to the specific user.
Who is Participating?
Steve KnightConnect With a Mentor IT ConsultancyCommented:
To add to that slightly if you want to do the copy only if it is one of the users listed then you could do:

@echo off
find /i "%username%" youruserlist.txt >NUL 2>&1 && xcopy /d "\\server\share\%username%.ini" "C:\destination"

The find command looks for the current username in the userlist file.  If it finds it then it runs the command after the && (this means run if the previous command succeeded).  The /i says case insensitive, the >NUL and >2&1 hide any output or errors it shows from the screen.  

The xcopy uses /d to only copy the file if it is newer - that might be appropriate or not for you?

effectively the same as what has been said, and the proviso of being a logon script, or on the startup group for that user on the computer itself as otherwise "%username%" is unknown.

Basically, it comes down to something like this:
copy \\server\share\%username%.ini C:\temp

Open in new window

But this won't work in a startup script - there's no user logged on when a startup script runs.
When you run this in a logon script, you need to make sure that the user has write access to the target location, or you pick a folder in the user's profile, for example the user's temp folder:
copy "\\server\share\%username%.ini" "%Temp%"

Open in new window


just to make sure.
you have a shared folder with ini files (user1.ini, user2.ini, etc) and you want to copy the correct ini file corresponding with the logged on user to the c:\temp folder.

if so, all you need is one command in the logon script:
copy \\SHAREDFOLDER\%USERNAME%.ini c:\temp
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.