MID function not working in vbscript as it does in VB

Posted on 2006-05-24
Last Modified: 2012-08-13

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"
      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
      fso.MoveFile SourceFileName, DestinationFileName
    End If
End Function
Question by:KenMSchwartz
    LVL 142

    Accepted Solution

           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

    Author Comment


    Worked like a charm!  Thanks.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
    Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
    Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
    Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now