Jump to content

Returning a filename from within a For Each statement...


Recommended Posts

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
Next

Msgbox "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


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 reading

4:\ Suggest that you run your script second loop to see what files are being process before checking

for 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

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 If

3:\Set objFile = objFileSys.OpenTextFile(objFile) where are the 1, 2, 8

Set objFile = objFileSys.OpenTextFile(objFile,1) would open file for reading

These 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 checking

for errors.

Example


For Each objFile In objFolder.Files
If LCase((objFileSys.GetExtensionName(objFile))) = LCase(EXT) Then
wscript.echo ObjFile.Path
End If
Next

Why 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

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 you

said 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: " & EDICount

Loop 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
Next

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"]'

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

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 you

said 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: " & EDICount

Loop 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
Next

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"]'

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

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 between

Set VARIABLE = VALUE

and

VARIABLE = VALUE

Edited by Falcor
Link to comment
Share on other sites

So others won't make the same mistake, why don't you post the corrected script which now works for you.

Cheers and Regards

Consider 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 If

And then it goes on to my concat script section... :rolleyes:

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
  • Recently Browsing   0 members

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