Home > Back-end >  How to add dashes after fourth and sixth characters with Win10 CMD Rename files
How to add dashes after fourth and sixth characters with Win10 CMD Rename files

Time:01-01

I have some files such as:

  • 20220716_165615-IMG_1234.jpg
  • 20220717_102742-IMG_1235.jpg
  • 20220717_193212-IMG_1236.jpg

They need to be renamed as

  • 2022-07-16_16.56.15-IMG_1234.jpg
  • 2022-07-17_10.27.42-IMG_1235.jpg
  • 2022-07-17_19.32.12-IMG_1236.jpg

I tried using a rename in win10 CMD:

ren ????????_??????-*.* ????-??-??_??.??.??-*.*

but I was not successful.

CodePudding user response:

That image file renaming task can be done with a batch file with the following command lines:

@echo off
setlocal EnableExtensions EnableDelayedExpansion
for /F "delims=" %%I in ('dir ????????_??????-IMG_*.jpg /A-D /B 2^>nul') do set "FileName=%%I" & ren "!FileName!" "!FileName:~0,4!-!FileName:~4,2!-!FileName:~6,5!.!FileName:~11,2!.!FileName:~13!"
endlocal

There can be executed directly in a Windows command prompt window with current directory being the directory containing the image files the following command line:

%ComSpec% /E:ON /V:ON /Q /S /C "for /F "delims=" %I in ('dir ????????_??????-IMG_*.jpg /A-D /B 2^>nul') do set "FileName=%I" & ren "!FileName!" "!FileName:~0,4!-!FileName:~4,2!-!FileName:~6,5!.!FileName:~11,2!.!FileName:~13!""

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 /?

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 using %ComSpec% /c and the DIR command line appended as additional arguments.

CodePudding user response:

Turns out it's easier with windows powershell: for the current folder:

Get-Item .\*.* | Rename-Item -NewName {$_.BaseName.insert(15,'.')   $_.Extension} -WhatIf
Get-Item .\*.* | Rename-Item -NewName {$_.BaseName.insert(13,'.')   $_.Extension} -WhatIf
Get-Item .\*.* | Rename-Item -NewName {$_.BaseName.insert(6,'-')   $_.Extension} -WhatIf
Get-Item .\*.* | Rename-Item -NewName {$_.BaseName.insert(4,'-')   $_.Extension} -WhatIf

for the contents of the subfolders:

Get-ChildItem -File -Recurse | Rename-Item -NewName {$_.BaseName.insert(11,'.')   $_.Extension} -WhatIf
Get-ChildItem -File -Recurse | Rename-Item -NewName {$_.BaseName.insert(6,'-')   $_.Extension} -WhatIf
Get-ChildItem -File -Recurse | Rename-Item -NewName {$_.BaseName.insert(4,'-')   $_.Extension} -WhatIf

(the -WhatIf argument shows what's about to be done. To execute it for real, remove the -WhatIf argument)

  • Related