-1

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

Sameer Patkar
  • 101
  • 1
  • 9

3 Answers3

1

You could use the ForFiles utility for such a task, in the Command Prompt, PowerShell Prompt, or from a batch file.

forfiles /p "C:\directory\with\images" /m "1.*" /c "cmd /c ren @file image.@ext.jpg"
Compo
  • 36,585
  • 5
  • 27
  • 39
0

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
Alex R.
  • 467
  • 3
  • 14
  • Can it be doable with command prompt.? – Sameer Patkar Jan 10 '22 at 09:34
  • @SameerPatkar Yes, it works in powershell. Use one-line `$foldercontent = Get-ChildItem -Path "C:\tmp" -Filter "1.*" | Where-Object { ! $_.PSIsContainer }; foreach($item in $foldercontent) { Move-Item -Path $item.FullName -Destination "C:\tmp\image$($item.Extension).jpg" };` – Alex R. Jan 10 '22 at 09:38
0

There could be used the following batch file to rename a file like 1.J73 to image_73.jpg:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "ImageFolder=C:\directory\with\images"
for /F "tokens=2 delims=Jj." %%I in ('dir "%ImageFolder%\1.J*" /A-D /B 2^>nul') do ren "%ImageFolder%\1.J%%I" "image_%%I.jpg"
endlocal

There is executed in background one more command process with the command line:

C:\Windows\System32\cmd.exe /c dir "C:\directory\with\images\1.J*" /A-D /B 2>nul

DIR searches in the specified directory for

  • just files because of the option /A-D (attribute not directory)
  • matching the wildcard pattern 1.J* and
  • outputs just the names of the found files without path in bare format because of the option /B to the handle STDOUT (standard output) of the background command process.

DIR would output an error message to handle STDERR (standard error) on not finding any matching file. This error message is suppressed by redirecting it to the device NUL using 2>nul.

Read the Microsoft documentation about Using command redirection operators for an explanation of 2>nul. The redirection operator > must be escaped with caret character ^ on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes the embedded dir command line with using a separate command process started in background.

The output of DIR to STDOUT is captured by the Windows command processor cmd.exe processing the batch file and processed line by line after started cmd.exe closed itself.

FOR with option /F ignores empty lines which do not occur here. A non-empty line would be split up by default into substrings using normal space and horizontal tab as string delimiters. There would be checked next the first character of the first substring on being a semicolon which is by default the end of line character in which case the line would be ignored for further processing. Otherwise the first substring is assigned to the specified loop variable and the commands in body of the FOR loop are executed by cmd.exe.

The default behavior on line processing is modified by the used options tokens=2 and delims=Jj.. The string delimiters are redefined with J and j and . which means a file name like 1.J73 is split up into the two substrings 1 and 73. The option tokens=2 instructs FOR to assign the second substring (token) to the specified loop variable I. The default eol=; is kept as the first substring is always 1 in this case and therefore no file name is ignored here. So for a file with name 1.J73 or 1.j73 the string 73 is assigned to the loop variable I.

Then the rename command REN is executed with the fully qualified file name of the current file and the new file name with the prefix image_ and file number from existing file name and with the file extension .jpg. So the file is renamed from 1.J73 to image_73.jpg if there is not already a file with name image_73.jpg and the file to rename is not currently opened by an application in a manner which prevents renaming the file while being opened by the application.

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • cmd /?
  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • ren /?
  • set /?
  • setlocal /?
Mofi
  • 46,139
  • 17
  • 80
  • 143