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

Posted on 2011-05-03
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 82

    Expert Comment

    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

    LVL 3

    Expert Comment


    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

    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

    Are your corporate email signatures appalling?

    Is it scary how unprofessional your email signatures look? Do users create their own terrible designs and give themselves stupid job titles? You can make this a lot easier for yourself by choosing an email signature management solution from Exclaimer today.

    Join & Write a Comment

    Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
    If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
    The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now