Home » questions » VBscript (ASP) Function Problem?

VBscript (ASP) Function Problem?

2006-08-14 11:18:00, Category: Programming & Design
I got my school quiz back and the following problem was stated: Write a function named ValidateFiles() which accepts a string, returns 1 if the string is empty, returns 2 if the string doesn't contain "." , otherwise it returns the file extension. Example: If you pass myFile.txt the function returns txt. How do I go about doing this? Thanks, Chris **NOTE** I don't want Visual Basic Coding, I need ASP coding, with vbscripting. The msg box is not part of this scripting language.

Answers

  1. Martyr2

    On 2006-08-14 14:48:46


    Function ValidateFiles(strFilename) If len(strFilename) = 0 then ValidateFiles = 1 Elseif (Instr(strFilename,".") = 0) then ValidateFiles = 2 Else Dim ext ext = split(strFilename,".") ValidateFiles = ext(1) End if End Function The function above is written in vbscript (asp 3.0) and should work to meet all your requirements. Let me know if you need further help and I will change my answer to help. Good luck!
  2. trainump

    On 2006-08-14 12:43:19


    Well, I know that you can use: Dim strInput strInput = InputBox("Enter string to test:","String Input") If Len(strInput) = 0 Then MsgBox 1, ,"Empty String" ElseIf InStr(strInput, ".") = 0 Then MsgBox 2, ,"String does not contain ." Else MsgBox Right(strInput, 3), ,"File Extension" End If but the file extension is just going to give you the last three characters of the string. Good luck! **EDIT** I guess I overlooked the part about needing to write it as a function: Function ValidateFiles(strInput) If Len(strInput) = 0 Then WScript.Echo 1 Exit Function ElseIf InStr(strInput, ".") = 0 Then WScript.Echo 2 Exit Function Else strArray = Split(strInput,".") WScript.Echo strArray(1) Exit Function End If End Function This will give you the file extension of any length, as long as the string does not contain two periods. **EDIT** I use the WSH to run my VBScripts. You can change "WScript.Echo" to "strOutput =" and try to adapt it like that. Other than that, I'm not sure how else to help you. Sorry.