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!

Script to copy files by owner name problem 1

Status
Not open for further replies.

elmurado

IS-IT--Management
Jul 15, 2003
673
AU
Hi all, I'm working on this script to move files/folders from directories by file owner.
I can get the snippet below to work fine if I have a single IF condition but as soon as I put another in I get an 'Unexpected next' error 800A041F

========================================
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\Scripttestfolder"

Set objFolder = objFSO.GetFolder(objStartFolder)
Wscript.Echo objFolder.Path
Set colFiles = objFolder.Files
For Each objFile in colFiles
Wscript.Echo "the file is:" & objFile.Name
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")



Set colItems = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='" & objFile & "'}" _
& " WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner")

For Each objItem in colItems
Wscript.Echo "The domain is:" & objItem.ReferencedDomainName 'echo owner etc
Wscript.Echo "The owner is:" & objItem.AccountName
WSCript.Echo "==============="
If objItem.AccountName = "admin" Then
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile objFile , "c:\ScriptTestFolder1\"


End If
Next
Next

==============================================
This works, but if I add this:

===========================================
ELSE If objItem.AccountName = "newadmin" Then
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile objFile , "c:\ScriptTestFolder1\"

==========================================
after the first If condition, then I get the Unexpected next error.
I'm wondering if I have the Next or the End If in the wrong place.
 
Ok, so I dumped the IF loop because I have a lot of owners to test the files against and Case Select works out of the box:

==========================
Select Case objItem.AccountName
Case "admin"
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile objFile , "c:\ScriptTestFolder1\"
Case "newadmin"
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile objFile , "c:\ScriptTestFolder1\"
Case "user A"
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile objFile , "c:\ScriptTestFolder1\"
End Select
================================


 
Remove the space in your "ELSE If" to be "ElseIf"

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Maybe something like this:

Code:
objStartFolder = "C:\temp" 

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
	& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
Set objFSO = CreateObject("Scripting.FileSystemObject")                
Set objFolder = objFSO.GetFolder(objStartFolder)
Set objDict = CreateObject("Scripting.Dictionary")
objDict.Add "admin", ""
objDict.Add "newadmin", ""
objDict.Add "user a", ""

Wscript.Echo objFolder.Path
Set colFiles = objFolder.Files

For Each objFile in colFiles
	Wscript.Echo "the file is:" & objFile.Name
	Set colItems = objWMIService.ExecQuery _                    
	("ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='" & objFile & "'}" _     
	& " WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner")        
	
	For Each objItem in colItems                            
		Wscript.Echo "The domain is:" & objItem.ReferencedDomainName        'echo owner etc
		Wscript.Echo "The owner is:" & objItem.AccountName
		WSCript.Echo "==============="
		If objDict.Exists(LCase(objItem.AccountName)) Then
			objFSO.CopyFile objFile , "c:\ScriptTestFolder1\"
		End If
	Next
Next

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Each time I make progress on this script I hit another snag.

I have got to the point where my script is copying files and some folders correctly to the new folders.

The stumbling block I have now is that I am getting a "vbscript runtime error permission denied"

in this snippet:
Code:
Wscript.Echo "Now for the Sub folders"
					

ShowSubfolders objFSO.GetFolder(objStartFolder)

Sub ShowSubFolders(Folder)							'function for recursing folders
    For Each Subfolder in Folder.SubFolders
        Wscript.Echo Subfolder.Path
        Set objFolder = objFSO.GetFolder(Subfolder.Path)
        Set colFiles = objFolder.Files
        For Each objFile in colFiles
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")	                     'bind to cimv2



Set colItems = objWMIService.ExecQuery _					
    ("ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='" & objFile & "'}" _ 	
        & " WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner")		

For Each objItem in colItems							'run through the files enumerated by the recursion
    Wscript.Echo "The domain is:" & objItem.ReferencedDomainName		'echo owner etc
    Wscript.Echo "The owner is:" & objItem.AccountName
