• 0

batch to copy profiles


Question

I am currently replacing 30 systems at work, and each system had multiple users. So what I would like to do to their old systems is copy their profile consisting of ONLY My Docs, Favorites, and Desktop into another directory on the drive with their profilename as the directory.. for example, say I have the following users:

c:\documents and settings\Smith

c:\documents and settings\Jones

c:\documents and settings\Clark

I would like the batch file to traverse documents and settings to find the profile directories and copy them to:

c:\ProfileBackup\Smith\[favorites/mydocs/desktop]\

c:\ProfileBackup\Jones\[favorites/mydocs/desktop]\

c:\ProfileBackup\Clark\[favorites/mydocs/desktop]\

etc...

can this be done, and any clue how?

thanks!

Link to comment
https://www.neowin.net/forum/topic/514219-batch-to-copy-profiles/
Share on other sites

16 answers to this question

Recommended Posts

  • 0

I whipped this up in a few seconds but the drawback is that you have to make sure the user is logged in which you would like to copy the profile for and try this:

So if you're logged in as Mary, it will only copy Mary's profile. So if you have 4 profiles on a machine, you'll have to log in with each profile and run this script. Also, this is assuming you're using XP.

@ECHO OFF
MD C:\PROFILES
XCOPY "%USERPROFILE%\MY DOCUMENTS\*.*" "C:\PROFILES\%USERNAME%\My Documents\"
XCOPY "%USERPROFILE%\FAVORITES\*.*" C:\PROFILES\%USERNAME%\Favorites\
XCOPY "%USERPROFILE%\DESKTOP\*.*" C:\PROFILES\%USERNAME%\Desktop\
EXIT

  • 0

Well, the only thing I could find is if you create temporary variables for the users profiles:

So on the machine with 15 profiles you'd have to create 15 user variables and then expand the batch file accordingly. That's as knowledgeable I get with batch files.

Like this:

@ECHO OFF
SET USER1="USER NAME OF USER"
SET USER2="USER NAME OF USER 2"
SET USER3=
SET USER4=
MD C:\PROFILES

ECHO COPYING %USER1%'S PROFILE
XCOPY /Q "C:\DOCUMENTS AND SETTINGS\%USER1%\MY DOCUMENTS\*.*" "C:\PROFILES\%USER1%\My Documents\"
XCOPY /Q "C:\DOCUMENTS AND SETTINGS\%USER1%\FAVORITES\*.*" C:\PROFILES\%USER1%\Favorites\
XCOPY /Q "C:\DOCUMENTS AND SETTINGS\%USER1%\DESKTOP\*.*" C:\PROFILES\%USER1%\Desktop\

ECHO COPYING %USER2%'S PROFILE
XCOPY /Q "C:\DOCUMENTS AND SETTINGS\%USER2%\MY DOCUMENTS\*.*" "C:\PROFILES\%USER2%\My Documents\"
XCOPY /Q "C:\DOCUMENTS AND SETTINGS\%USER2%\FAVORITES\*.*" C:\PROFILES\%USER2%\Favorites\
XCOPY /Q "C:\DOCUMENTS AND SETTINGS\%USER2%\DESKTOP\*.*" C:\PROFILES\%USER2%\Desktop\

ECHO COPYING %USER3%'S PROFILE
XCOPY /Q "C:\DOCUMENTS AND SETTINGS\%USER3%\MY DOCUMENTS\*.*" "C:\PROFILES\%USER3%\My Documents\"
XCOPY /Q "C:\DOCUMENTS AND SETTINGS\%USER3%\FAVORITES\*.*" C:\PROFILES\%USER3%\Favorites\
XCOPY /Q "C:\DOCUMENTS AND SETTINGS\%USER3%\DESKTOP\*.*" C:\PROFILES\%USER3%\Desktop\

ECHO COPYING %USER4%'S PROFILE
XCOPY /Q "C:\DOCUMENTS AND SETTINGS\%USER4%\MY DOCUMENTS\*.*" "C:\PROFILES\%USER4%\My Documents\"
XCOPY /Q "C:\DOCUMENTS AND SETTINGS\%USER4%\FAVORITES\*.*" C:\PROFILES\%USER4%\Favorites\
XCOPY /Q "C:\DOCUMENTS AND SETTINGS\%USER4%\DESKTOP\*.*" C:\PROFILES\%USER4%\Desktop\
EXIT

  • 0

