1

How could I modify name of every file by adding _ before the filename extension in a Get-ChildItem -Include without calling 3 times a Foreach.

My script works, but I would like to simplify it by using -Include and not -Filter.

function renommer_fichier {

    $files = Get-ChildItem -Path C:\mestp\mesFichiers -Filter *.jpg
        Foreach ($file in $files)
        {
            $file | Rename-Item -NewName { $_.Name -replace '.jpg', '_.jpg' }
        }
    $files = Get-ChildItem -Path C:\mestp\mesFichiers -Filter *.mp3
        Foreach ($file in $files)
        {
            $file | Rename-Item -NewName { $_.Name -replace '.mp3', '_.mp3' }
        }
    $files = Get-ChildItem -Path C:\mestp\mesFichiers -Filter *.mpeg
        Foreach ($file in $files)
        {
            $file | Rename-Item -NewName { $_.Name -replace '.mpeg', '_.mpeg' }
        }

}
mklement0
  • 382,024
  • 64
  • 607
  • 775
G0Y1M
  • 79
  • 1
  • 6

1 Answers1

1
  • Unfortunately, the -Include parameter - which supports specifying multiple patterns - doesn't work as one would intuitively expect:

  • Use the .BaseName and .Extension properties in the delay-bind script block you're passing to Rename-Item to facilitate inserting _ before the filename extension.

Get-Item -Path C:\mestp\mesFichiers\* -Include *.jpg, *mp3, *.mpeg |
  Rename-Item -WhatIf -NewName { $_.BaseName + '_' + $_.Extension } 

Note: The -WhatIf common parameter in the command above previews the operation. Remove -WhatIf once you're sure the operation will do what you want.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • I'm sorry for my rookie question but as I search on web I cant figure out what is $_ meaning clearly – G0Y1M Aug 28 '22 at 22:25
  • 1
    @G0Y1M `$_` is what PowerShell calls an _automatic variable_ and, in a pipeline / delay-bind script-block context, it represents the current pipeline input object. See the [automatic `$_` variable](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Automatic_Variables#_) – mklement0 Aug 28 '22 at 22:32
  • Ok so $_ only works as an automatic return variable following the use of a Pipe. If I perform a ForEach $File in $Files each value of $File remains $File and can't be invoked by the $_ variable – G0Y1M Aug 28 '22 at 22:35
  • 1
    @G0Y1M: Yes, even though `foreach` is - confusingly - also an _alias_ for the [`ForEach-Object`](https://learn.microsoft.com/powershell/module/microsoft.powershell.core/foreach-object) _cmdlet_ - where `$_` must be used - it is distinct from the [`foreach`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Foreach) _statement_, where you must use a self-chosen iterator variable. – mklement0 Aug 28 '22 at 22:39
  • and let's imagine that I Get-Item three kind of file according to a -Include of three extension as in my question. How can I create an if, elseif, else to move the list retrieved by the Get-Item according to the extension of each file. I read somewhere that you can use -Like in if? – G0Y1M Aug 28 '22 at 22:44
  • 1
    https://stackoverflow.com/q/73522970/18023379 – G0Y1M Aug 28 '22 at 23:32