+ Post New Thread
Results 1 to 8 of 8
Scripts Thread, File and Folder copying script in Coding and Web Development; Hi All I was wondering if the scripting gurus out there could help me with something. Basically I am after ...
  1. #1
    marekbrad's Avatar
    Join Date
    May 2006
    Location
    Bradford
    Posts
    138
    Thank Post
    25
    Thanked 38 Times in 31 Posts
    Rep Power
    23

    File and Folder copying script

    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

  2. #2
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    4,987
    Thank Post
    120
    Thanked 283 Times in 261 Posts
    Rep Power
    107
    Is the my documents folder always in the root of the Old users folder?

  3. Thanks to ChrisH from:

    marekbrad (24th August 2011)

  4. #3
    marekbrad's Avatar
    Join Date
    May 2006
    Location
    Bradford
    Posts
    138
    Thank Post
    25
    Thanked 38 Times in 31 Posts
    Rep Power
    23
    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

  5. #4
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    4,987
    Thank Post
    120
    Thanked 283 Times in 261 Posts
    Rep Power
    107
    What os will you run this on?

  6. #5
    marekbrad's Avatar
    Join Date
    May 2006
    Location
    Bradford
    Posts
    138
    Thank Post
    25
    Thanked 38 Times in 31 Posts
    Rep Power
    23
    server 2008R2

  7. #6
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    4,987
    Thank Post
    120
    Thanked 283 Times in 261 Posts
    Rep Power
    107
    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!!!!!!!!!

    Code:
    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

  8. Thanks to ChrisH from:

    marekbrad (24th August 2011)

  9. #7
    marekbrad's Avatar
    Join Date
    May 2006
    Location
    Bradford
    Posts
    138
    Thank Post
    25
    Thanked 38 Times in 31 Posts
    Rep Power
    23
    Chris u are a star buddy thank you so much .. will try it out as you say and let you know

    many many many thanks :-)

  10. #8
    ChrisH's Avatar
    Join Date
    Jun 2005
    Location
    East Lancs
    Posts
    4,987
    Thank Post
    120
    Thanked 283 Times in 261 Posts
    Rep Power
    107
    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:

    Code:
    cscript CopyFilesRobo.vbs
    Here is the new script

    Code:
    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

SHARE:
+ Post New Thread

Similar Threads

  1. Disabling file and folder compression
    By KK20 in forum Windows Server 2008
    Replies: 3
    Last Post: 14th June 2011, 08:29 AM
  2. Script to copy file and rename with YYMMDD_HHMM
    By Admiral208 in forum Scripts
    Replies: 12
    Last Post: 7th January 2011, 06:16 AM
  3. File and folder sync app
    By cookie_monster in forum Windows
    Replies: 3
    Last Post: 2nd December 2010, 01:41 PM
  4. Long File and Folder Names
    By karldenton in forum Windows
    Replies: 16
    Last Post: 22nd July 2009, 12:47 PM
  5. Replies: 6
    Last Post: 28th September 2006, 07:06 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •