Jump to content

FilesRequired UDF


rhg
 Share

Recommended Posts

My first UDF; simple and self-explanatory I think. Hope you guys like it.

The idea for me was to run this routine and on the return of 0 (int)

display a MsgBox with the missing file(s) and trigger an Exit.

Updated : 2006-02-08 14:30

Arguments:

$fileDir is the directory you want to search for the required file(s)

$fileStr is either A) a single file name or B) a delimited string of

file names

$del is the delimiter one is using to delimit the file names in $fileStr; Default is comma.

Returns:

1 on Success : 0 on Failure

Notes:

If the function doesn't find the file(s), then it pops-up an error dialog with

the list of missing files and only the missing files.

Func FilesRequired($fileDir,$fileStr,$del=",")
    Local $f, $tmpx=""
    If StringInStr($fileStr,$del)>0 Then
        Local $cs = StringSplit($fileStr,$del)
        Local $x
        For $x = 1 To $cs[0]
            $f = $fileDir&$cs[$x]
            If Not FileExists($f) Then
                $tmpx = $tmpx &"* "&$f&@CRLF
            EndIf
        Next
    Else
        $f = $fileDir&$fileStr
        If Not FileExists($f) Then
            $tmpx = $tmpx &"* "&$f&@CRLF
        EndIf
    EndIf
    If StringLen($tmpx)>0 Then
        $f = "The Following Required Files "
        $f = $f & "are Missing or Corrupt:"
        $f = $f & @CRLF&@CRLF&$tmpx&@CRLF
        $f = $f & "Exiting With Errors..."
        MsgBox(48,"Critical Error",$f)
        Return 0
    Else
        Return 1
    EndIf
EndFunc

Example Usage

Dim $mypath = @ScriptDir&"\"
Dim $required = "myfile1.ini,myfile2.exe,myfile3.jpg"

If FilesRequired($mypath,$required,",")=1 Then
    MsgBox(48,"Yay","All Required Files There")
; Continue processing here
EndIf
Exit
Edited by rhg
Link to comment
Share on other sites

maybe have it so you can use any delimeter you choose? not like it's that hard to change but maybe just as an option to make it more versatile. I'm pretty sure the

If Not FileExists($f)=1 Then

Is the same as:

If Not FileExists($f) Then

and

If FileExists($f)<>1 Then

Just some ideas. Here's what I'd do... No offense or anything.

Func FilesRequired($fileDir,$fileStr,$del=",")
    Local $f, $tmpx=""
    If StringInStr($fileStr,",")>0 Then
        Local $cs = StringSplit($fileStr,$del)
        Local $x
        For $x = 1 To $cs[0]
            $f = $fileDir&$cs[$x]
            If Not FileExists($f) Then
                $tmpx = $tmpx &"* "&$f&@CRLF
            EndIf
        Next
    Else
        $f = $fileDir&$fileStr
        If Not FileExists($f) Then
            $tmpx = $tmpx &"* "&$f&@CRLF
        EndIf
    EndIf
    If StringLen($tmpx)>0 Then
        $f = "The Following Required Files"
        $f = $f & "are Missing or Corrupt:"
        $f = $f & @CRLF&@CRLF&$tmpx&@CRLF
        $f = $f & "Exiting With Errors..."
        MsgBox(48,"Critical Error",$f)
        Return 0
    Else
        Return 1
    EndIf
EndFunc
Link to comment
Share on other sites

Thanks Nuffilein805 for the feedback.

Thinking about your suggestion...what do you propose exactly? Like the function uses the data in $fileStr to perform a HDD search and returns the path for each file? Or just returns 1 for 'Yes I'm Here' or 0 'No I'm Not', and just handle user notification 'on 0'?

Not sure what you want it to do exactly but I am up to improving it. :lmao: Just let me know.

Link to comment
Share on other sites

Thanks Oxin8 for the feedback. Think I was replying when you posted but thats a good idea and no offense taken. I'll update the function after replying.

If Not FileExists($f) Then

Isn't this a boolean expression? Wasn't sure how AI handles -1/1/0 in boolean expressions. Is the same as VBScript?

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...