Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy all but one folder 1

Status
Not open for further replies.

dseaver

IS-IT--Management
Jul 13, 2006
467
I am trying to copy all the folders from documents and settings except for the default user, this is what I have, and it does not work

Code:
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")


fso.CreateFolder "C:\cDrive"
fso.CreateFolder "C:\cDrive\Documents and Settings"

On Error Resume Next

Set FSO = CreateObject("Scripting.FileSystemObject")
ShowSubfolders FSO.GetFolder("C:\Documents and Settings")

Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
        If Subfolder.Name = "Default User" Then
	 	Wscript.Echo "DEFAULT"
	Else
		Subfolder.Copy("C:\cDrive\Documents and Settings\")
	End If
	
    Next
End Sub
 
It only copies the first folder then stops
 
You never tell your sub to call itself to recurse the other folders.

Sub ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
If Subfolder.Name = "Default User" Then
Wscript.Echo "DEFAULT"
ShowSubFolders Subfolder.Path 'something like this
Else
Subfolder.Copy("C:\cDrive\Documents and Settings\")
End If

Next
End Sub

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
What do you mean "You never tell your sub to call itself to recurse the other folders."?

My code works when i just echo the folder name, but when I go to copy the folders, its stops after copying after it copies the first one.
Code:
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")


'fso.CreateFolder "C:\cDrive"
'fso.CreateFolder "C:\cDrive\Documents and Settings"

On Error Resume Next

Set FSO = CreateObject("Scripting.FileSystemObject")
ShowSubfolders FSO.GetFolder("C:\Documents and Settings")

Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
        If Subfolder.Name = "Default User" Then
         Wscript.Echo "DEFAULT"
    Else
	Wscript.Echo Subfolder.Path
        'Subfolder.Copy("C:\cDrive\Documents and Settings\")
    End If
    
    Next
End Sub
that echos all of the folders, not just the first, if i uncomment the copy line, it fails
 
Take out the "on error resume next" and you'll be told what the problem is : probably related to the failure to copy.
 
You are likely have problem trying to copy files like UsrClass.dat, NTUSER.DAT, ...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
[1] Also copy method has the "overwrite" switch. If not specified and assuming all should copy well, running the same script twice would still fail - unable to overwrite.
[2] Another way to let you salvage more for those able to get copied is to add "on error resume next" within the sub as well in addition to the directive in the caller of the sub as shown.
 
Ok, adding "on error resume next" in the sub helps each folder write until it errors in that folder. How would I copy each file so that I can minimize data being lost?
 
Also, can I just change ___.copy to ___.move and use the same script? I am just using copy now so that I don't destroy data, I am going to actually be moving the data then moving it back at another point
 
Another way is to use the good old XCOPY (with /C) ...

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Here is the current incarnation, using xcopy, I need to debug the xcopy line

Code:
Dim fso
Dim objSourcePath
Set fso = CreateObject("Scripting.FileSystemObject")


'fso.CreateFolder "C:\cDrive"
'fso.CreateFolder "C:\cDrive\Documents and Settings"


Set FSO = CreateObject("Scripting.FileSystemObject")
ShowSubfolders FSO.GetFolder("C:\Documents and Settings")

Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
	On Error Resume Next
        If Subfolder.Name = "Default User" Then
         Wscript.Echo "DEFAULT"
    Else
	objSourcePath = Subfolder.path 
        WshShell.Run("XCOPY """ & objSourcePath & """ """ & "C:\cDrive\Documents and Settings\"Subfolder.Name"*.*" &  """ /R /I /C /H /K /E /Y")

    End If
    
    Next
End Sub
 
Perhaps this ?
WshShell.Run "XCOPY """ & objSourcePath & "\*.*"" ""C:\cDrive\Documents and Settings\" & Subfolder.Name & "\"" /R /I /C /H /K /E /Y", 3, True

Notes:
You have to instantiate the WshShell object before entering the loop.
Why instantiationg 2 times the fso object ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
One request if possible, add the command to delete the folder and its contents after it is copied
 
Since you are trying ot copy profiles you should do that using the profile manager. Otherwise you will need to log all users off the system and access the disk remotely.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Having errors when copying let me think you'll have problems to delete ...
 
PHV, the two fso's are a result of pasting a couple of solutions togeter then commenting out part so that it doesn't try to create the folders everytime. ALso, that line worked great, i just made a shell object and it was all set
 
The project I am working on calls for me to make backups of the system, but leave the indivdual users out of it, and also to restore the image of the back up everynight. I am using Acronis True Image, and I found that if you restore a backup of file and folders, it leaves a lot of garbage, at least the way I have been doing it. So what I was planning to do was to make a whole partition backup of the C drive without the user info, hence this script, and after the back up is conclude, I would restore all the files i copied to the remote location using the invers of the script, I am copying to the C drive for testing reasons now, but it will be copying to the second drive
 
Ok, I have decided just to move the Desktop and my documents, can someone help me with my modified code now?

Code:
Dim objSourcePath
Dim MyObj
Set MyObj=CreateObject("WScript.Shell")



Set FSO = CreateObject("Scripting.FileSystemObject")
ShowSubfolders FSO.GetFolder("C:\Documents and Settings")

Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
	On Error Resume Next
        If Subfolder.Name = "Default User" Then
         Wscript.Echo "DEFAULT"
    Else
	objSourcePath = ""Subfolder.path "\Desktop\""" 
        MyObj.Run "XCOPY """ & objSourcePath & "\*.*"" ""D:\cDrive\Documents and Settings\" & Subfolder.Name & "\Desktop\"" /R /I /C /H /K /E /Y /Q", 3, True
        objSourcePath = ""Subfolder.path "\My Documents\""" 
        MyObj.Run "XCOPY """ & objSourcePath & "\*.*"" ""D:\cDrive\Documents and Settings\" & Subfolder.Name & "\My Documents\"" /R /I /C /H /K /E /Y /Q", 3, True

    End If
    
    Next
End Sub
 
objSourcePath = Subfolder.path & "\Desktop\*.*"
MyObj.Run "XCOPY """ & objSourcePath & """ ""D:\cDrive\Documents and Settings\" & Subfolder.Name & "\Desktop\"" /R /I /C /H /K /E /Y /Q", 3, True
objSourcePath = Subfolder.path & "\My Documents\*.*"
MyObj.Run "XCOPY """ & objSourcePath & """ ""D:\cDrive\Documents and Settings\" & Subfolder.Name & "\My Documents\"" /R /I /C /H /K /E /Y /Q", 3, True

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Perfect PHV, Thanks! Notice I also took out the other fso :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top