Here is another alternative way of doing what you wanted to do.

Try this VBS script it will copy what you want to the new folder.

Save As CopyUserFolders.vbs

Notes

Replace this strComputer = "." with strComputer = "SOME_COMPUTER_NAME" will in thoery

connect to a machine on the network. I can not test this part as I have no network.

strComputer = "."
On Error Resume Next 
 Dim F1, F2, colAccounts, objUser, StrUser
 Dim Act	 : Set Act = CreateObject("Wscript.Shell")
 Dim Fso	 : Set Fso = CreateObject("Scripting.FileSystemObject")
 Dim DocSet  : DocSet = Act.ExpandEnvironmentStrings("%SystemDrive%\Documents and Settings\")
 Dim DTop	: DTop = "\Desktop" 
 Dim Fav	 : Fav = "\Favorites"
 Dim MyDoc   : MyDoc = "\My Documents"
 Dim Profile : Profile = Act.ExpandEnvironmentStrings("%SystemDrive%\ProfileBackup\")
'/-> Create The folder If It Does Not Exists
  If Not Fso.FolderExists(Profile) Then Fso.CreateFolder(Profile) End If
  Set colAccounts = GetObject("WinNT://" & strComputer & "")
  colAccounts.Filter = Array("user")
'/-> Loop To Go Threw The User Accounts
   For Each objUser In colAccounts
   If Fso.FolderExists(DocSet & objUser.Name) Then 
   F1 = Profile & objUser.Name
'/-> Create The folder If It Does Not Exists
   If Not Fso.FolderExists(F1) Then Fso.CreateFolder(F1) End If 
   F2 = F2 & vbCrLf & objUser.Name 
'/-> Popup Messagebox 
   Act.Popup "Preparing To Copy this User : " & objUser.Name & vbCrLf &_
   "These Folder :" & DTop & ", " & Fav & ", " & MyDoc ,5,"Copy User Folders",4128
'/-> User Desktop
	StrUser = DocSet & objUser.Name & DTop
	Fso.CopyFolder(StrUser),(F1 & DTop)
'/-> User Favorite
	StrUser = DocSet & objUser.Name & Fav
	Fso.CopyFolder(StrUser),(F1 & Fav)
'/-> User My Documents
	StrUser = DocSet & objUser.Name & MyDoc
	Fso.CopyFolder(StrUser),(F1 & MyDoc)
	End If  
   Next
 Act.Popup "Completed Copying These Profiles" & vbCrLf & F2, 5, "Copy Users Folders", 4128
  • 0

Here is another alternative way of doing what you wanted to do.

Try this VBS script it will copy what you want to the new folder.

Save As CopyUserFolders.vbs

Notes

Replace this strComputer = "." with strComputer = "SOME_COMPUTER_NAME" will in thoery

connect to a machine on the network. I can not test this part as I have no network.

This is PERFECT! Thank you very much!

  • 0

Jake,

I just ran into a problem.. when I tried it on a windows2000 Laptop, it worked perfect. Now I'm at work and most of the systems are WinXP. What the script does now is copy the first one in the directory (Administrator) and then it doesn't move on to all the others.. any ideas?

Thanks again!

  • 0

Hi all,

You can write the batch version above a little bit shorter.

::--- snipp CopyProfile.bat

@echo off

:NextPlease

If [%1]==[] goto :eof

echo Copying %1's profile

For %%i in ("My Documents", Favorites, Desktop) do Echo Xcopy /q "C:\DOCUMENTS AND SETTINGS\%1\%%~i\*.*" "C:\profiles\%1\%%~i\"

shift & goto nextplease

::--- snapp CopyProfiles.bat

Demo [CMD-Prompt]:

> f:\Administrator\copyProfiles.bat userA MyUncle HisSister Another a b c d e f g h i j k l m

Copying userA's profile

Xcopy /q "C:\DOCUMENTS AND SETTINGS\userA\My Documents\*.*" "C:\profiles\userA\My Documents\"

Xcopy /q "C:\DOCUMENTS AND SETTINGS\userA\Favorites\*.*" "C:\profiles\userA\Favorites\"

Xcopy /q "C:\DOCUMENTS AND SETTINGS\userA\Desktop\*.*" "C:\profiles\userA\Desktop\"

Copying MyUncle's profile

Xcopy /q "C:\DOCUMENTS AND SETTINGS\MyUncle\My Documents\*.*" "C:\profiles\MyUncle\My Documents\"

Xcopy /q "C:\DOCUMENTS AND SETTINGS\MyUncle\Favorites\*.*" "C:\profiles\MyUncle\Favorites\"

Xcopy /q "C:\DOCUMENTS AND SETTINGS\MyUncle\Desktop\*.*" "C:\profiles\MyUncle\Desktop\"

Copying HisSister's profile

Xcopy /q "C:\DOCUMENTS AND SETTINGS\HisSister\My Documents\*.*" "C:\profiles\HisSister\My Documents\

"

Xcopy /q "C:\DOCUMENTS AND SETTINGS\HisSister\Favorites\*.*" "C:\profiles\HisSister\Favorites\"

Xcopy /q "C:\DOCUMENTS AND SETTINGS\HisSister\Desktop\*.*" "C:\profiles\HisSister\Desktop\"

Copying Another's profile

Xcopy /q "C:\DOCUMENTS AND SETTINGS\Another\My Documents\*.*" "C:\profiles\Another\My Documents\"

Xcopy /q "C:\DOCUMENTS AND SETTINGS\Another\Favorites\*.*" "C:\profiles\Another\Favorites\"

Xcopy /q "C:\DOCUMENTS AND SETTINGS\Another\Desktop\*.*" "C:\profiles\Another\Desktop\"

Copying a's profile

Xcopy /q "C:\DOCUMENTS AND SETTINGS\a\My Documents\*.*" "C:\profiles\a\My Documents\"

Xcopy /q "C:\DOCUMENTS AND SETTINGS\a\Favorites\*.*" "C:\profiles\a\Favorites\"

Xcopy /q "C:\DOCUMENTS AND SETTINGS\a\Desktop\*.*" "C:\profiles\a\Desktop\"

Copying b's profile

Xcopy /q "C:\DOCUMENTS AND SETTINGS\b\My Documents\*.*" "C:\profiles\b\My Documents\"

Xcopy /q "C:\DOCUMENTS AND SETTINGS\b\Favorites\*.*" "C:\profiles\b\Favorites\"

Xcopy /q "C:\DOCUMENTS AND SETTINGS\b\Desktop\*.*" "C:\profiles\b\Desktop\"

Copying c's profile

.....

....

Copying m's profile

Xcopy /q "C:\DOCUMENTS AND SETTINGS\m\My Documents\*.*" "C:\profiles\m\My Documents\"

Xcopy /q "C:\DOCUMENTS AND SETTINGS\m\Favorites\*.*" "C:\profiles\m\Favorites\"

Xcopy /q "C:\DOCUMENTS AND SETTINGS\m\Desktop\*.*" "C:\profiles\m\Desktop\"

------------

P.S. The "echo" in front of the XCopy-command is only for demo. Just delete it to rumn the batch in your real envirinment.

@Threadowner:

You have to run this script with "the right rights". Run it with "runas" or "runasSpc" as Administrator.

Same problem occurs 2 posts later with the VBS-snippet.

HTH

Biber

Edited by Biber
  • 0

Jake,

I just ran into a problem.. when I tried it on a windows2000 Laptop, it worked perfect. Now I'm at work and most of the systems are WinXP. What the script does now is copy the first one in the directory (Administrator) and then it doesn't move on to all the others.. any ideas?

Thanks again!

I am not to sure why it is not working, it work fine on my XP machine.

The network stuff is not my best subject, perhaps someone who know more about networking may

be able to provide a reason as to why it not working correct.

Go to this link The Hey, Scripting Guy! Archive

See if they have anything there that may help you, then PM if you need any help with making the script.

  • 0

Biber,

Tried your script and didn't have any luck, it does nothing. Is there a mistake in there somewhere?

Jake,

How many accounts do you have on your PC? If it has multiple profiles, does it copy the needed folders from each profile?

  • 0

I have 4 user accounts on my computer and it copy each of there folders that are listed in the script.

Now this is on my local computer, I have only One computer so I can not test it on a network.

You can try this script I have made some changes in it.

Save As CopyUserFolder_V1.vbs

strComputer = "."
On Error Resume Next 
Dim F1, F2, colAccounts, ColItem, DocSet, Profile, StrUser
Dim Act	  : Set Act = CreateObject("Wscript.Shell")
Dim Fso	  : Set Fso = CreateObject("Scripting.FileSystemObject")
Dim ObjWmi   : Set ObjWmi = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\CIMV2") 
Dim Items	: Set Items = ObjWmi.ExecQuery("SELECT * FROM Win32_OperatingSystem",,48) 
Dim DTop	 : DTop = "\Desktop" 
Dim Fav	  : Fav = "\Favorites"
Dim MyDoc	: MyDoc = "\My Documents"
'/-> Sets The Path To The Folders Using Wmi Querry
  For Each ColItem in Items 
   DocSet = ColItem.SystemDrive & "\Documents and Settings\"
   Profile = ColItem.SystemDrive & "\ProfileBackup\"
  Next
'/-> Create The folder If It Does Not Exists
  If Not Fso.FolderExists(Profile) Then Fso.CreateFolder(Profile) End If
'/-> Loop To Go Threw The User Accounts
  Set Items = ObjWmi.ExecQuery("SELECT * FROM Win32_UserAccount",,48) 
   For Each ColItem in Items
   If Fso.FolderExists(DocSet & ColItem.Name) Then 
   F1 = Profile & ColItem.Name
'/-> Create The folder If It Does Not Exists
   If Not Fso.FolderExists(F1) Then Fso.CreateFolder(F1) End If 
   F2 = F2 & vbCrLf & ColItem.Name 
'/-> Popup Messagebox 
   Act.Popup "Preparing To Copy this User : " & ColItem.Name & vbCrLf &_
   "These Folder :" & DTop & ", " & Fav & ", " & MyDoc ,5,"Copy User Folders",4128
'/-> User Desktop
	StrUser = DocSet & ColItem.Name & DTop
	Fso.CopyFolder(StrUser),(F1 & DTop)
'/-> User Favorite
	StrUser = DocSet & ColItem.Name & Fav
	Fso.CopyFolder(StrUser),(F1 & Fav)
'/-> User My Documents
	StrUser = DocSet & ColItem.Name & MyDoc
	Fso.CopyFolder(StrUser),(F1 & MyDoc)
	End If  
   Next
Act.Popup "Completed Copying These Profiles" & vbCrLf & F2, 5, "Copy Users Folders", 4128
  • 0

@TheReasonUFailed

Biber,

Tried your script and didn't have any luck, it does nothing.

Is there a mistake in there somewhere?

No way. Impossible.

:no: ...okay... may be ...

I think, you forgot the parameters for usernames?

My scripts are running... even underwater... :whistle:

But, here I've just another (formatted) dirty oneliner which will

a) need no parameters

b) you CAN start without risk - the script will just display what it would do

