[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 999
  • Last Modified:

MID function not working in vbscript as it does in VB

Experts,

I have a code module that works perfectly in VB, but not in VBScript.  The Mid() function fails with the error "Type Mismatch".

The logic goes like this:
Open a text file, read each line, if a certain character combination is found, then change a character on the previous line.  The error comes when trying to change the character.

See the line within the for loop.

This works perfectly when ran from VB, just not in VBScript.

Any ideas?  I'm really under the gun to get this done, so I have valued it accordingly.


Function Main()

  Dim fso
  Set fso = CreateObject("Scripting.FileSystemObject")
  Dim folder, files, file
  Dim SourceDir, ArchiveDir, Filename
  SourceDir = DTSGlobalVariables("SourceDir").Value
  ArchiveDir = DTSGlobalVariables("ArchiveDir").Value
  Dim SourceFile, SourceFileName, DestinationFile, DestinationFileName, v, aFileLines
  Dim i


  Set folder = fso.GetFolder(SourceDir)
  Set files = folder.files
  For Each file In files
    If Mid(file.Name, 1, 6) = "UPS947" Then
      Filename = file.Name
      SourceFileName = SourceDir & "\" & file.Name
      Set SourceFile = fso.OpenTextFile(SourceFileName)
      v = SourceFile.ReadAll
      aFileLines = Split(v, vbCrLf)
      For i = 0 To UBound(aFileLines)
        If Mid(aFileLines(i), 162, 2) = "WD" or Mid(aFileLines(i), 162, 2) = "DM" Then Mid(aFileLines(i - 1), 200, 1) = "Z"
      Next
      DestinationFileName = SourceDir & "\" & Mid(Filename, 1, 7) & "DTS_Processed_" & Mid(Filename, 8, (Len(Filename) - 7))
      Set DestinationFile = fso.CreateTextFile(DestinationFileName)
      For i = 0 To UBound(aFileLines)
        DestinationFile.Write (aFileLines(i)) & vbCrLf
      Next
      fso.MoveFile SourceFileName, DestinationFileName
    End If
  Next
     
End Function
0
KenMSchwartz
Asked:
KenMSchwartz
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
       If Mid(aFileLines(i), 162, 2) = "WD" or Mid(aFileLines(i), 162, 2) = "DM" Then Mid(aFileLines(i - 1), 200, 1) = "Z"
will need to read:

        If Mid(aFileLines(i), 162, 2) = "WD" or Mid(aFileLines(i), 162, 2) = "DM" Then
            aFileLines(i - 1) = left(aFileLines(i - 1), 199) & "Z" & Mid(aFileLines(i - 1), 201)
        end if
0
 
KenMSchwartzAuthor Commented:
AngelIII,

Worked like a charm!  Thanks.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now