MSFN Forum: Returning a filename from within a For Each statement... - MSFN Forum

Jump to content


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Returning a filename from within a For Each statement... Rate Topic: -----

#1 User is offline   Falcor 

  • Newbie
  • Group: Members
  • Posts: 10
  • Joined: 28-February 13
  • OS:Windows 8 x64
  • Country: Country Flag

Posted 03 March 2013 - 04:11 AM

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?


#2 User is offline   gunsmokingman 

  • MSFN Master
  • Group: Super Moderator
  • Posts: 2,356
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 03 March 2013 - 01:09 PM

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



#3 User is offline   Falcor 

  • Newbie
  • Group: Members
  • Posts: 10
  • Joined: 28-February 13
  • OS:Windows 8 x64
  • Country: Country Flag

Posted 03 March 2013 - 09:56 PM

View Postgunsmokingman, on 03 March 2013 - 01:09 PM, said:

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.

View Postgunsmokingman, on 03 March 2013 - 01:09 PM, said:

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


View Postgunsmokingman, on 03 March 2013 - 01:09 PM, said:

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.

View Postgunsmokingman, on 03 March 2013 - 01:09 PM, said:

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 User is offline   gunsmokingman 

  • MSFN Master
  • Group: Super Moderator
  • Posts: 2,356
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 04 March 2013 - 02:14 AM

Quote

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.

Quote

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



Quote

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.

#5 User is offline   Falcor 

  • Newbie
  • Group: Members
  • Posts: 10
  • Joined: 28-February 13
  • OS:Windows 8 x64
  • Country: Country Flag

Posted 04 March 2013 - 10:13 AM

View Postgunsmokingman, on 04 March 2013 - 02:14 AM, said:

Quote

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.

Quote

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



Quote

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 User is offline   Falcor 

  • Newbie
  • Group: Members
  • Posts: 10
  • Joined: 28-February 13
  • OS:Windows 8 x64
  • Country: Country Flag

Posted 04 March 2013 - 10:37 AM

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

This post has been edited by Falcor: 04 March 2013 - 10:38 AM


#7 User is online   bphlpt 

  • MSFN Expert
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 1,122
  • Joined: 12-May 07

Posted 05 March 2013 - 04:22 AM

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

Cheers and Regards

#8 User is offline   Falcor 

  • Newbie
  • Group: Members
  • Posts: 10
  • Joined: 28-February 13
  • OS:Windows 8 x64
  • Country: Country Flag

Posted 05 March 2013 - 06:41 PM

View Postbphlpt, on 05 March 2013 - 04:22 AM, said:

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:

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2013 msfn.org
Privacy Policy