Navarre_EDI
asked on
No hard code file name in program pass it at prompt
I would like to pass the program name and file to process instead of hard coding it in the program.
Program Name:Costco_ESD.vbs
example at dos prompt do Costco_ESD.vbs flatfile1
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
' Create the regular expression object
Set objRegEx = CreateObject("VBScript.Reg Exp")
' Sets or returns a Boolean value that indicates if a pattern should match all occurrences in an entire search string or just the first one
' We set it to True to return a collection containing all matches
objRegEx.Global = True
' Input file
strEDI = "C:\Program Files\TIE Commerce\eVision\Coscto_ES D\single_f iles\Split _20110324_ 15265623_0 01.asingle "
' Output file
strOutputFile = "Outbound_001.txt"
' This is the regular expression pattern that we must find in the file. If it is not found, blnTriggerFound is left at false, and no data is output.
strTrigger = "[\r\n]PID.*ESD.*"
' This array holds each of the regular expression patterns to find in the file.
' [\r\n] matches a line feed, so, to match a string at the beginning of a line, we use this first
' .* returns multiple matches of any character, which is basically just anything after the specified text
arrDataToRetrieve = Array("[\r\n]BEG.*", "[\r\n]DTM.*", "[\r\n]N1.*ST.*", "[\r\n]N3.*", "[\r\n]N4.*", "[\r\n]PER.*", "[\r\n].*PO1.*[\r\n]PID.*E SD.*")
' Set output message to an empty string
strMessage = ""
' Set the flag for the trigger to false
blnTriggerFound = False
' Open the input file
Const intForReading = 1
Set objEDI = objFSO.OpenTextFile(strEDI , intForReading, False)
' Read all of the file into a string variable to perform the regular expression searches on
strAllText = objEDI.ReadAll
' Close the input file
objEDI.Close
' Set the regular expression pattern to look for
objRegEx.Pattern = strTrigger
' Perform the search on strAllText, returning all matches to the colMatches collection
Set colMatches = objRegEx.Execute(strAllTex t)
' Check in the trigger was found, based on whether the count of mathces is greater than zero
If colMatches.Count > 0 Then blnTriggerFound = True
' Enumerate through each element in the arrDataToRetrieve array, so search for each pattern
For Each strData In arrDataToRetrieve
' Set the pattern
objRegEx.Pattern = strData
' Search strAllText for each pattern
Set colMatches = objRegEx.Execute(strAllTex t)
If colMatches.Count > 0 Then
' Enumerate through the returned matches
For Each strMatch In colMatches
' Trim off the carriage returns from the matches (because we used /r/n)
If Left(strMatch, 2) = VbCrLf Then strMatch = Mid(strMatch, 2)
If Right(strMatch, 2) = VbCrLf Then strMatch = Left(strMatch, Len(strMatch) - 2)
' Append the match found to our output string
If strMessage = "" Then
strMessage = strMatch
Else
strMessage = strMessage & strMatch
End If
Next
End If
Next
' Trim the carriage returns and line feeds from the final output string
If Left(strMessage, 1) = VbLf Then strMessage = Mid(strMessage, 2)
If Right(strMessage, 1) = VbCr Then strMessage = Left(strMessage, Len(strMessage) - 1)
' Check whether the trigger was found, and only if it was found, output the data to the output file
If blnTriggerFound = True Then
' Create the new, empty output file
Set objOutput = objFSO.CreateTextFile(strO utputFile, True)
' Write the message
objOutput.Write strMessage
' Close the output file
objOutput.Close
MsgBox "Data has been output to " & strOutputFile
Else
'MsgBox "Not found"
End If
Program Name:Costco_ESD.vbs
example at dos prompt do Costco_ESD.vbs flatfile1
Set objFSO = CreateObject("Scripting.Fi
' Create the regular expression object
Set objRegEx = CreateObject("VBScript.Reg
' Sets or returns a Boolean value that indicates if a pattern should match all occurrences in an entire search string or just the first one
' We set it to True to return a collection containing all matches
objRegEx.Global = True
' Input file
strEDI = "C:\Program Files\TIE Commerce\eVision\Coscto_ES
' Output file
strOutputFile = "Outbound_001.txt"
' This is the regular expression pattern that we must find in the file. If it is not found, blnTriggerFound is left at false, and no data is output.
strTrigger = "[\r\n]PID.*ESD.*"
' This array holds each of the regular expression patterns to find in the file.
' [\r\n] matches a line feed, so, to match a string at the beginning of a line, we use this first
' .* returns multiple matches of any character, which is basically just anything after the specified text
arrDataToRetrieve = Array("[\r\n]BEG.*", "[\r\n]DTM.*", "[\r\n]N1.*ST.*", "[\r\n]N3.*", "[\r\n]N4.*", "[\r\n]PER.*", "[\r\n].*PO1.*[\r\n]PID.*E
' Set output message to an empty string
strMessage = ""
' Set the flag for the trigger to false
blnTriggerFound = False
' Open the input file
Const intForReading = 1
Set objEDI = objFSO.OpenTextFile(strEDI
' Read all of the file into a string variable to perform the regular expression searches on
strAllText = objEDI.ReadAll
' Close the input file
objEDI.Close
' Set the regular expression pattern to look for
objRegEx.Pattern = strTrigger
' Perform the search on strAllText, returning all matches to the colMatches collection
Set colMatches = objRegEx.Execute(strAllTex
' Check in the trigger was found, based on whether the count of mathces is greater than zero
If colMatches.Count > 0 Then blnTriggerFound = True
' Enumerate through each element in the arrDataToRetrieve array, so search for each pattern
For Each strData In arrDataToRetrieve
' Set the pattern
objRegEx.Pattern = strData
' Search strAllText for each pattern
Set colMatches = objRegEx.Execute(strAllTex
If colMatches.Count > 0 Then
' Enumerate through the returned matches
For Each strMatch In colMatches
' Trim off the carriage returns from the matches (because we used /r/n)
If Left(strMatch, 2) = VbCrLf Then strMatch = Mid(strMatch, 2)
If Right(strMatch, 2) = VbCrLf Then strMatch = Left(strMatch, Len(strMatch) - 2)
' Append the match found to our output string
If strMessage = "" Then
strMessage = strMatch
Else
strMessage = strMessage & strMatch
End If
Next
End If
Next
' Trim the carriage returns and line feeds from the final output string
If Left(strMessage, 1) = VbLf Then strMessage = Mid(strMessage, 2)
If Right(strMessage, 1) = VbCr Then strMessage = Left(strMessage, Len(strMessage) - 1)
' Check whether the trigger was found, and only if it was found, output the data to the output file
If blnTriggerFound = True Then
' Create the new, empty output file
Set objOutput = objFSO.CreateTextFile(strO
' Write the message
objOutput.Write strMessage
' Close the output file
objOutput.Close
MsgBox "Data has been output to " & strOutputFile
Else
'MsgBox "Not found"
End If
You can use
strEDI = WScript.Arguments.Item(0)
to get the first value. For the next value use Item(1) and so forth.
strEDI = WScript.Arguments.Item(0)
to get the first value. For the next value use Item(1) and so forth.
Yup and just use a check on wscript.arguments.count to check it is completed, i.e. things like this:
If Wscript.Arguments.Count < 2 Then
wscript.echo "You have not supplied two options required, only " & Wscript.Arguments.Count & vbcrlf & vbcrlf & "Usage: xxx.vbs y y"
ELSE
wscript.echo "Argument 1 is " & Wscript.Arguments(0) & vbcrlf & "Argument 2 is " & Wscript.Arguments(1)
wscript.echo "rest of script"
End if
Steve
If Wscript.Arguments.Count < 2 Then
wscript.echo "You have not supplied two options required, only " & Wscript.Arguments.Count & vbcrlf & vbcrlf & "Usage: xxx.vbs y y"
ELSE
wscript.echo "Argument 1 is " & Wscript.Arguments(0) & vbcrlf & "Argument 2 is " & Wscript.Arguments(1)
wscript.echo "rest of script"
End if
Steve
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Open in new window
Even more information is available here: http://technet.microsoft.com/en-us/library/ee156618.aspx
-Matt-