WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Samples from Users

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Locate and Validate Credit Card Numbers Using RegExp

 Keywords:  Locate Find Valid Validate Credit Card Numbers Regular Expression RegExp Wildcard File Searcher Folder Finder

; (c)Detlev Dalitz.20091207.

;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfAskLine (strAL_Title, strAL_Prompt, strAL_Default, intAL_Format)
Return AskLine (strAL_Title, strAL_Prompt, strAL_Default, intAL_Format)
:CANCEL
Return "" ; Return empty string if dialog has been cancelled resp. closed by escape key.
;..........................................................................................................................................
; strAL_Title   = "" ; Title of the dialog box.
; strAL_Prompt  = "" ; Question to be put to the user.
; strAL_Default = "" ; Default answer.
; intAL_Format  = 0  ; Format as ANSI string.
; intAL_Format  = 1  ; Format as Unicode string.
; intAL_Format  = 2  ; Format as hex string, with four hex bytes per Unicode character.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfIsValidCreditCard (strCardNumber)
objRegExp = ObjectCreate ("VBScript.RegExp")
objRegExp.Global = @TRUE
objRegExp.Pattern = "\d{4}-\d{4}-\d{4}-\d{4}|\d{16}|\d{4} \d{4} \d{4} \d{4}"
If !objRegExp.Test(strCardNumber) Then Return @FALSE
objRegExp.Pattern = "[^\d]"
strCardNumber = objRegExp.Replace(strCardNumber, "")
intSum = 0
blnAlternate = @FALSE
For intPos = 16 To 1 By -1
   intNum = StrSub (strCardNumber, intPos, 1)
   If blnAlternate
      intNum = intNum * 2
      If intNum > 9 Then intNum = 1 + intNum mod 10
   EndIf
   intSum = intSum + intNum
   blnAlternate = !blnAlternate
Next
Return !(intSum mod 10)
;..........................................................................................................................................
; This UDF udfIsValidCreditCard validates a Credit Card Number.
; http://en.wikipedia.org/wiki/Luhn_algorithm
;
; The input string must be formatted like one of the following formats:
; "nnnn-nnnn-nnnn-nnnn", "nnnn nnnn nnnn nnnn", "nnnnnnnnnnnnnnnn"
; as defined by the RegExp pattern.
;
; The function returns the boolean value @TRUE resp. @FALSE depending on the validation.
;
; Detlev Dalitz.20091207.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfSearchCreditCardPattern (strFilename)
strFiletext = FileGet (strFilename)
If strFiletext == "" Then Return ""
objRegExp = ObjectCreate ("VBScript.RegExp")
objRegExp.Global = @TRUE
objRegExp.IgnoreCase = @FALSE
objRegExp.MultiLine = @FALSE
objRegExp.Pattern = "(\d{4}-\d{4}-\d{4}-\d{4}|\d{16}|\d{4} \d{4} \d{4} \d{4})"
If !objRegExp.Test(strFiletext) Then Return ""
strMatches = ""
objMatches = objRegExp.Execute(strFiletext)
ForEach objMatch In objMatches
   strMatches = strMatches : @LF : objMatch.Value : @TAB : objMatch.FirstIndex : @TAB : '"' : strFilename : '"'
Next
Return StrSub (strMatches, 2, -1)
;..........................................................................................................................................
; Structure of output item per single match:
; Credit Card number @TAB Position in File @TAB Filepath
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Main.

strAL_Title = "Create list of Credit Card numbers" ; Title of the dialog box.
strAL_Prompt = "Input starting directory" ; Question to be put to the user.
strAL_Default = "J:\" ; Default answer.
intAL_Format = 0  ; Format as ANSI string.
strStartDirectory = udfAskLine (strAL_Title, strAL_Prompt, strAL_Default, intAL_Format)
If strStartDirectory == "" Then Goto CANCEL

AddExtender ("wwfaf44i.dll")
; Initialize flags for readability.
intFFNoFlags = 0     ; No Flags.
intFFHidden = 1      ; Include hidden files.
intFFSystem = 2      ; Include system files.
intFFFolders = 4     ; Inspect  subfolder names also.
intFFFindFolders = 8 ; Only inspect subfolder names not file names.
intFFRecurse = 16    ; Recurse through subfolders.
intFFNoPathInfo = 32 ; Do not return path information to files or folders found.
intFFNoRedirect = 64 ; Do not turn off file redirection for x64 windows.

; Open a "File and Folder" search handle.
hdlFF = fafOpen (strStartDirectory, "*.txt", intFFRecurse)

; Perform the search.
strCCMatchesList = ""
While @TRUE
   strFilename = fafFind (hdlFF)
   If strFilename == "" Then Break
   ;   Pause ("Debug", strFilename)
   strMatches = udfSearchCreditCardPattern (strFilename)
   If strMatches != "" Then strCCMatchesList = strCCMatchesList : @LF : strMatches
EndWhile

; Close the search handle.
intResult = fafClose (hdlFF)

; Write list of possible Credit Card number matches to file.
strCCMatchesFile = DirScript () : "CCmatches.txt"
strCCMatchesList = StrSub (strCCMatchesList, 2, -1)
strCCMatchesList = StrReplace (strCCMatchesList, @LF, @CRLF)
FilePut (strCCMatchesFile, strCCMatchesList)
Run (strCCMatchesFile, "")

; Validate Credit Card number matches.
strCCValidList = ""
intCount = ItemCount (strCCMatchesList, @LF)
For intItem = 1 To intCount
   strCCMatch = ItemExtract (intItem, strCCMatchesList, @LF)
   strCCMatch = ItemExtract (1, strCCMatch, @TAB)
   If udfIsValidCreditCard (strCCMatch) Then strCCValidList = strCCValidList : @LF : strCCMatch
Next

; Write list of valid Credit Card numbers to file.
strCCValidFile = DirScript () : "CCValid.txt"
strCCValidList = StrSub (strCCValidList, 2, -1)
strCCValidList = StrReplace (strCCValidList, @LF, @CRLF)
FilePut (strCCValidFile, strCCValidList)
Run (strCCValidFile, "")

:CANCEL
Exit

Article ID:   W18173
Filename:   Locate and Validate Credit Card Numbers Using RegExp.txt
File Created: 2009:12:07:09:53:20
Last Updated: 2009:12:07:09:53:20