Batch file to copy from all user profiles

10,105

You could use reg query to get the list of user profiles from the registry, but you only care about users who have a folder under C:\Users, so just loop over those:

for /d %%u in (C:\Users\*) do for %%x in (doc xls) do xcopy C:\Users\%%~nu\*.%%x "\\server\i$\User Backups\%%~nu\%computername%\" /c /i /y /s /d
Share:
10,105
Admin
Author by

Admin

Updated on August 24, 2022

Comments

  • Admin
    Admin almost 2 years

    I want to generate a batch file that will copy all *.doc and *.xls file types from a user's profile. I will then set the batch file to run automatically via Scheduled Tasks after-hours when all users are logged out. Here is what I have:

    for %%x in (doc xls) do xcopy c:\Users\user1\*.%%x "\\server\i$\User Backups\user1\%computername%\" /c /i /y /s /d
    

    This works fine, however, I need to generate a line item in the batch file for each and every user in our organization (user1, user2, etc.) so that I hit all profiles. When new users are hired, the file needs to be updated with their profile info. Ideally, I'd like something a bit more automated that is similar to this:

    for %%x in (doc xls) do xcopy %userprofile%\*.%%x "\\server\i$\User Backups\%username%\%computername%" /c /i /y /s /d 
    

    The downfall is that by using %userprofile% in place of the 'user1' input, it only runs against the currently logged in user. Is there another option I could incorporate that wouldn't care about the currently logged in user, and instead just run against all user profiles on a machine?

  • Admin
    Admin about 12 years
    Thank you for the feedback. I was able to utilize what you suggested, and consolidate my original script file. I certainly appreciate your help!
  • crosenblum
    crosenblum almost 11 years
    That's perfect for Win Vista and above, but what about WinXP? How does one loop thru userprofiles then?
  • Kevin
    Kevin almost 11 years
    @crosenblum, just replace "C:\Users" with "C:\Documents and Settings" or whatever the localized user profile location is on the machine in question.