Jump to content

How can I DO something until a bunch of things?


kor
 Share

Recommended Posts

So I am trying to figure out what the best way is to program this.

I have about 30+ different things that my script does as part of a cleanup function. I am having some reliability issues because I think some things are failing because of permissions. I want to build in some debugging and such. So what would be the best way to simplify this code when running 30 things in a series?

; Delete driver folder
    $i = 0
    Do
        DirRemove("C:\drivers", 1)
        $i &= + 1
        Sleep(500)
    Until Not FileExists("C:\drivers\") Or $i > 600 ; allow up to 5 minutes to delete driver folder
If FileExists("C:\drivers\") Then _Warning("Failed to deleted driver folder")

I want to run through and do 20 or 30 different things, and keep doing each thing until the script is sure the thing it's doing is done. Meaning, don't move on in the script until you're sure the driver folder has been deleted (or 5 minutes has passed) If the folder hasn't been deleted after 5 minuntes/600 tries there is a problem.

Link to comment
Share on other sites

From the help file it didn't look like dirRemove set @error. It only returned 1 or 0.

Edit: yes, I did mean += 1, thank you for catching that.

So

$i = 0

$i += 1

is equivalent to

$i = 0

$i = $i + 1

??

Edited by kor
Link to comment
Share on other sites

From the help file it didn't look like dirRemove set @error. It only returned 1 or 0.

oops your right. :unsure:

well in that case heres 3 alternatives :-p

If Not DirRemove("C:\drivers", 1) Then _Warning("Failed to deleted driver folder")

$i = DirRemove("C:\drivers", 1)
If $i = 0 Then _Warning("Failed to deleted driver folder")

$i = DirRemove("C:\drivers", 1)
If Not $i Then _Warning("Failed to deleted driver folder")

Either way theres no use in "giving it time" to remove, cause the dirremove function will only return if its done and not before, so basacally you are "giving it time" after its already done wat it has to do ;-)

[Edit]wrote the last one backwards :-s

Edited by Djarlo
Link to comment
Share on other sites

how does this look?

$i = 0
    Do
        $iError = 0
        If Not FileDelete(@DesktopCommonDir & "\Windo*.lnk") Then $iError = 1
        If Not FileDelete(@DesktopCommonDir & "\Micro*.lnk") Then $iError = 1
        If Not FileDelete(@StartupCommonDir & "\*.lnk") Then $iError = 1
        If Not DirRemove(@ProgramsCommonDir & "\Games", 1) Then $iError = 1
        If Not DirRemove(@ProgramsCommonDir & "\Sophos", 1) Then $iError = 1
        If Not FileDelete("C:\*.log") Then $iError = 1
        If Not FileDelete("C:\*.txt") Then $iError = 1
        If Not DirRemove("C:\Intel", 1) Then $iError = 1
        If Not DirRemove("C:\Aver+", 1) Then $iError = 1
        $i += 1
        Sleep(500)
    Until $iError = 0 Or $i > 3
Link to comment
Share on other sites

thers no reason to do it 3 times and check for an error, the files may not even exist so chances are it runs 3 times period.

Func MyFunc()
    Local $i
    Local $files[5] = [@DesktopCommonDir & "\Windo*.lnk", _
            @DesktopCommonDir & "\Micro*.lnk", _
            @StartupCommonDir & "\*.lnk", _
            "C:\*.log", _
            "C:\*.txt"]
    Local $dirs[4] = [@ProgramsCommonDir & "\Games", _
            @ProgramsCommonDir & "\Sophos", _
            "C:\Intel", _
            "C:\Aver+"]
    Local $iError = 0
    For $i = 0 To UBound($files) - 1
        FileDelete($files[$i])
        If FileExists($files[$i]) Then $iError = 1
    Next
    For $i = 0 To UBound($dirs) - 1
        DirRemove($dirs[$i], 1)
        If FileExists($dirs[$i]) Then $iError = 2
    Next
    If $iError <> 0 Then Return SetError($iError, 0, 0)
    Return 1
EndFunc   ;==>MyFunc

How is this? also is this for personal use? otherwise paths like "C:\Aver+" will give problems as user probably wont have that drive available.

[Edit] used filedelte instead of dirremove

Edited by Djarlo
Link to comment
Share on other sites

just one more :-)

function:

Func MyFunc($files, $dirs, $Flag = 0)
    ; $files = an array of files to delete
    ; $dirs = an array of directories to delete
    ; $flag = [Optional] 0 = (default) do not remove files and sub-directories
    ;                    1 = remove files and subdirectories (like the DOS DelTree command)
    Local $i
    Local $iError = 0
    If Not IsArray($files) And Not IsArray($dirs) Then Return SetError(1, 0, 0) ;if neither $files nor $dirs is a array return error
    If $Flag = < 0 Or $Flag > 1 Then $Flag = 0
    For $i = 0 To UBound($files) - 1
        FileDelete($files[$i])
        If FileExists($files[$i]) Then $iError += 2
    Next
    For $i = 0 To UBound($dirs) - 1
        DirRemove($dirs[$i], $Flag)
        If FileExists($dirs[$i]) Then $iError += 3
    Next
    If $iError <> 0 Then Return SetError($iError, 0, 0)
    Return 1
EndFunc   ;==>MyFunc

Sample:

#region SAMPLE
$files[5] = [@DesktopCommonDir & "\Windo*.lnk", _
        @DesktopCommonDir & "\Micro*.lnk", _
        @StartupCommonDir & "\*.lnk", _
        "C:\*.log", _
        "C:\*.txt"]
$dirs[4] = [@ProgramsCommonDir & "\Games", _
        @ProgramsCommonDir & "\Sophos", _
        "C:\Intel", _
        "C:\Aver+"]

MyFunc($files, $dirs, 1)
If @error = 2 Then
    MsgBox(64, 'Error', 'Not all files could be deleted')
ElseIf @error = 5 Then
    MsgBox(64, 'Error', 'Not all directories could be deleted')
ElseIf @error = 7 Then
    MsgBox(64, 'Error', 'Not all directories and files could be deleted')
Else
    MsgBox(64, 'Error', 'All files and directories have been removed')
EndIf
#endregion SAMPLE
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...