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!

Sanity check... 1

Status
Not open for further replies.

bowwow

IS-IT--Management
Jun 11, 2002
60
GB
Could someone please assist with my script below. Im pretty certain my use of the IF.. Then is incorrect, Im new to scripting so still trying to get to grips with the Syntax issues. Im trying to check for any File of Size 0 with a specific local folder. Any help much appreicated:

Function TestFileSizeOf0InFolder

Dim fso, fo, fc, f

set fso = CreateObject("Scripting.FileSystemObject")
set fo = fso.GetFolder("c:\testfolder\")
set fc = fo.Files
set f = fc.FileSize

If f=("0") Then

WScript.Echo "File Found"

Else

End If

End Function
 
Hello bowwow,

One way to do it is this.
Code:
Function TestFileSizeOf0InFolder

Dim fso, fo, fc, f

set fso = CreateObject("Scripting.FileSystemObject")
set fo = fso.GetFolder("c:\testfolder\")
set fc = fo.Files
for each f in fc
    if f.size=0 then
        WScript.Echo "File Found"
        TestFileSizeOf0InFolder=1    'add optional functionality
        exit function    'one found would suffice?
    end if
next
wscript.echo "File not found"
TestFileSizeOf0InFolder=0    'added optional functionality
'cleanup left to garbage collection

End Function
regards - tsuji
 
Thanks Tsuji,
Ive modified the script (I hope appropriatly) to reflect the fact that 1 file of 0 size if sufficeint to cause concern.

What would be the best method of Generating an Event in the Windows Event Logs via vbs? (I presume replaceing the WScript.Echo command)

Any help much appreciated!

Function TestFileSizeOf0InFolder

Dim fso, fo, fc, f

set fso = CreateObject("Scripting.FileSystemObject")
set fo = fso.GetFolder("c:\testfolder\")
set fc = fo.Files
for each f in fc
if f.size=0 then
WScript.Echo "File Found"
exit function
end if
next
wscript.echo "File not found"
End Function
 
bowwow,

Check out logEvent method of wshshell.
documentation said:
LogEvent Method
Adds an event entry to a log file.

object.LogEvent(intType, strMessage [,strTarget])
Arguments
object
WshShell object.
intType
Integer value representing the event type.
strMessage
String value containing the log entry text.
strTarget
Optional. String value indicating the name of the computer system where the event log is stored (the default is the local computer system). Applies to Windows NT/2000 only.
Remarks
The LogEvent method returns a Boolean value (true if the event is logged successfully, otherwise false). In Windows NT/2000, events are logged in the Windows NT Event Log. In Windows 9x/Me, events are logged in WSH.log (located in the Windows directory). There are six event types.

Type Value
0 SUCCESS
1 ERROR
2 WARNING
4 INFORMATION
8 AUDIT_SUCCESS
16 AUDIT_FAILURE

Example
The following code logs SUCCESS or ERROR depending on the outcome of the function runLoginScript().

[VBScript]
Set WshShell = WScript.CreateObject("WScript.Shell")
rc = runLoginScript() 'Returns true if logon succeeds.

if rc then
WshShell.LogEvent 0, "Logon Script Completed Successfully"
else
WshShell.LogEvent 1, "Logon Script failed"
end if
[JScript]
var WshShell = WScript.CreateObject("WScript.Shell");
var rc = runLoginScript();

if (rc)
WshShell.LogEvent(0, "Logon Script Completed Successfully");
else
WshShell.LogEvent(1, "Logon Script failed");
- tsuji
 
Set WshShell = WScript.CreateObject("WScript.Shell")
rc = runLoginScript() 'Returns true if logon succeeds
if rc then
WshShell.LogEvent 0, "Logon Script Completed Successfully"
else
WshShell.LogEvent 1, "Logon Script failed"
end if

 
Well, thanks to you guys I think Im getting close now. Ive modified the Script as per your pointers but its still not firing. Within the test folder there are 2 files, 1 with size of 0, another which is has some bytes of data. As such Id expect it to log an error Event in the Event Log, but it doesent seem to be doing so? Am I missing something? Is the 'f'\file set correctly?

Function TestFileSizeOf0InFolder

Dim fso, fo, fc, f, WshShell

set fso = CreateObject("Scripting.FileSystemObject")
set fo = fso.GetFolder("c:\testfolder\")
set fc = fo.Files
Set WshShell = WScript.CreateObject("WScript.Shell")
for each f in fc
if f.size=0 then
WshShell.LogEvent 1, "At least one file, of 0 bytes in Size, detected in c:\testfolder\ Folder"
exit function
end if
next
WshShell.LogEvent 0, "No Files of Size 0 detected in c:\testfolder\ Folder - Successful Check"
End Function
 
bowwow,

Is it that it writes as success, or that there is nothing left in the log?

Make sure you look at the right place for the event log file. Otherwise, make use of the 3rd argument to specify the location of the log to your liking (if it is winNT up)?

- tsuji
 
I double-clcik the vbs file and nothing appears to happen. I check the Event logs, via eventvwr.exe, and there are no new Events in any of the Logs. Whereas Id expect it should have posted either a Success or Error event, as detailed in teh script.
 
careful with your use of Function. you are not returning anything so i would go for Sub

also, generally i tend to only put my big object declarations at the top of my script globally.

otherwise if you called your function like

Function TestFileSizeOf0InFolder(strFolder)

for 1000's folders, (the whole idea of functions and subs is reusablity, or one of them at least) then you create and destory FSO and WshShell 1000's times. not a good plan i would say
 
bowwow,

Are you running on winNT? Check you version of wsh by this single line.
[tt]wscript.echo wscript.version[/tt]
It should better be 5.6, else update your wsh.

- tsuji
 
ok will update the version... script is due to run on Windows 2000, although Ive been doing the tests on XP platform uptill now
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top