[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2011-05-03
Medium Priority
Last Modified: 2012-05-11
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.
Question by:windows7expert
LVL 85

Expert Comment

ID: 35513685
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


Expert Comment

ID: 35513686

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
LVL 43

Accepted Solution

Steve Knight earned 2000 total points
ID: 35513793
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.


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When asking a question in a forum or creating documentation, screenshots are vital tools that can convey a lot more information and save you and your reader a lot of time
IF you are either unfamiliar with rootkits, or want to know more about them, read on ....
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

868 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question