File name without extension

This topic contains 1 reply, has 2 voices, and was last updated by Profile photo of Peter Jurgens Peter Jurgens 1 year, 5 months ago.

  • Author
  • #25665
    Profile photo of PowerShell84

    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?

  • #25669
    Profile photo of Peter Jurgens
    Peter Jurgens

    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

You must be logged in to reply to this topic.