Page 1 of 1 1
Topic Options
#153141 - 2005-12-08 09:48 PM Help with checking to see if a function executed or not...
thepip3r Offline
Hey THIS is FUN
*****

Registered: 2005-03-02
Posts: 350
This script is being written to wipe a bunch of temp directories in SAFE mode. My problem is that I'm having a hard time instituting error checking... the script actually works but the messages are misleading and I want them to be accurate. Case and point: the second DirFileWipe() called wipes the temp folder under C:\Windows\Temp but when I'm logged in as a normal user and the script gets access denied, my script still reports as executing successfully. Can anyone suggest how to implement error checking to account for that? @ERROR doesn't seem to be reporting anything... =/

Code:
Break ON
$ = SetOption("WrapAtEOL","ON")

? "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
? "!! ENSURE THIS SCRIPT IS BEING RUN FROM SAFE MODE !!"
? "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" + @CRLF

? "Determining OS Type... "

SELECT
CASE @PRODUCTTYPE = "Windows XP Home Edition" OR @PRODUCTTYPE = "Windows XP Professional"
? @PRODUCTTYPE + @CRLF + @CRLF
$path = %SystemDrive% + "\Temp"
DirFileWipe($path)

$path = %WinDir% + "\Temp"
DirFileWipe($path)

CASE @PRODUCTTYPE = "Windows 2000 Professional" OR @PRODUCTTYPE = "Windows 2000 Server" OR @PRODUCTTYPE = "Windows 2000 Domain Controller"
? "This script is only configured for Windows 2000/XP"

ENDSELECT

Sleep 15

EXIT





function DirFileWipe($path)

IF EXIST ($path)
? "- Wiping temp folder: " + $path
$DirList = DirList($path,4+2+1)
FOR EACH $x in $DirList
? @ERROR
DEL $x /h
NEXT

$DirList2 = DirList($path,4+2+1)
FOR EACH $x in $DirList2
SHELL "%COMSPEC% /c RD /S/Q $x"
? @SERROR
NEXT

? "- $path was wiped successfully..." + @CRLF
ELSE
? "- $path did not exist..."
ENDIF

endfunction

function dirlist($dirname, optional $options)
dim $filename, $counter, $filepath, $mask
dim $list, $sublist, $subcounter

$counter=-1

