2

Screenshot

I have a number of files with the same name and different extensions in a folder. Originally these files were all in .jpg format with different names.

Like:
1.j87
1.j88
1.j89

Now I've to rename each image to different name and extension individually.

I found this, but I don't know how to use this script. Can someone help?

2
  • $i = 1 ; gci "C:\pathToFolder" | rni $_ -newname ("filename" + $i + ".jpg") ; $i++ save this in a script.ps1 file and right click it and "execute with powershell" change filename and .xy to the name and extension you want. change C:\pathtofolder to the folderpath where your files are
    – SimonS
    Commented Oct 19, 2017 at 11:19
  • Possible duplicate. superuser.com/questions/1088810/…
    – root
    Commented Oct 19, 2017 at 14:43

4 Answers 4

0

I know how to accomplish this with Windows CMD shell after you change to the directory of your JPG files.

Save this script to a BAT file such as renamer.bat or any non-reserved command and execute it from your command prompt.

setlocal enabledelayedexpansion

set /a count=0
for /f "tokens=*" %%a in ('dir /b /od *.jpg') do (

    ren %%a ArbitraryString!count!.jpg
    set /a count+=1

)

Based on your question, it sounds like you also want to rename every *.jpg file something different than *.jpg, which would cream your file associations and make the images tedious to open. If this is REALLY TRUE, then substitute the Rename line with:

ren %%a ArbitraryString!count!.!count!jpg
1
0

In response to Arjun's comment 10/19 ~2100 UTC, you should execute this script in a batch file inside the same subdirectory as your files, it will rename the files A Random string of numbers.01.jpg and subsequent numbers until all files in the directory are processed. ALL files in those directories whether JPG or not will be renamed to a random name, sequential number and .jpg extension.

Any subdirectories will require executing the script inside those directories.

setlocal enabledelayedexpansion
set /a count=0
for /f "tokens=*" %%a in ('dir /b /od *.*') do (
    ren %%a %random%!count!.jpg
    set /a count+=1
)
0

The script you found unconditionally renames all items to filename{sequence number from 1}.jpg from your current directory. Keep in mind that Rename or Move commands can be very destructive, especially when you don't exactly know what the code is.

I change the code to be safer for you as this:

Get-Item "2017-0~1.*" | ForEach-Object {

   $newName = $_.basename + $_.Extension.replace(".", "_") + ".jpg"
   Write-Host "rename-item" $_ $newName
   # rename-item $_ $newName
}

The code gets items named "2017-0~.*" in the current directory. For each item, rename the item by appending the extension ".jpg" to the end while the original extension dot and extension is changed to underscore and extension e.g. ".001" to "_001". In this case you can know what the original file name was.

Note that the real action is commented.

To execute the code:

> cd c:\to\your\directory

C:\to\your\directory> powershell

PS C:\to\your\directory> 

Paste the code

PS C:\to\your\directory> Get-Item "2017-0~1.*" | ForEach-Object {
>>
>>    $newName = $_.basename + $_.Extension.replace(".", "_") + ".jpg"
>>    Write-Host "rename-item" $_ $newName
>>    # rename-item $_ $newName
>> }
>>

PS C:\to\your\directory> exit

C:\to\your\directory>
0

If you are not comfortable using scripts you can rename the files using the excellent (and free) IrfanView, just follow the following steps:

  • Go to File > Batch Conversion/Rename....

  • In Work as select Batch rename.

  • In Batch rename settings change the default pattern of image### to image###.jpg as you want to change the extension too. This will name the files "image001.jpg", "image002.jpg", etc. (if you have more than 999 images in that folder use instead image####.jpg). You can configure other name pattern using the Options button.

  • In the explorer panel on the right select All files (*.*) in Type and go to the folder in which you have the files, then select the ones you want to rename and press Add (or press Add all if you want to to rename all the files of that folder).

  • In Output directory for result files press Use current ("look in") directory for selecting the same folder in which files are located, or use Browse to select a different one if you want to move renamed files to another folder.

  • Finally this press Start batch to begin the batch renaming.

Note that as a safety measure by default IrfanView will make a copy of the files with the new names in the output folder, instead of just renaming the existing ones. To rename the original files go to the Options of Batch rename settings and change Copy original/input files to output directory to Move original/input files to output directory.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .