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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Last chance saloon... VB script query

Status
Not open for further replies.

bowwow

IS-IT--Management
Jun 11, 2002
60
GB
Any help much appreciated...
Heres the objectives:
1. check within a local Folder
2. iterate\step through any Files in that Folder looking for files that are 0 bytes in Size
3. should any file be found to be 0 bytes in size, then generate an Event error in the Windows 2000 Event Log.

This is an urgent requirement, and Im pretty new to scripting, so please step through the script and see if you can spot any sytax errors etc Any help very much appreciated.

No-firing VBS script so far:

Sub TestFileSizeOf0InFolder

'declare FSO variable
Dim fso
set fso = CreateObject("Scripting.FileSystemObject")

'declare other variables
Dim fo, fc, f
set fo = fso.GetFolder("c:\testfolder\")
set fc = fo.Files
set f = fc.FileSize

for each f in fc
if f.size=0 then

Const cError = 1
Const cWarning = 2
Const cInformation = 4

sub LogError(intErrorType)
set oEvent = Server.createObject("Log.Event")
OEvent.Application = "Specify Source"
OEvent.Description = "Provide descriptive text"
oEvent.EventID = "1001"
oEvent.LogType = intErrorType
oEvent.WriteEvent
set oEvent = Nothing
Err.Clear
End Sub

end if
next

End Sub


 
Hello bowwow,

This is then your revised script? What do you mean by
>No-firing VBS script so far

I can only see these things on the surface.
[1] I had already deleted this line, but it reappears! It is definitely wrong.
[tt][red]set f = fc.FileSize[/red][/tt]
[2] If your script is to be executed on the server, you may need to change this.
[tt]set fso = [red]Server.[/red]CreateObject("Scripting.FileSystemObject")[/tt]
[3] You do not construct nested sub/function in vbs. It is not supported. If you intend to execute it serially like you have shown, then make a line to call it like:
[tt]LogError cError[/tt]
or what ever errortype constant as argument. Then move the whole [blue]sub LogError(intErrorType)... end sub[/blue] block outside the sub TestFileSizeOf0InFolder.
[4] Move the const declaration out of the if ... end if block and even preferrably to the top of the sub. It's not good practice. Worse, it may cause error if the if...end if has to be executed more than once.
[5] On the server, it is important to do active cleanup because leak accumulate if anything goes wrong. So, it is better to add these at appropriate place before exiting the sub.
[tt] set fc=nothing
set fo=nothing
set fso=nothing[/tt]
[6] I don't think I know log.event component so I cannot comment on it.

regards - tsuji
 
Thanks once again Tsuji.
Does the sytax below, now make more sense? Although believe calling of the log.event needs modification, will look into this later (on a course this morning).

Sub TestFileSizeOf0InFolder

Const cError = 1
Const cWarning = 2
Const cInformation = 4

'declare FSO variable
Dim fso
set fso = Server.CreateObject("Scripting.FileSystemObject")

'declare other variables
Dim fo, fc, f
set fo = fso.GetFolder("c:\testfolder\")
set fc = fo.Files

for each f in fc
if f.size=0 then
LogError cError

end if
next

set fc=nothing
set fo=nothing
set fso=nothing

End Sub

sub LogError(intErrorType)
set oEvent = Server.createObject("Log.Event")
OEvent.Application = "Specify Source"
OEvent.Description = "Provide descriptive text"
oEvent.EventID = "1001"
oEvent.LogType = intErrorType
oEvent.WriteEvent
set oEvent = Nothing
Err.Clear
End Sub
 
Since had feedback that the 'LogError cError' line isnt valid, as such modified as per below. Now getting error on the last line!! Boy.. am I hoping this is nearly solved!! Ive been looking for something in the structure of it all, but cant spot what it may be.

Any help much'o appreciated!

Error is:
C:\script4.vbs(32, 1) Microsoft VBScript compilation error: Expected statement

Latest statement:

'Event Constants
Const EVENT_TYPE_SUCCESS = 0
Const EVENT_TYPE_ERROR = 1

'declare FSO variable
Dim fso
set fso = Server.CreateObject("Scripting.FileSystemObject")

'declare other variables
Dim fo, fc, f
set fo = fso.GetFolder("c:\scripts\")
set fc = fo.Files

for each f in fc
if f.size=0 then

Dim oNewEvent

' Create a new event
Set oNewEvent = ScriptContext.CreateEvent
' Set event properties
oNewEvent.Message = ("Text to reflect problem here")
oNewEvent.EventNumber = 1001
oNewEvent.EventType = 1
' Submit the event
ScriptContext.Submit oNewEvent

Set oNewEvent = Nothing

end if
next

set fc=nothing
set fo=nothing
set fso=nothing

End
 
End isn't a valid VBScript instruction.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
bowwow,

As pointed out, use "End Sub" rather than "End". The rest is the other guys' problem, those who wrote the components, either Log.Event or ScriptContext.

- tsuji
 
Now have a working solution! see below. Special thanks to tsuji for helping me get this straight, thanks again. Special point of note is its designed to run within MOM by a MOM Agent, runs on both 2000 Sp1 and 2005:

Const cError = 1
Const cWarning = 2
Const cInformation = 4

'declare FSO variable
Dim fso
set fso = CreateObject("Scripting.FileSystemObject")

'declare other variables
Dim fo, fc, f
set fo = fso.GetFolder("c:\testfolder\")
set fc = fo.Files

for each f in fc
if f.size=0 then
CreateSubmitEvent

Else

end if

next

set fc=nothing
set fo=nothing
set fso=nothing

Sub CreateSubmitEvent
Dim oNewEvent
Set oNewEvent = ScriptContext.CreateEvent
oNewEvent.Message = ("File of 0 Bytes in Size Discovered")
oNewEvent.EventNumber = 9999
oNewEvent.EventType = cError
ScriptContext.Submit(oNewEvent)
Set oNewEvent = Nothing
End Sub
 
award tsuji a star then! there never seems to be enough in this forum :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top