Jump to content

Recommended Posts

Posted

Hi All

 

I was wondering if the scripting gurus out there could help me with something.

 

Basically I am after a way of doing this

 

copy the contents of each users "my documents" from within the users home folder i.e. F:\user\2031\my documents\ to a new folder that has already been created i.e. H:\users\2031

 

I need the script to automatically crawl through the source folder i.e F:\users , look in the existing folders , find the my docs folder inside and then copy its contents to the new folder if that makes sense?

 

really struggling with this one as not a script wizard at all

 

many thanks

 

Mark

Posted

It Looks like this F:\users\%username%\my documents .. contents to be copied to H:\users\%username%\

 

If that makes sense .. cheers for the quick reply dude

Posted

I was going to do it in Powershell then I remembered I did a similar script for someone last week so I just modified that one.

 

Save the code as MoveFiles.vbs or something like that. It might not be to robust but I told it to continue on error ie file in use etc. Not sure how well it will work.

You need the edit the ORIGINAL_ROOT and DEST_ROOT.

Please try it on a small test elsewhere first eg copy a few users folders elsewhere and point it at them first.

USE AT YOUR OWN RISK!!!!!!!!!

 

On Error Resume Next
Set FSO = CreateObject("Scripting.FileSystemObject")
Const ORIGINAL_ROOT = "D:\Test" ' Source folder
Const DEST_ROOT = "D:\Test2" ' Destination folder
Set objFolder = FSO.GetFolder(ORIGINAL_ROOT)

strCurrentFolder = ""


   For Each Subfolder in objFolder.SubFolders
	strCurrentFolder = subfolder.path
      ' Wscript.Echo "Currently looking at: " & Subfolder.Path & VbCrLf ' For testing to show which folder 
	FSO.CopyFile strCurrentFolder & "\My Documents\*.*", DEST_ROOT & "\" & Subfolder.Name 'Copy all files in the root
	FSO.CopyFolder strCurrentFolder & "\My Documents\*.*", DEST_ROOT & "\" & Subfolder.Name ' Copy all folders in the root
	'Wscript.Echo "Copy line=" & strCurrentFolder & "\My Documents\*.* " & DEST_ROOT & "\" & Subfolder.Name
   Next

  • Thanks 1
Posted

I wasn't happy with just using the copy function so here is a version that will use robocopy. This will preserve the NTFS permissions as well.

Please run it from an elevated cmd prompt or it will not work:

 

cscript CopyFilesRobo.vbs

 

Here is the new script

 

Set FSO = CreateObject("Scripting.FileSystemObject")

Const ORIGINAL_ROOT = "D:\Test" ' Source folder
Const DEST_ROOT = "D:\Test2" ' Destination folder
Set objFolder = FSO.GetFolder(ORIGINAL_ROOT)
Set objShell = CreateObject("WScript.Shell")
strCmdLine = ""
strCurrentFolder = ""

Const ROBO_OPTIONS = "/e /zb /copyall /r:3 /w:15" ' Change these robocopy switches to what you want


   For Each Subfolder in objFolder.SubFolders
	strCurrentFolder = subfolder.path
       'Wscript.Echo "Currently looking at: " & Subfolder.Path & VbCrLf ' For testing to show which folder 
	strCmdLine = "Robocopy " & Chr(34) & Subfolder.Path & "\My Documents" & Chr(34) & " " & DEST_ROOT & "\" & subfolder.name & " " & ROBO_OPTIONS' to build the cmd line that will run
	'Wscript.echo strCmdLine ' to show the cmd line that will run
	ObjShell.Run StrCmdLine,1,True 'run robocopy
	
   Next

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...