Falcor Posted March 3, 2013 Share Posted March 3, 2013 For some reason, I can't get this to work. Basically, I am building in error checking prior to running other functions in an HTA app. I have managed to overcome all odds and have automated an extremely arduous process (processing EDI), but I would also like to error check each EDI file for specific errors. If an error is detected, I want it to tell me which file has the error(s) and how many there are.What I have: Dim Char_Date, Char_DateTime, objFileSys, objFolder, objFile, objRead, objWrite, objRegEx, strFile, intLength, strEnd Dim sContents, sDir, sFile, nFile, iMatch, colMatches, strFileName Dim EDIDir, EDICount, EXT EXT = "edi" EDIDir = "C:\Users\Mike\Google Drive\- Projects\Test Scripts\HTA\TEST EDI FILES" EDICount = 0 Set objFileSys = CreateObject("Scripting.FileSystemObject") Set objFolder = objFileSys.GetFolder(EDIDir) For Each objFile In objFolder.Files If LCase((objFileSys.GetExtensionName(objFile))) = LCase(EXT) Then EDICount = EDICount + 1 End If Next Msgbox "Detected EDI Files: " & EDICount'# Error check EDI files. Const ForAppending = 8 Const ForReading = 1 Const ForWriting = 2 For Each objFile In objFolder.Files If LCase((objFileSys.GetExtensionName(objFile))) = LCase(EXT) Then Set strFileName = objFileSys.GetFileName(objFile) Set objFile = objFileSys.OpenTextFile(objFile) sContents = objFile.ReadAll objFile.Close Set objRegEx = CreateObject("VBScript.RegExp") objRegEx.IgnoreCase = False objRegEx.Global = True objRegEx.Pattern = "19\*\*" Set colMatches = objRegEx.Execute(sContents) If colMatches.Count >= 1 Then Msgbox colMatches.Count & " errors in " & strFileName & "." Else Msgbox "0 errors found in " & strFileName & "." End If End If NextMsgbox "Error checking complete."The first part of the script runs just fine. It tells me how many EDI files I have in a specific directory. When it gets to the error checking, though, it doesn't work. From all the examples I have seen...it should. I keep getting an error that states: "Object required: '[string: MYFILENAME.EDI"]'Any ideas? Link to comment Share on other sites More sharing options...
gunsmokingman Posted March 3, 2013 Share Posted March 3, 2013 1:\ LCase(EXT) not needed because you have the variable in lower case already EXT = "edi"2:\ Set strFileName = objFileSys.GetFileName(objFile) you have a variable but do not use it.3:\Set objFile = objFileSys.OpenTextFile(objFile) where are the 1, 2, 8 Set objFile = objFileSys.OpenTextFile(objFile,1) would open file for reading4:\ Suggest that you run your script second loop to see what files are being process before checkingfor errors.Example For Each objFile In objFolder.Files If LCase((objFileSys.GetExtensionName(objFile))) = LCase(EXT) Then wscript.echo ObjFile.Path End If Next Link to comment Share on other sites More sharing options...
Falcor Posted March 4, 2013 Author Share Posted March 4, 2013 1:\ LCase(EXT) not needed because you have the variable in lower case already EXT = "edi"This is in case of typos. I dont want to get an error simply because of case-sensitivity.2:\ Set strFileName = objFileSys.GetFileName(objFile) you have a variable but do not use it.See these lines:If colMatches.Count >= 1 Then Msgbox colMatches.Count & " errors in " & strFileName & "." Else Msgbox "0 errors found in " & strFileName & "." End If3:\Set objFile = objFileSys.OpenTextFile(objFile) where are the 1, 2, 8 Set objFile = objFileSys.OpenTextFile(objFile,1) would open file for readingThese are used after the error checking is completed by a concatenation script.4:\ Suggest that you run your script second loop to see what files are being process before checkingfor errors.Example For Each objFile In objFolder.Files If LCase((objFileSys.GetExtensionName(objFile))) = LCase(EXT) Then wscript.echo ObjFile.Path End If NextWhy would I need a second loop? All of the examples I have seen should allow for the script to report which file is currently being worked on, but it simply isn't working for me. Ideally, I would combine both sections of the script and have it count and error check with the same loop. I'll get to that after the entire script is finished. Keeping them separated allows me to error check the script more easily. Link to comment Share on other sites More sharing options...
gunsmokingman Posted March 4, 2013 Share Posted March 4, 2013 This is in case of typos. I dont want to get an error simply because of case-sensitivity.So what you saying is that you would not remember that your variable was all lower case, what yousaid might of made sense if it was some kind of user input and case sensitivity.Why would I need a second loop?You already have 2 loops in your script, you should only need one.Loop 1 For Each objFile In objFolder.Files If LCase((objFileSys.GetExtensionName(objFile))) = LCase(EXT) Then EDICount = EDICount + 1 End If Next Msgbox "Detected EDI Files: " & EDICountLoop 2 For Each objFile In objFolder.Files If LCase((objFileSys.GetExtensionName(objFile))) = LCase(EXT) Then Set strFileName = objFileSys.GetFileName(objFile) Set objFile = objFileSys.OpenTextFile(objFile) sContents = objFile.ReadAll objFile.Close Set objRegEx = CreateObject("VBScript.RegExp") objRegEx.IgnoreCase = False objRegEx.Global = True objRegEx.Pattern = "19\*\*" Set colMatches = objRegEx.Execute(sContents) If colMatches.Count >= 1 Then Msgbox colMatches.Count & " errors in " & strFileName & "." Else Msgbox "0 errors found in " & strFileName & "." End If End If NextThe first part of the script runs just fine. It tells me how many EDI files I have in a specific directory. When it gets to the error checking, though, it doesn't work. From all the examples I have seen...it should. I keep getting an error that states: "Object required: '[string: MYFILENAME.EDI"]'This is why I said to rewrite your second loop to just show what files are being process, but it your script so it must work. Link to comment Share on other sites More sharing options...
Falcor Posted March 4, 2013 Author Share Posted March 4, 2013 This is in case of typos. I dont want to get an error simply because of case-sensitivity.So what you saying is that you would not remember that your variable was all lower case, what yousaid might of made sense if it was some kind of user input and case sensitivity.Why would I need a second loop?You already have 2 loops in your script, you should only need one.Loop 1 For Each objFile In objFolder.Files If LCase((objFileSys.GetExtensionName(objFile))) = LCase(EXT) Then EDICount = EDICount + 1 End If Next Msgbox "Detected EDI Files: " & EDICountLoop 2 For Each objFile In objFolder.Files If LCase((objFileSys.GetExtensionName(objFile))) = LCase(EXT) Then Set strFileName = objFileSys.GetFileName(objFile) Set objFile = objFileSys.OpenTextFile(objFile) sContents = objFile.ReadAll objFile.Close Set objRegEx = CreateObject("VBScript.RegExp") objRegEx.IgnoreCase = False objRegEx.Global = True objRegEx.Pattern = "19\*\*" Set colMatches = objRegEx.Execute(sContents) If colMatches.Count >= 1 Then Msgbox colMatches.Count & " errors in " & strFileName & "." Else Msgbox "0 errors found in " & strFileName & "." End If End If NextThe first part of the script runs just fine. It tells me how many EDI files I have in a specific directory. When it gets to the error checking, though, it doesn't work. From all the examples I have seen...it should. I keep getting an error that states: "Object required: '[string: MYFILENAME.EDI"]'This is why I said to rewrite your second loop to just show what files are being process, but it your script so it must work.OK - I tried it your way, but I still get an error: "Object doesn't support this property or method: 'Path'"I'm not too sure what the issue is at this point. Link to comment Share on other sites More sharing options...
Falcor Posted March 4, 2013 Author Share Posted March 4, 2013 (edited) I am such an id***...I just tried a different method and it worked without issue. Sorry for the hassle.Note to Self: There is a big difference betweenSet VARIABLE = VALUEandVARIABLE = VALUE Edited March 4, 2013 by Falcor Link to comment Share on other sites More sharing options...
bphlpt Posted March 5, 2013 Share Posted March 5, 2013 So others won't make the same mistake, why don't you post the corrected script which now works for you.Cheers and Regards Link to comment Share on other sites More sharing options...
Falcor Posted March 6, 2013 Author Share Posted March 6, 2013 So others won't make the same mistake, why don't you post the corrected script which now works for you.Cheers and RegardsConsider it done!Dim EXT, EDIDir, EDILog, EDICount, ErrorCount, EDIHeadCount, objFolder, objLogFile, objFile, strFileName Dim objRegEx, intConc, objRead, objWrite, strFile, intLength, strEnd, ObjOutputFile Dim sContents, sContents2, sContents3, sDir, sFile, nFile, iMatch, colMatches, colMatches2 Dim Char_Date, Char_DateTime Const ForAppending = 8 Const ForReading = 1 Const ForWriting = 2'# Set log variable, count variables to 0, and paths. EXT = "edi" EDIDir = "C:\Users\Mike\Google Drive\- Projects\Test Scripts\HTA\TEST EDI FILES" EDILog = EDIDir & "\EDILog.log" EDICount = 0 ErrorCount = 0 EDIHeadCount = 0 sContents = Null sContents2 = Null sContents3 = Null'# Delete EDILog.log if it exists. Set objFolder = objFileSys.GetFolder(EDIDir) If (objFileSys.FileExists("C:\Users\Mike\Google Drive\- Projects\Test Scripts\HTA\TEST EDI FILES\EDILog.log")) Then objFileSys.DeleteFile "C:\Users\Mike\Google Drive\- Projects\Test Scripts\HTA\TEST EDI FILES\EDILog.log" Else End If'# Create new EDILog.log file. Set objLogFile = objFileSys.OpenTextFile(EDILog, ForAppending, TRUE)'# Count number of EDI files, display results, and add to log. For Each objFile In objFolder.Files If LCase((objFileSys.GetExtensionName(objFile))) = EXT Then EDICount = EDICount + 1 End If Next Msgbox "Detected EDI Files: " & EDICount objLogFile.WriteLine "Detected EDI Files: " & EDICount'# Error check EDI files, display results if errors found, and output to log file. (!! means error | X means OK) For Each objFile In objFolder.Files If LCase((objFileSys.GetExtensionName(objFile))) = EXT Then strFileName = objFile.Name Set objFile = objFileSys.OpenTextFile(objFile) sContents = objFile.ReadAll objFile.Close Set objRegEx = CreateObject("VBScript.RegExp") objRegEx.IgnoreCase = False objRegEx.Global = True objRegEx.Pattern = "19\*\*" Set colMatches = objRegEx.Execute(sContents) If colMatches.Count >= 1 Then Msgbox colMatches.Count & " errors in " & strFileName & "!" ErrorCount = ErrorCount + 1 objLogFile.WriteLine "!!" & vbTab & colMatches.Count & " errors in " & strFileName Else objLogFile.WriteLine "X " & vbTab & colMatches.Count & " errors in " & strFileName End If End If Next objLogFile.WriteLine vbCRLF & "Error checking complete." objLogFile.Close'# If there are errors display the log file and quit further action. If ErrorCount >= 1 Then Msgbox "Errors were detected. Please review the log file and correct these errors to continue." objShell.run chr(34) & EDILog & chr(34),1,FALSE Else'# If there are no errors prompt to concatenate. intConc = Msgbox("No errors detected." & vbCRLF & vbCRLF & "Do you want to concatenate?", vbYesNo, "Concatenate?") End IfAnd then it goes on to my concat script section... Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now