3

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?

Joseph Stine
  • 1,022
  • 1
  • 12
  • 23
user1411190
  • 31
  • 1
  • 1
  • 2

1 Answers1

1

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
Kevin
  • 8,353
  • 3
  • 37
  • 33
  • 1
    Thank you for the feedback. I was able to utilize what you suggested, and consolidate my original script file. I certainly appreciate your help! – user1411190 May 25 '12 at 19:31
  • That's perfect for Win Vista and above, but what about WinXP? How does one loop thru userprofiles then? – crosenblum Jun 03 '13 at 07:49
  • @crosenblum, just replace "C:\Users" with "C:\Documents and Settings" or whatever the localized user profile location is on the machine in question. – Kevin Jun 06 '13 at 21:11