$dirname=trim($dirname)
if $dirname=''
$dirname=@CURDIR
endif
if right($dirname,1)='\'
$dirname=left($dirname,len($dirname)-1)
endif
if getfileattr($dirname) & 16
$mask='*.*'
else
$mask=substr($dirname,instrrev($dirname,'\')+1)
$dirname=left($dirname,len($dirname)-len($mask)-1)
endif

redim $list[10]
$filename=dir($dirname+'\'+$mask)
while $filename<>'' and @ERROR=0
if $filename<>'.' and $filename<>'..'
select
case (getfileattr($dirname+'\'+$filename) & 16)
if $options & 1
$counter=$counter+1
if $options & 2
$list[$counter]=$dirname+'\'+$filename+'\'
else
$list[$counter]=$filename+'\'
endif
endif
if ($options & 4)
$sublist=dirlist($dirname+'\'+$filename+'\'+$mask,4)
if ubound($sublist)+1
redim preserve $list[ubound($list)+ubound($sublist)+1]
for $subcounter=0 to ubound($sublist)
$counter=$counter+1
if $options & 2
$list[$counter]=$dirname+'\'+$filename+'\'+$sublist[$subcounter]
else
$list[$counter]=$filename+'\'+$sublist[$subcounter]
endif
next
endif
endif
case ($options & 2)
$counter=$counter+1
$list[$counter]=$dirname+'\'+$filename
case 1
$counter=$counter+1
$list[$counter]=$filename
endselect
if $counter mod 10
redim preserve $list[$counter+10]
endif
endif
$filename = dir('')
loop

if $counter+1
redim preserve $list[$counter]
else
$list=''
endif

if $mask<>'*.*' and ($options & 4)
$filename=dir($dirname+'\*.*')
while $filename<>'' and @ERROR=0
if $filename<>'.' and $filename<>'..'
if (getfileattr($dirname+'\'+$filename) & 16)
$sublist=dirlist($dirname+'\'+$filename+'\'+$mask,4)
if ubound($sublist)+1
redim preserve $list[ubound($list)+ubound($sublist)+1]
for $subcounter=0 to ubound($sublist)
$counter=$counter+1
if $options & 2
$list[$counter]=$dirname+'\'+$filename+'\'+$sublist[$subcounter]
else
$list[$counter]=$filename+'\'+$sublist[$subcounter]
endif
next
endif
endif
endif
$filename = dir('')
loop
endif

if $counter+1
redim preserve $list[$counter]
else
$list=''
endif

$dirlist=$list
endfunction


Top
#153142 - 2005-12-08 10:48 PM Re: Help with checking to see if a function executed or not...
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
I don't see where you pass @Error back to the calling script. You need to catch the errorlevel in the UDF and pass it back withthe EXIT parm.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#153143 - 2005-12-09 06:20 AM Re: Help with checking to see if a function executed or not...
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
You can clean up your code quite a bit with:
Code:

IF INSTR(@PRODUCTTYPE,"XP") OR INSTR(@PRODUCTTYPE,"2000")
? @PRODUCTTYPE + @CRLF + @CRLF
$path = %SystemDrive% + "\Temp"
DirFileWipe($path)

$path = %WinDir% + "\Temp"
DirFileWipe($path)
ELSE
? "This script is only configured for Windows 2000/XP"
ENDIF


With your original SELECT statement, it will never touch 2000 Systems.

Of course with more than two conditions, you can go back to the SELECT..CASE..ENDSELECT.

HTH,

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#153144 - 2005-12-09 05:19 PM Re: Help with checking to see if a function executed or not...
thepip3r Offline
Hey THIS is FUN
*****

Registered: 2005-03-02
Posts: 350
Ok, here is the meat of the revised script:

Code:
Break ON
$ = SetOption("WrapAtEOL","ON")

? "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
? "!! ENSURE THIS SCRIPT IS BEING RUN FROM SAFE MODE !!"
? "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" + @CRLF

? "Determining OS Type... "

IF INSTR(@PRODUCTTYPE, "XP") OR INSTR(@PRODUCTTYPE, "2000")

? @PRODUCTTYPE + @CRLF + @CRLF
$path = %SystemDrive% + "\Temp"
DirFileWipe($path)

$path = %WinDir% + "\Temp"
DirFileWipe($path)

ELSE
? "This script is only configured for Windows 2000/XP"
ENDIF

Sleep 15

EXIT





function DirFileWipe($path)

IF EXIST ($path)
? "- Wiping temp folder: " + $path
$DirList = DirList($path,4+2+1)
FOR EACH $x in $DirList
DEL $x /h
NEXT

$DirList2 = DirList($path,4+2+1)
FOR EACH $x in $DirList2
SHELL "%COMSPEC% /c RD /S/Q $x"
? @SERROR
NEXT

? "- $path was wiped successfully..." + @CRLF
ELSE
? "- $path did not exist..."
ENDIF

endfunction



In my DirFileWipe() function, how could I run a check on the passed directory to see if I had the ability to scan it.

IF $DirList <> 0 doesn't evaluate correctly. Looking back at that UDF, I didn't see it reporting error codes on success or failure. Any thoughts?


Edited by thepip3r (2005-12-09 05:19 PM)

Top
#153145 - 2005-12-12 09:52 AM Re: Help with checking to see if a function executed or not...
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Assuming that the UDF returns error codes, you need to check immediately after the function call, i.e.:
Code:
$DirList2 = DirList($path,4+2+1)
If @ERROR
; Error reporting here.
Else
FOR EACH $x in $DirList2
...
Next
EndIf



There is one error that you might get back which is a "no more items" style error - this is not an error condition, it just means that the directory enumeration reached the end of the list so you should ignore it.

Top
#153146 - 2005-12-12 02:44 PM Re: Help with checking to see if a function executed or not...
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Within the function, you need to return @Error with the Exit command.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
Page 1 of 1 1


Moderator:  Arend_, Allen, Jochen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 1607 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.059 seconds in which 0.027 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org