Jump to content

VBScripting Issue with System Administration Script


Recommended Posts

Guest adrianr
Posted

Hello,

I hope this is the correct group to send this to....

 

I've written a vbscript that searches through a file system and reports on

files that have not been accessed in xx days. I pulled a lot of the code

from scripting guys examples.

My scripts works fine until I get to a folder that the script does not have

access to. With 'On Error Resume Next' configured, I check to see if 'err'

is <> 0 and if so, write the problem folder to a dictonary object before

clearing the error and then resuming the script.

My problem is that for some unknown reason, my script always picks up the

next folder in the sequence and reports an error on that too - even if it

gets processed.

 

Can someone point out what I'm doing wrong?

 

I've put a snippet of the code at the bottom - I can post the whole thing if

requested.

 

Many Thanks,

 

Adrian

 

My Subroutine that gets called:

 

Sub ShowSubFolders(Folder)

On Error Resume Next

For Each Subfolder in Folder.SubFolders

Set objFolder = objFSO.GetFolder(Subfolder.Path)

Set colFiles = objFolder.Files

 

' For Each file in the folder....

For Each objFile in colFiles

 

dtmFileDate = objFile.DateLastAccessed

If dtmFileDate < dtmDate Then

strText = objFile.Path

fleLOG.WriteLine strText

End If

Next

If Err <> 0 Then

'We have an error reading the files/folders - probably access

denied...

strText = SubFolder.Path

If Not objDictionary.Exists(strText) Then

objDictionary.Add strText, strText

intErrorCount = intErrorCount + 1

wscript.echo "Error Number : " & intErrorCount & " is with :

" & strText & " and error code is " & err.description

Err.Clear

End If

End If

' Routine calls itself recusively until the folder tree is processed.

ShowSubFolders Subfolder

Next

End Sub

 

 

My bit at the end that logs 'problem' folders:

 

If intErrorCount > 0 Then

wscript.echo intErrorCount

Set fleErr = objFSO.CreateTextFile(strErrorFile, TRUE)

For Each strText in objDictionary.Keys

fleErr.WriteLine strText

Next

End If

 

I have one folder that I can't access - however intErrorCount is always 3

and two folders are in my errors.txt file:

C:\System Volume Information

C:\System Tests

  • Replies 3
  • Created
  • Last Reply
Guest Richard Mueller [MVP]
Posted

Re: VBScripting Issue with System Administration Script

 

adrianr wrote:

> I hope this is the correct group to send this to....

>

> I've written a vbscript that searches through a file system and reports on

> files that have not been accessed in xx days. I pulled a lot of the code

> from scripting guys examples.

> My scripts works fine until I get to a folder that the script does not

> have

> access to. With 'On Error Resume Next' configured, I check to see if

> 'err'

> is <> 0 and if so, write the problem folder to a dictonary object before

> clearing the error and then resuming the script.

> My problem is that for some unknown reason, my script always picks up the

> next folder in the sequence and reports an error on that too - even if it

> gets processed.

>

> Can someone point out what I'm doing wrong?

>

> I've put a snippet of the code at the bottom - I can post the whole thing

> if

> requested.

>

> Many Thanks,

>

> Adrian

>

> My Subroutine that gets called:

>

> Sub ShowSubFolders(Folder)

> On Error Resume Next

> For Each Subfolder in Folder.SubFolders

> Set objFolder = objFSO.GetFolder(Subfolder.Path)

> Set colFiles = objFolder.Files

>

> ' For Each file in the folder....

> For Each objFile in colFiles

>

> dtmFileDate = objFile.DateLastAccessed

> If dtmFileDate < dtmDate Then

> strText = objFile.Path

> fleLOG.WriteLine strText

> End If

> Next

> If Err <> 0 Then

> 'We have an error reading the files/folders - probably access

> denied...

> strText = SubFolder.Path

> If Not objDictionary.Exists(strText) Then

> objDictionary.Add strText, strText

