Author Posts

May 26, 2015 at 11:32 pm

Hi developer,

Here is my sample script:
Get-Content .\Test.txt | ForEach-Object{$_ -replace "\s+",","} | Out-File -FilePath Test.csv -Encoding OEM

It is ok as above the script.

Now i am going to change of my logic script as below:

$File = .\Test.txt
$BaseName = $File.BaseName

Get-Content [b]$File[/b] | ForEach-Object{$_ -replace "\s+",","} | Out-File -FilePath [b]$BaseName[/b].csv -Encoding OEM

it does not work and how to get file name or base name (Test) without extension?

Any idea?

May 27, 2015 at 1:14 am

Your line retrieving the file is not actually getting the file. I imagine the file is just opening on your screen when you run "$File = .\Test.txt". You have to use Get-Item to get file properties like BaseName.

$BaseName = Get-Item .\test.txt | Select-Object -ExpandProperty BaseName