locked
Wanted to trim suffix from my string using powershell scripting? RRS feed

  • Question

  • Hi,

    I have a string "abcdefg_control" . I wanted to return "abcdef" i.e remove the suffix "_control" . Also, I tried to use TrimEnd() and Split() in powershell script but the shell prompt is throwing errors with these methods. Is there some other way to use these .NET Framework methods in power shell? Please let me know about the way to use these .NET Framework methods in powershell and also solve my problem of removing the suffix.

    Thanks,

    Anirudh

    Friday, August 19, 2011 8:17 AM

Answers

  • Hi

    I tried first example but my powershell is throwing following error:

    "Method invocation failed because [System.IO.FileInfo] doesn't contain a method named 'indexof'.
    At D:\ariba\scripts\My_script_Control\NewBalancingControlFile.ps1:62 char:56"

     

    I have used:

    $str=$actualFile.substring(0,$actualFile.indexof("."))

    where $actualFile is "remittance.csv"

    Thanks

    PS > [io.path]::GetFileNameWithoutExtension("file.txt")
    file
    
    PS > $actualFile.substring(0,$actualFile.indexof("."))
    file
    PS > $str = $actualFile.substring(0,$actualFile.indexof("."))
    PS > $str
    file



    is simple : $actualFile.BaseName

     

    PS > $file = gi C:\windows\win.ini
    PS > $file.GetType().Fullname
    System.IO.FileInfo
    PS > $file.BaseName
    win


    Friday, August 19, 2011 9:18 AM

All replies

  • 1)
    $str = "abcdefg_control" 
    $str.substring(0,$str.indexof("_"))
    
    2)
    ("abcdefg_control" -split "_")[0]
    

    3)
    "abcdefg_control".split("_")[0]



    • Proposed as answer by Bigteddy Friday, August 19, 2011 10:33 AM
    Friday, August 19, 2011 8:38 AM
  • Hi

    I tried first example but my powershell is throwing following error:

    "Method invocation failed because [System.IO.FileInfo] doesn't contain a method named 'indexof'.
    At D:\ariba\scripts\My_script_Control\NewBalancingControlFile.ps1:62 char:56"

     

    I have used:

    $str=$actualFile.substring(0,$actualFile.indexof("."))

    where $actualFile is "remittance.csv"

    Thanks

    Friday, August 19, 2011 9:14 AM
  • Hi

    I tried first example but my powershell is throwing following error:

    "Method invocation failed because [System.IO.FileInfo] doesn't contain a method named 'indexof'.
    At D:\ariba\scripts\My_script_Control\NewBalancingControlFile.ps1:62 char:56"

     

    I have used:

    $str=$actualFile.substring(0,$actualFile.indexof("."))

    where $actualFile is "remittance.csv"

    Thanks

    PS > [io.path]::GetFileNameWithoutExtension("file.txt")
    file
    
    PS > $actualFile.substring(0,$actualFile.indexof("."))
    file
    PS > $str = $actualFile.substring(0,$actualFile.indexof("."))
    PS > $str
    file



    is simple : $actualFile.BaseName

     

    PS > $file = gi C:\windows\win.ini
    PS > $file.GetType().Fullname
    System.IO.FileInfo
    PS > $file.BaseName
    win


    Friday, August 19, 2011 9:18 AM