none
Exception calling "Substring" with "2" argument(s): "Index and length must refer to a location within the string. Parameter name: length" At RRS feed

  • Question

  • Hai All,

    am getting this error while executing the following script.

    Please help me on this..

    Exception calling "Substring" with "2" argument(s): "Index and length must refer to a location within the string. Parameter name: length" At 

    $linecount = Get-Content $filepath 
    foreach ($line in $linecount)
    {
    if ($line.substring(0,2) -eq 'W ' -and $line.Substring(0,3) -ne 'W -'  -and  $line.Substring(0,2) -ne '***'  -and $line.Substring(0,1) -ne 'E' )
    {
    $line | Add-Content $Filepath1
    }

    }

    Thanks & Regards,

    Dinesh Kumar.

    Monday, August 22, 2016 7:47 AM

Answers

  • Hi Dinesh,

    suit yourself. I don't know your full circumstances, so I really can't judge whether you should use it (based on the facts I have, it makes no sense though).

    To work with substring, you need to check the total length first:

    $linecount =  Get-Content $filepath 
    foreach ($line in $linecount)
    {
    if ($line.Length -gt 2) {
    if ($line.substring(0,2) -eq 'W ' -and $line.Substring(0,3) -ne 'W -'  -and  $line.Substring(0,2) -ne '***'  -and $line.Substring(0,1) -ne 'E' )
    {
    $line | Add-Content $Filepath1
    }
    }
    }

    The result will be no different to my regex solution but it is far harder to read, once you have basic understanding of regex (doesn't really take much skill at that complexity).

    Oh, and it's slower, though not by so much as to really matter.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by Dinesh Edvin Monday, August 22, 2016 10:53 AM
    Monday, August 22, 2016 9:52 AM

All replies

  • Hi Dinesh,

    this happens, because your file contains lines less than 3 signs long. Your filter can be simplified to this:

    Get-Content $filepath | select-string "^W [^-]" | Add-Content $Filepath1

    Cheers,
    Fred

    PS:
    The Regular Expression explained:
    This will match all lines that start with "W " (without the quotes), and whose third symbol is not a "-" (again without the quotes).


    There's no place like 127.0.0.1


    • Edited by FWN Monday, August 22, 2016 8:01 AM
    Monday, August 22, 2016 7:59 AM
  • Hi,

    In the above script you have included only for the matches which lines that start with "W " .

    but still I need to exclude some rows by using conditions.

    can yu give me briefly?

    Monday, August 22, 2016 8:54 AM
  • Hi Dinesh,

    it excludes all other rows, same as your original logic did (If it has to start with "W ", it automatically cannot start with "***" or "E"). There is no point in explicitly excluding those. You can include others, however:

    # Old filter
    "^W [^-]"
    
    # New Filter
    "^W [^-]|^E [^+]"

    This will pass through all lines that either...

    start with "W " but do not have a "-" as third symbol.

    or

    start with "E " but do not have a "+" as thrid symbol.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Monday, August 22, 2016 9:02 AM
  • Hai Fred,

    I should use the Substring and I have to avoid this error 
      "Exception calling "Substring" with "2" argument(s): "Index and length must refer to a
       location within the string. Parameter name: length" At "

    Please look into my above script and guide me how to resolve that error.


    Thanks and Regards,
    Dinesh.

    Monday, August 22, 2016 9:45 AM
  • Hi Dinesh,

    suit yourself. I don't know your full circumstances, so I really can't judge whether you should use it (based on the facts I have, it makes no sense though).

    To work with substring, you need to check the total length first:

    $linecount =  Get-Content $filepath 
    foreach ($line in $linecount)
    {
    if ($line.Length -gt 2) {
    if ($line.substring(0,2) -eq 'W ' -and $line.Substring(0,3) -ne 'W -'  -and  $line.Substring(0,2) -ne '***'  -and $line.Substring(0,1) -ne 'E' )
    {
    $line | Add-Content $Filepath1
    }
    }
    }

    The result will be no different to my regex solution but it is far harder to read, once you have basic understanding of regex (doesn't really take much skill at that complexity).

    Oh, and it's slower, though not by so much as to really matter.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    • Marked as answer by Dinesh Edvin Monday, August 22, 2016 10:53 AM
    Monday, August 22, 2016 9:52 AM
  • Thank you so much Fred!!!
    Monday, August 22, 2016 10:54 AM
  • Yes you are right.Thank you so much for this. I checked length before using substring.

    Friday, August 25, 2017 8:23 AM