Home > Back-end >  Powershell script to read and split filename from a folder
Powershell script to read and split filename from a folder

Time:03-22

I'm new to powershell. I want to read names of each file in a folder for eg. 900_CA_2022.pdf, remove the _ from the filename and create a new text file which has name 900CA2022900_CA_2022.txt

Basically, I want to remove the _ from the extension-less file name, append the latter as-is, and use a new extension, .txt

CodePudding user response:

Important: All solutions below create the new files in the current directory. If needed, construct the target file path with an explicit directory path, using initial files

Script Code

$files = Get-ChildItem "./*.pdf" -Recurse -Force
$files | ForEach-Object{
    New-Item -Path "$($_ | Split-Path)/$($_ | Split-Path -Leaf).txt" -Force
}

Result files
result files
result files gui

  • Related