> intErrorCount = intErrorCount + 1

> wscript.echo "Error Number : " & intErrorCount & " is with

> :

> " & strText & " and error code is " & err.description

> Err.Clear

> End If

> End If

> ' Routine calls itself recusively until the folder tree is

> processed.

> ShowSubFolders Subfolder

> Next

> End Sub

>

>

> My bit at the end that logs 'problem' folders:

>

> If intErrorCount > 0 Then

> wscript.echo intErrorCount

> Set fleErr = objFSO.CreateTextFile(strErrorFile, TRUE)

> For Each strText in objDictionary.Keys

> fleErr.WriteLine strText

> Next

> End If

>

> I have one folder that I can't access - however intErrorCount is always 3

> and two folders are in my errors.txt file:

> C:\System Volume Information

> C:\System Tests

>

>

 

I like to use "On Error Resume Next" only for the statements that I expect

might raise an error, then restore normal error handling with "On Error GoTo

0". When I worked on a similar script this allowed me to see all the places

where an error can happen and so deal with them. I used code similar to

below:

=========

Sub SearchFiles(strPath)

' Recursive sub to search files.

Set objFolder = objFSO.GetFolder(strPath)

Set objFiles = objFolder.Files

 

' Trap possible error on "For Each" statement.

On Error Resume Next

For Each strFile In objFiles

If (Err.Number <> 0) Then

Wscript.Echo "Error Number: " & Err.Number

Wscript.Echo "Description: " & Err.Description

Wscript.Echo "Folder: " & objFolder.Path

End If

' Restore normal error handling.

On Error GoTo 0

' More code to process file.

' ...

' Trap possible error on "For Each" statement.

On Error Resume Next

Next

If (Err.Number <> 0) Then

Wscript.Echo "Error Number: " & Err.Number

Wscript.Echo "Description: " & Err.Description

Wscript.Echo "Folder: " & objFolder.Path

End If

On Error GoTo 0

 

' More code.

 

' Trap possible error on "For Each" statement.

On Error Resume Next

For Each strFolder In objFolder.SubFolder

If (Err.Number <> 0) Then

Wscript.Echo "Error Number: " & Err.Number

Wscript.Echo "Description: " & Err.Description

Wscript.Echo "Folder: " & objFolder.Path

Else

' Restore normal error handling.

On Error GoTo 0

Call SearchFiles(strFolder)

End If

' Trap possible error on "For Each" statement.

On Error Resume Next

Next

End Sub

========

I hope this helps. It allows me to process all files on a Vista machine,

where there are many permissions issues. Note that the statement "On Error

GoTo 0" also clears any error condition.

 

--

Richard Mueller

Microsoft MVP Scripting and ADSI

Hilltop Lab - http://www.rlmueller.net

--

Guest eaguilar
Posted

Re: VBScripting Issue with System Administration Script

 

I would suggest avoiding "Go To" statements as much as you can. "Spaghetti

code" will have a negative impact on code performance in general, and on

segmented architectures in particular. It is actually surprising that

vbscript still provides the possibility to use it, in the XXI century :)

 

Right after each loop's iteration, you can use Err.Clear to rid yourself of

any previous errors.

 

Hope this helps.

 

--

 

</edwin>

Guest adrianr
Posted

Re: VBScripting Issue with System Administration Script

 

'Eaguilar'/Richard,

 

Many thanks for your input with my problems - After a great deal of editing

and re-editing the script is now working as expected.

 

Thanks again to both of you for your assistance!

 

Adrian

 

"eaguilar" wrote:

> I would suggest avoiding "Go To" statements as much as you can. "Spaghetti

> code" will have a negative impact on code performance in general, and on

> segmented architectures in particular. It is actually surprising that

> vbscript still provides the possibility to use it, in the XXI century :)

>

> Right after each loop's iteration, you can use Err.Clear to rid yourself of

> any previous errors.

>

> Hope this helps.

>

> --

>

> </edwin>

>


×
×
  • Create New...