Select Case objItem.AccountName
	Case "owner1"
	Set objFSO = CreateObject("Scripting.FileSystemObject") 
	If objFSO.FolderExists("C:\ScriptTestFolder1\" & objFolder) Then
	WScript.Echo objFolder.Name & " Already exists"
	Else
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	objFSO.CopyFolder objFolder , "c:\ScriptTestFolder1\"
	objFSO.DeleteFolder objFolder
	End If
	Case "owner2"
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	objFSO.CopyFolder objFolder , "c:\ScriptTestFolder1\"
	[COLOR=red]objFSO.DeleteFolder objFolder[/color]
	Case "owner3"
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	objFSO.CopyFolder objFolder , "c:\ScriptTestFolder1\"
	objFSO.DeleteFolder objFolder
	End Select
Next
            Wscript.Echo objFile.Name
	    WScript.Echo "=================="
        Next
        Wscript.Echo
        ShowSubFolders Subfolder
    Next

And the error references the delete line I have in red. What I can't work out is that if I go and delete the folder, it works but the delete will not work in the script even though I am running it as the same user. Even if I run as domain admin it won't delete. The rest of the script seems to run
fine.
I think it has to do with the folders being set to read only-how can I overcome this if this is the case?
 
You can try
objFSO.DeleteFolder objFolder, True

or try getting the folder before deleting.
Set objFolder = objFSO.GetFolder("C:\foldertodelete")
objFolder.Delete True


--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Thanks dm4ever. tried that but it doesn't seem to work either.
I'm beginning to think that maybe there is a better way to do this and now I'm going right back to basics to try and figure out if I have something wrong in my logic of how I'm approaching this mini project.

Basically, I need to move all files and folders which are scattered almost randomly on a file server into some newly created shared drives which are arranged along department lines.
hence the need to move files and folders by owner.

I wanted to originally just use MoveFolder but that didn't seem to work in the recursive function above. <scratch head>
 
Okay, so here is the code that I've finished for moving files by owner.
it works quite nicely on the test folders I've setup(recurses to 3 + folders deep).

What I wanted to know was what kind of error checking would be good to include here?

for instance, I was wondering about what would happen to files that were large enough to take a while to move(obviously they don't take as long as a copy as just the inode? directory? info is being modified and the file itself is not being physically moved).

Also, what if it finds a file that it cannot move for strange reasons such as missing owners(ie old deleted users) etc.
Code:
'====================================================
'Script to Move Files recursively
'by owner. 
' talski
'====================================================


Set objFSO = CreateObject("Scripting.FileSystemObject")				'use FSO
objStartFolder = "C:\Scripttestfolder"						'name of top directory change this to reqd dir

Set objFolder = objFSO.GetFolder(objStartFolder)
Wscript.Echo objFolder.Path
Set colFiles = objFolder.Files
For Each objFile in colFiles
    WScript.Echo "==================="
    Wscript.Echo "the file is:" & objFile.Name
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")	'bind to cimv2



Set colItems = objWMIService.ExecQuery _					
    ("ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='" & objFile & "'}" _ 	
        & " WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner")		'this is where it connects to the correct class and

For Each objItem in colItems
    Wscript.Echo "The owner is:" & objItem.ReferencedDomainName & "\" & objItem.AccountName	'run through the files in the top folder
    

Select Case objItem.AccountName							'this will make decision based on owner of file
	Case "sadmin"
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	If objFSO.FileExists("c:\ScriptTestFolder1\sadmin\" & objFile.Name) Then
	WScript.Echo "A file called " & objFile & " already exists"
	WScript.Echo "!!!!!!!!!!!!"
	Else	
	objFSO.MoveFile objFile , "c:\ScriptTestFolder1\sadmin\"
	End If
	Case "sssaadmin"
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	If objFSO.FileExists("c:\ScriptTestFolder1\sssaadmin\" & objFile.Name) Then
	WScript.Echo "A file called " & objFile & " already exists"
	WScript.Echo "!!!!!!!!!!!!"
	Else
	objFSO.MoveFile objFile , "c:\ScriptTestFolder1\sssaadmin\"
	End If
	Case "talski_m"
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	If objFSO.FileExists("c:\ScriptTestFolder1\talski_m\" & objFile.Name) Then
	WScript.Echo "A file called " & objFile & " already exists"
	WScript.Echo "!!!!!!!!!!!!"
	Else
	objFSO.MoveFile objFile , "c:\ScriptTestFolder1\talski_m\"
	End If
	End Select

Next
Next
ShowSubfolders objFSO.GetFolder(objStartFolder)					'function for recursing through the sub-folders
Sub ShowSubFolders(Folder)
    For Each Subfolder in Folder.SubFolders
        Wscript.Echo Subfolder.Path
        Set objFolder = objFSO.GetFolder(Subfolder.Path)
        Set colFiles = objFolder.Files
        For Each objFile in colFiles
		WScript.Echo "==================="
            	Wscript.Echo objFile.Name
Set objWMIService = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")	'bind to cimv2



Set colItems = objWMIService.ExecQuery _					
    ("ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='" & objFile & "'}" _ 	
        & " WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner")
For Each objItem in colItems
	Wscript.Echo "The owner is:" & objItem.ReferencedDomainName & "\" & objItem.AccountName
	
Select Case objItem.AccountName							'move file according to owner and check if file exists
	Case "sadmin"
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	If objFSO.FileExists("c:\ScriptTestFolder1\sadmin\" & objFile.Name) Then
	WScript.Echo "A file called " & objFile & " already exists"
	WScript.Echo "!!!!!!!!!!!!"
	Else	
	objFSO.MoveFile objFile , "c:\ScriptTestFolder1\sadmin\"
	End If
	Case "sssaadmin"
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	If objFSO.FileExists("c:\ScriptTestFolder1\sssaadmin\" & objFile.Name) Then
	WScript.Echo "A file called " & objFile & " already exists"
	WScript.Echo "!!!!!!!!!!!!"
	Else
	objFSO.MoveFile objFile , "c:\ScriptTestFolder1\sssaadmin\"
	End If
	Case "talski_m"
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	If objFSO.FileExists("c:\ScriptTestFolder1\talski_m\" & objFile.Name) Then
	WScript.Echo "A file called " & objFile & " already exists"
	WScript.Echo "!!!!!!!!!!!!"
	Else
	objFSO.MoveFile objFile , "c:\ScriptTestFolder1\talski_m\"
	End If
	End Select
        Next
        Wscript.Echo
        ShowSubFolders Subfolder
    Next
Next
End Sub
 
For starters...I think a little script cleaning might be good. I removed a lot of unnecessary Set objFSO = CreateObject("Scripting.FileSystemObject") which you only need to create once...the same with Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 'bind to cimv2


Code:
'====================================================
'Script to Move Files recursively
'by owner.
' talski
'====================================================


Set objFSO = CreateObject("Scripting.FileSystemObject")                'use FSO
objStartFolder = "C:\Scripttestfolder"                        'name of top directory change this to reqd dir
Set objFolder = objFSO.GetFolder(objStartFolder)
Wscript.Echo objFolder.Path
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")    'bind to cimv2
Set colFiles = objFolder.Files
For Each objFile in colFiles
	WScript.Echo "==================="
	Wscript.Echo "the file is:" & objFile.Name
	
	Set colItems = objWMIService.ExecQuery _                    
	("ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='" & objFile & "'}" _     
	& " WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner")        'this is where it connects to the correct class And
	
	For Each objItem in colItems
		Wscript.Echo "The owner is:" & objItem.ReferencedDomainName & "\" & objItem.AccountName    'run through the files in the top folder
		Select Case objItem.AccountName                            'this will make decision based on owner of file
		Case "sadmin"
			If objFSO.FileExists("c:\ScriptTestFolder1\sadmin\" & objFile.Name) Then
				WScript.Echo "A file called " & objFile & " already exists"
				WScript.Echo "!!!!!!!!!!!!"
			Else    
				objFSO.MoveFile objFile , "c:\ScriptTestFolder1\sadmin\"
			End If
		Case "sssaadmin"
			If objFSO.FileExists("c:\ScriptTestFolder1\sssaadmin\" & objFile.Name) Then
				WScript.Echo "A file called " & objFile & " already exists"
				WScript.Echo "!!!!!!!!!!!!"
			Else
				objFSO.MoveFile objFile , "c:\ScriptTestFolder1\sssaadmin\"
			End If
		Case "talski_m"
			If objFSO.FileExists("c:\ScriptTestFolder1\talski_m\" & objFile.Name) Then
				WScript.Echo "A file called " & objFile & " already exists"
				WScript.Echo "!!!!!!!!!!!!"
			Else
				objFSO.MoveFile objFile , "c:\ScriptTestFolder1\talski_m\"
			End If
		End Select
	Next
Next

ShowSubfolders objFSO.GetFolder(objStartFolder)                    'function for recursing through the sub-folders

Sub ShowSubFolders(Folder)
	For Each Subfolder in Folder.SubFolders
		Wscript.Echo Subfolder.Path
		Set objFolder = objFSO.GetFolder(Subfolder.Path)
		Set colFiles = objFolder.Files
		For Each objFile in colFiles
			WScript.Echo "==================="
			Wscript.Echo objFile.Name
			Set colItems = objWMIService.ExecQuery _                    
			("ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='" & objFile & "'}" _     
			& " WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner")
			For Each objItem in colItems
				Wscript.Echo "The owner is:" & objItem.ReferencedDomainName & "\" & objItem.AccountName
				Select Case objItem.AccountName                            'move file according to owner and check if file exists
					Case "sadmin"
					If objFSO.FileExists("c:\ScriptTestFolder1\sadmin\" & objFile.Name) Then
						WScript.Echo "A file called " & objFile & " already exists"
						WScript.Echo "!!!!!!!!!!!!"
					Else    
						objFSO.MoveFile objFile , "c:\ScriptTestFolder1\sadmin\"
					End If
				Case "sssaadmin"
					If objFSO.FileExists("c:\ScriptTestFolder1\sssaadmin\" & objFile.Name) Then
						WScript.Echo "A file called " & objFile & " already exists"
						WScript.Echo "!!!!!!!!!!!!"
					Else
						objFSO.MoveFile objFile , "c:\ScriptTestFolder1\sssaadmin\"
					End If
				Case "talski_m"
					If objFSO.FileExists("c:\ScriptTestFolder1\talski_m\" & objFile.Name) Then
						WScript.Echo "A file called " & objFile & " already exists"
						WScript.Echo "!!!!!!!!!!!!"
					Else
						objFSO.MoveFile objFile , "c:\ScriptTestFolder1\talski_m\"
					End If
				End Select
			Next
			Wscript.Echo
			ShowSubFolders Subfolder
		Next
	Next
End Sub

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top