locked
Powershell -Replace Textfile List of paths RRS feed

  • Question

  • Good Morning from germany,

    I'm actually working working about 10 hours on the same problem:

    I'm generating a Textfile with Filepaths, that I want to copy later on in my script. That doesn't make any problems.

    Example.txt:

    C:\Test1\Test2\Test3\file1.txt

    C:\Test1\Test2\Test3\file2.txt

    C:\Test1\Test2\Test3\file3.txt

    The point now is, that I want to replace the ending of the path (file1.txt, file2.txt, etc.). I tried just simply with replacing "*.txt, but that doesn't work for me. I tried also Split String, but that doens't work for me either...

    May someone give me clue, what method I may user for this. I'm working for just around a week with powershell, so please be calm, if I don't get anything immediately. :-)

    Thanks!

    Friday, August 21, 2015 7:57 AM

Answers

  • 
    
    PS C:\scripts> cat example.txt
    C:\Test1\Test2\Test3\file1.txt
    C:\Test1\Test2\Test3\file2.txt
    C:\Test1\Test2\Test3\file3.txt
    PS C:\scripts> cat example.txt|split-path
    C:\Test1\Test2\Test3
    C:\Test1\Test2\Test3
    C:\Test1\Test2\Test3
    PS C:\scripts> cat example.txt|split-path|out-file example2.txt
    PS C:\scripts> cat example2.txt
    C:\Test1\Test2\Test3
    C:\Test1\Test2\Test3
    C:\Test1\Test2\Test3
    PS C:\scripts>
    
    

    \_(ツ)_/


    • Edited by jrv Friday, August 21, 2015 10:07 AM
    • Marked as answer by Elaine Jing Wednesday, August 26, 2015 7:13 AM
    Friday, August 21, 2015 10:07 AM

All replies

  • Hi,

    here is something to play with

    $a = 'C:\Test1\Test2\Test3\file1.txt'
    $a.TrimEnd('.txt')
    
    ## or ##
    
    $b = Get-ChildItem C:\Test1\Test2\Test3\file1.txt
    $b.FullName
    $b.BaseName
    $b.DirectoryName
    $b.Name
    
    ($b.FullName).TrimEnd('.txt')


    • Edited by psott Friday, August 21, 2015 8:33 AM
    Friday, August 21, 2015 8:31 AM
  • $pathpart=split-path 'C:\Test1\Test2\Test3\file1.txt'

    $filepart=split-path 'C:\Test1\Test2\Test3\file1.txt'  -leaf

    help split-path -full


    \_(ツ)_/

    • Proposed as answer by Chen VMVP Friday, August 21, 2015 9:23 AM
    Friday, August 21, 2015 8:38 AM
  • Hello,

    If it's something inside a file that you want to replace/remove?


    you could try these two procedures, if not please ignore i may have understood the question incorrectly.

    1.remove ( .txt extension)

    $newLine = ""
    $content = Get-Content .\example.txt
    foreach($line in $content)
    {
    $newLine +=  $line.trim('.txt') + "`n"
    }
    $newLine |Set-Content .\example.txt


    Output: inside file example.txt

    C:\Test1\Test2\Test3\file1

    C:\Test1\Test2\Test3\file2

    C:\Test1\Test2\Test3\file3

    2.replace (.txt extension with .pdf)

    $newLine = ""
    $content = Get-Content .\Example.txt
    foreach($line in $content)
    {
    $newLine +=  $line.Replace('.txt','.pdf') + "`n"
    }
    $newLine |Set-Content .\Example.txt

    Output: inside the example.txt file

    C:\Test1\Test2\Test3\file1.pdf

    C:\Test1\Test2\Test3\file2.pdf

    C:\Test1\Test2\Test3\file3.pdf

    Regards,


    Venu

    Friday, August 21, 2015 9:22 AM
  • Thanks Alexander,

    but this replaces only the suffix .txt. I wanted to replace the whole filename 'file1.txt'. "Split-Path" seem to bring the solution...

    thanks anyway. :-)

    Friday, August 21, 2015 9:59 AM
  • Thanks Venu,

    but this replaces only the suffix .txt. I wanted to replace the whole filename 'file1.txt'. "Split-Path" seem to bring the solution...

    thanks anyway. :-)
    Friday, August 21, 2015 10:00 AM
  • Thank you jrv,

    this seems to be a good way, just having a bit trouble with saving my "paths".

    $pathpart = Get-Content "example.txt" | Split-Path | Set-Content "example.txt"

    is not working... May you give me second hint? :-)

    Greetings

    Friday, August 21, 2015 10:03 AM
  • 
    
    PS C:\scripts> cat example.txt
    C:\Test1\Test2\Test3\file1.txt
    C:\Test1\Test2\Test3\file2.txt
    C:\Test1\Test2\Test3\file3.txt
    PS C:\scripts> cat example.txt|split-path
    C:\Test1\Test2\Test3
    C:\Test1\Test2\Test3
    C:\Test1\Test2\Test3
    PS C:\scripts> cat example.txt|split-path|out-file example2.txt
    PS C:\scripts> cat example2.txt
    C:\Test1\Test2\Test3
    C:\Test1\Test2\Test3
    C:\Test1\Test2\Test3
    PS C:\scripts>
    
    

    \_(ツ)_/


    • Edited by jrv Friday, August 21, 2015 10:07 AM
    • Marked as answer by Elaine Jing Wednesday, August 26, 2015 7:13 AM
    Friday, August 21, 2015 10:07 AM
  • What a pleasure, thank you very much!
    Friday, August 21, 2015 10:33 AM