-1

I've created a VBScript that aims to change folder share names on a server. This is currently running as a logon script for my test users.

I've defined this function:

Private Function intChangeShareName()
   Dim intPoz1, intPoz2, intIndex
   Dim strPom

   intChangeShareName = CONST_NO_CHANGE

   strActions = strActions & strOldNameShare & vbcrlf
  strPom = Trim(strOldNameShare)
  If Left(strPom, 2) <> "\\" then Exit Function

   intPoz1 = inStr(3, strPom, "\")
   intPoz2 = inStr(3, strPom, ".")
   If intPoz2 > 0 Then
      strSrvName = Mid(strPom, 3, intPoz2 - 3)
      strDomName = Mid(strPom, intPoz2 + 1, intPoz1 - intPoz2 - 1)
   Else
     strSrvName = Mid(strPom, 3, intPoz1 - 3)
      strDomName = ""
   End If

   intIndex = 0
  Do while intIndex <= UBound(arrOldSrv)
     If UCase(strSrvName) = UCase(arrOldSrv(intIndex)) Then
        If strDomName = "" Then
           strNewNameSrv = arrNewSrv(intIndex)
           strNewNameDom = ""
        intChangeShareName = CONST_CHANGE
         End If

        If UCase(strDomName) = UCase(arrOldDom(intIndex)) Then
           strNewNameSrv = arrNewSrv(intIndex)
           strNewNameDom = "." & arrNewDom(intIndex)
        intChangeShareName = CONST_CHANGE
         End If
      End If
      intIndex = intIndex + 1
   Loop

   If intChangeShareName = CONST_CHANGE Then
      strNewNameShare = "\\" & strNewNameSrv & strNewNameDom & Mid(strPom, intPoz1)
      strActions = strActions & "*  " & strNewNameShare & vbcrlf
      blRequireLogoff = True
'       Wscript.Echo "a " & strNewNameShare
   End If

End Function

However every time I log in I get the following error: VBScript runtime error - Invalid procedure call or argument: 'Mid' - Code 800A0005

I don't see anything wrong with my Mid function, can someone please help me?

DSKyo
  • 153
  • 3
  • 6
  • 15

1 Answers1

1

All occurrences of MID function in your code follow syntax pattern Mid(string, start[, length]) properly:

Arguments

  • string String expression from which characters are returned. If string contains Null, Null is returned.
  • start Character position in string at which the part to be taken begins. If start is greater than the number of characters in string, Mid returns a zero-length string ("").
  • length Number of characters to return. If omitted or if there are fewer than length characters in the text (including the character at start), all characters from the start position to the end of the string are returned.

Excerpted from the original code snippet, variables renamed merely for clearness:

iPosBackSlash = inStr(3, strPom, "\")
iPosDot       = inStr(3, strPom, ".")
If iPosDot > 0 Then
   strSrvName = Mid(strPom,           3, iPosDot - 3)
   strDomName = Mid(strPom, iPosDot + 1, iPosBackSlash - iPosDot - 1)
Else
   strSrvName = Mid(strPom,           3, iPosBackSlash - 3)
   strDomName = ""
End If
''' and thereinafter '''
strNewNameShare = "\\" & strNewNameSrv & strNewNameDom & Mid(strPom, iPosBackSlash)

On the other side, MID function would fail raising terminating error Invalid procedure call or argument: 'Mid' - Code 800A0005 if start or length are out of range.

Let's consider some valid UNC paths as possible values of strPom variable:

  • \\server\root, \\server\root\sub, \\server.domain\root, \\server.domain\root\sub: the original function would not fail,
  • but for instance \\server\root\sub.folder raises error in question as specified length i.e. iPosBackSlash - iPosDot - 1 results to a negative number.

If (iPosDot > 0) and (iPosDot < iPosBackSlash) Then should resolve the latter issue although someone could find a valid UNC path such that iPosDot - 3 or iPosBackSlash - 3 would lead to the error?

Answer, returned to original variable names, use next improved If command:

If intPoz2 > 0 and (intPoz2 < intPoz1) Then
    ''' etc '''
JosefZ
  • 1,564
  • 1
  • 10
  • 18