1

I need to rename all files in subdirectories. The names of the subdirectories are different, the file names are different, but they are always JPG files. I need a BAT script that will change the names to the current date with the next digit at the end in each directory, ie: 2021-01-05_1.jpg 2021-01-05_2.jpg etc. I found such a script on this website, I rewritten it a bit and it works perfectly but not in subdirectories. Is it possible to modify it to do it also in subdirectories?

@echo off
setlocal EnableDelayedExpansion

set filename=%DATE%
set suffix=1000
for /F "delims=" %%i in ('dir /B *.jpg') do (
   set /A suffix+=1
   ren "%%i" "%filename%_!suffix:~1!.jpg"
)
2
  • 4
    Start with adding /s to the dir command.
    – DavidPostill
    Jan 5, 2021 at 21:26
  • You are genius. Works perfect, thank you.
    – Robs
    Jan 5, 2021 at 22:10

2 Answers 2

0

Perhaps you can understand PowerShell. Use PowerShell instead of cmd, because all the things cmd can do, PowerShell can do too, but cmd doesn't support PowerShell commands.

To open PowerShell, use Win+R→type PowerShellCtrl+Shift+Enter

Then, you can get the same results you intended by your batch file in PowerShell with these codes:

$Filename=(Get-Date).tostring("yyyy-MM-dd")
$suffix=1
Get-ChildItem -path "path\to\folder" -file -force -recurse -filter "*.jpg" | foreach-object {
    Rename-Item -path $_.fullname -NewName "${Filename}_${suffix}.jpg"
    $suffix+=1
}
0

In /

  • Use a for /r (*.jpg) loop:
@echo off && cd /d "%~dp0"

for /r %%i in (*.jpg)do set /a "_cnt+=1000+1" && (
   cmd /v /c "ren "%%~i" "!date:/=-!_!_cnt:~-3!.jpg"" )

In /

  • Use a gci *.jpg -re; 1..$Itens.count | loop:
[Array] $Itens = gci *.jpg -re; 1..$Itens.count  | % {
  ren $Itens[$_-1] -new " $((Get-Date).ToString("yyyy-MM-dd")+('_{0:d3}.jpg' -f [int]$_))" }

You must log in to answer this question.

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