1

Changing file extension of files in a folder if the file was created yesterday.

This is to change the file extension path from .comh to .txt

if ($countFiles.Count -gt 0)

    {

    foreach ($f in $files)

      {

     if ($f.CreationTime -gt($(Get-Date).AddDays((-1))))

      {
        #Changing of extension      
      }  
Jiawei
  • 13
  • 1
  • 3
  • take a look at what you get from `@(Get-ChildItem -LiteralPath $env:TEMP -File)[0] | Get-Member` for some ideas about what properties are provided with `FileInfo` objects. [*grin*] – Lee_Dailey Jul 10 '19 at 03:36
  • please refer https://stackoverflow.com/help/how-to-ask, – Nirav Mistry Jul 10 '19 at 03:50
  • Can you please elaborate your requirements ? – Nirav Mistry Jul 10 '19 at 03:52
  • You could try `Dir *.comh | rename-item -newname { [io.path]::ChangeExtension($_.name, "txt") }`. I got this from here [How do I change the extension of many files in a directory?](https://stackoverflow.com/questions/12120326/how-do-i-change-the-extension-of-many-files-in-a-directory) and it works fine for me – techguy1029 Jul 10 '19 at 19:04

1 Answers1

2

You'll get all the properties of that file in $f , use Rename-Item cmdlet to rename it, you would build the new name from the existing by changing the extension.

#Changing of extension
$NewName = $F.BaseName + '.txt'
$F | Rename-Item -NewName $NewName
Prasoon Karunan V
  • 2,916
  • 2
  • 12
  • 26