:: -------------- snipp CopyProfiles.bat-------------
@For %%f in (Desktop Favorites "My Documents") do @(
for /f "delims=" %%i in ('dir /b /ad "%userprofile%\..\."') do @(
	  if exist "%userprofile%\..\%%i\%%~f" @(
		   xcopy /L /f /s /o /y /h /r "%userprofile%\..\%%i\%%~f\*.*" "c:\profiles\%%i\%%~f\"
)))
::--------------snapp CopyProfiles.bat-----------

Because of the XCOPY-parms "/L" and "/F" XCopy will display what it WILL copy without the /l parameter and with "/F" it will display the name of each source and target file.

Just try it out.

@excessdl

Really surem, both scripts are working. ;-)

Greetz from Bremen, Germany

Biber,

Mod of "Batch & Shell" on www.administrator.de

Edited by Biber
  • 0

Biber,

That's excellent -- thank you. Here is what I updated your code to, and it works perfectly:

md c:\%ComputerName%-ProfileBackup /y

@For %%f in (Desktop Favorites "My Documents") do @(
for /f "delims=" %%i in ('dir /b /ad "%userprofile%\..\."') do @(
	  if exist "%userprofile%\..\%%i\%%~f" @(
		   xcopy  /f /s /o /y /h /r "%userprofile%\..\%%i\%%~f\*.*" "c:\%ComputerName%-ProfileBackup\%%i\%%~f\"
)))

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.