Difference between revisions of "Rename multiple files with powershell"

From DevOps Notebook
(Created page with "Example:<br> Rename files named image.1.jpg, image.3.jpg... into 1.jpg, 3.jpg <br> <pre> <pre> Get-ChildItem image.*.jpg|ForEach-Object { $NewName = $_.Name -replace "^(i...")
 
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
 
Example:<br>
 
Example:<br>
Rename files named image.1.jpg, image.3.jpg... into 1.jpg, 3.jpg <br>
+
Rename files named ''image.1.jpg, image.3.jpg''... into 1.jpg, 3.jpg <br>
  
<pre>
+
<syntaxhighlight lang="PowerShell">
<pre>
 
 
Get-ChildItem image.*.jpg|ForEach-Object {
 
Get-ChildItem image.*.jpg|ForEach-Object {
 
     $NewName = $_.Name -replace "^(image\.)(.*)",'$2'
 
     $NewName = $_.Name -replace "^(image\.)(.*)",'$2'
Line 9: Line 8:
 
     Move-Item -Path $_.FullName -Destination $Destination -Force
 
     Move-Item -Path $_.FullName -Destination $Destination -Force
 
}
 
}
</pre>
+
</syntaxhighlight>
</pre>
 

Latest revision as of 10:38, 31 January 2023

Example:
Rename files named image.1.jpg, image.3.jpg... into 1.jpg, 3.jpg

Get-ChildItem image.*.jpg|ForEach-Object {
    $NewName = $_.Name -replace "^(image\.)(.*)",'$2'
    $Destination = Join-Path -Path $_.Directory.FullName -ChildPath $NewName
    Move-Item -Path $_.FullName -Destination $Destination -Force
}