Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account



Photo

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

- - - - -

  • Please log in to reply
7 replies to this topic

#1
Falcor

Falcor

    Newbie

  • Member
  • 13 posts
  • Joined 28-February 13
  • OS:Windows 8 x64
  • Country: Country Flag
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?


How to remove advertisement from MSFN

#2
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,419 posts
  • Joined 02-August 03
  • OS:none specified
  • Country: Country Flag
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



GunSmokingMan



#3
Falcor

Falcor

    Newbie

  • Member
  • 13 posts
  • Joined 28-February 13
  • OS:Windows 8 x64
  • Country: Country Flag

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.

#4
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,419 posts
  • Joined 02-August 03
  • OS:none specified
  • Country: Country Flag

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.


GunSmokingMan



#5
Falcor

Falcor

    Newbie

  • Member
  • 13 posts
  • Joined 28-February 13
  • OS:Windows 8 x64
  • Country: Country Flag

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.

#6
Falcor

Falcor

    Newbie

  • Member
  • 13 posts
  • Joined 28-February 13
  • OS:Windows 8 x64
  • Country: Country Flag
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, 04 March 2013 - 10:38 AM.


#7
bphlpt

bphlpt

    MSFN Addict

  • Member
  • PipPipPipPipPipPipPip
  • 1,798 posts
  • Joined 12-May 07
  • OS:none specified
  • Country: Country Flag
So others won't make the same mistake, why don't you post the corrected script which now works for you.

Cheers and Regards

Posted Image


#8
Falcor

Falcor

    Newbie

  • Member
  • 13 posts
  • Joined 28-February 13
  • OS:Windows 8 x64
  • Country: Country Flag

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:




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users