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.
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.