powershellbatch-filecommand-linewindows-administration

How do I rename all files from folder to xxx{incremental}.jpg using command prompt


I have below files in folder with a same name. Only the extensions are different. All files are valid jpg but with the wrong extension.

enter image description here

How to rename them with extension as .jpg, with a different prefix?

ren *.* *.jpg ----> Is not working for me as they all are having same prefix


Solution

  • This should work for you:

    Clear-Host
    $directoryPath = "C:\directory\with\images"
    #Getting all files with "1.*" from $directoryPath
    $foldercontent = Get-ChildItem -Path $directoryPath -Filter "1.*" | Where-Object { ! $_.PSIsContainer }
    foreach($item in $foldercontent) {
        Move-Item -Path $item.FullName -Destination "$directoryPath\image$($item.Extension).jpg"
    }
    

    File names will be:

    image.J84.jpg
    image.J85.jpg