Link to home
Start Free TrialLog in
Avatar of sirbounty
sirbountyFlag for United States of America

asked on

Trouble with script

Thanx to another expert, guidway, I was able to produce the following script:
It works great up until it comes in contact with a certain folder/file apparently.

I thought at first it was due to the folder being named with a "!" but I don't believe that's it.  Presumably this scans alphabetically?  If so, we go through the following folders before it stops:

!!!!MEU Pricing folder
!!Save_Florida_2003
!Thomas O'Malley

The last entry is a Hack.MDB in !!Save_Florida_2003 before it stops and errors on:
(66,6) (null): 0x8004103A

VERY GRATEFUL FOR ANY AND ALL ASSISTANCE WITH THIS!!! : )

Code below:
==============================

Dim FSO, oFil, s2, objArgs, objItem
Dim computerName, owner
Dim objFileSystem, objOutputFile
Dim strOutputFile
Set FSO = CreateObject("Scripting.FileSystemObject")
  strComputer = "."
  Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
  Set objNetwork = Wscript.CreateObject("Wscript.Network")
  computerName = objNetwork.ComputerName
   strOutputFile = "e:\batch\" & computerName & "-db.lst"
   Set objFileSystem = CreateObject("Scripting.fileSystemObject")
   Set objOutputFile = objFileSystem.CreateTextFile(strOutputFile, TRUE)

   if fso.folderexists("g:\groups_1") then
     set f = fso.getfolder("g:\groups_1")
   elseif fso.folderexists("g:\groups") then
     set f = fso.getfolder("g:\groups")
   end if
 
   call searchall(f)

   if fso.folderexists("h:\groups_2") then
     set f = fso.getfolder("h:\groups_2")
   elseif fso.folderexists("h:\groups") then
     set f = fso.getfolder("h:\groups")
   end if
   
   call searchall(f)

   if fso.folderexists("i:\groups_3") then
     set f = fso.getfolder("i:\groups_3")
   elseif fso.folderexists("i:\groups") then
     set f = fso.getfolder("i:\groups")
   end if

   call searchall(f)

Sub SearchFolder(byRef objFolder)
    Dim objFile, objSubFolder

    if not objFolder.name = "System Volume Information" then
       For Each objFile in objFolder.Files
          Call SearchFile(objFile)    
       next 'objFile  
    end if

    if not objFolder.name = "System Volume Information" then
       For Each objSubFolder in objFolder.SubFolders
         Call SearchFolder(objSubFolder)
    Next 'objSubFolder  
    end if
End Sub
Sub SearchAll(byVal strFolder)
    dim objFSO, objFolder
  ' Setup the FSO Object  
    Set objFSO = CreateObject("Scripting.FileSystemObject")
  ' Get the root folder  
    Set objFolder = objFSO.GetFolder(strFolder)
    objOutputFile.WriteLine("SERVER NAME" & vbTab & "CREATED" & vbTab & "LAST ACCESSED" & vbTab & "MODIFIED" & vbTab & "FILE NAME" & vbTab & "SIZE" & vbTab & "COMPLETE PATH" & vbTab & "OWNER")
    Call SearchFolder(objFolder)
End Sub
Sub SearchFile(byVal oFil)
  if right(oFil.name, 4) = ".mdb" then  
'     objOutputFile.WriteLine(oFil.path)
     Set colItems = objWMIService.ExecQuery ("ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='" & oFil.path & "'}" & " WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner")
     For Each objItem in colItems
         owner = objItem.AccountName
     Next
'         s2 = ""
       s2 = Computername & vbTab & left(oFil.DateCreated, 10) & vbTab
         s2 = s2 & left(oFil.DateLastAccessed, 10) & vbTab
         s2 = s2 & left(oFil.DateLastModified, 10) & vbTab
         s2 = s2  & oFil.Name &  vbTab
         s2 = s2  & formatnumber(oFil.Size, 0,-1,,-1) & " bytes" & vbTab
         s2 = s2 & oFil.Path & vbTab
         s2 = s2 & owner & " "
         objOutputFile.WriteLine(s2)
  end if
End Sub
Avatar of sirbounty
sirbounty
Flag of United States of America image

ASKER

Appears, from testing on another device to be the ' character...how do I get around this?
SOLUTION
Avatar of Arthur_Wood
Arthur_Wood
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Arthur_Wood.
Presumably, I would test prior to this section, correct?

       For Each objFile in objFolder.Files
--->  objFile=replace(objFile,"'","''")
          Call SearchFile(objFile)    
       next 'objFile

But wouldn't that error out trying to find the file?

If the filename is currently Jim'sFile and I then change it with replace to Jim''sFile - it won't find that file...will it?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great - I'll give that a shot - thanx!
Okay, I'm a beginner no doubt...but...

Presumably somewhere inside the SearchFile routine is where i'd change this...

Sub SearchFile(byVal oFil)
but if I try
oFil.name=Replace(Ofil.name,"'","?")

It's stating file already exists... :(

Where should I replace the file name string?  objFile perhaps?
Update...closer, but still no cigar.

here's wat I've discovered.  Yes, it must be indeed with the ' character.  I renamed it and it ran through nicely.
However - if I don't rename it (which would be impossible on all servers - not to mention customer complaints :)
it fails here:

 Set colItems = objWMIService.ExecQuery ("ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='" & oFil.path & "'}" & " WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner")
     For Each objItem in colItems  <-----  errror points here
         owner = objItem.AccountName
     Next


So...I decided to use the replace string and it appears to 'work' - however, now it is not pulling back ANY ownership information...  here's the revised line:

     Set colItems = objWMIService.ExecQuery ("ASSOCIATORS OF {Win32_LogicalFileSecuritySetting='" & replace(oFil.path,"'","?") & "'}" & " WHERE AssocClass=Win32_LogicalFileOwner ResultRole=Owner")
Going to try to alter as I found here:
http://www.microsoft.com/technet/scriptcenter/resources/qanda/oct04/hey1007.mspx

Will post back shortly...
Nah - still not it.

For now, I'll just use an
if instr(1,oFil.path,"'") then owner="" and skip the check owner WMI call.

I'll leave this open for a little longer - if someone has an answer - I'D LOVE to know it!!!

It's working on all the other files as expected.  Thanx for your time!
http://www.microsoft.com/technet/scriptcenter/resources/qanda/dec04/hey1217.mspx seems to resolve it...I'll try it out and let you know!
Well, 'yes' and 'no'.  The answer is to escape the apostrophe with a preceding backslash (so ' becomes \').

But I have yet to get it to work properly and here is why I think so...

I pass objFile as an object here:

For Each objFile in objFolder.Files
          Call SearchFile(objFile)    

If I tried to use the replace function, well now I've no longer got an object, I've got a string, right?  So, no more referencing objFile (or oFil) 'dot' anything (Path or Name).

So...I tried passing two seperate variables, strFileName and strFolderName - but was having trouble, and thought better of it.

Any ideas how to bypass my problem, please post.  I'm still working on this.  Thanx
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of fatalXception
fatalXception

cool, glad you got it working. I only realised late last night after posting my comment that although it'd run, it wouldn't find anything. duh.