Powershell -Replace Textfile List of paths

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!

August 21st, 2015 4:00am

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')


Free Windows Admin Tool Kit Click here and download it now
August 21st, 2015 4:34am

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

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

help split-path -full

August 21st, 2015 4:41am

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,

Free Windows Admin Tool Kit Click here and download it now
August 21st, 2015 5:25am

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. :-)

August 21st, 2015 6:02am

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. :-)
Free Windows Admin Tool Kit Click here and download it now
August 21st, 2015 6:03am

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

August 21st, 2015 6:06am


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 21 hours 1 minutes ago
Free Windows Admin Tool Kit Click here and download it now
August 21st, 2015 6:10am

What a pleasure, thank you very much!
August 21st, 2015 6:36am

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')


Free Windows Admin Tool Kit Click here and download it now
August 21st, 2015 8:31am


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
August 21st, 2015 10:07am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics