B
B
Brendan Castaneda2020-11-07 17:14:44
cmd/bat
Brendan Castaneda, 2020-11-07 17:14:44

How to rename files in bat file?

I have a .bat file with a folder next to it.
The folder contains .jpg files with different names. (1234567890.jpg, helloWorld.jpg, img-23012010.jpg)
I'm trying to write in a .bat file so that he renames all these files in this folder.
With this name file-001.jpg file-002.jpg file-003.jpg ... file-069.jpg
I can't implement file recalculation..
Here are examples that do not work correctly

set dir=Result\
forfiles /p %dir% /m *.jpg /c "cmd /c ren @file out-0001.jpg"
pause

^ Renames only 1 file, others say NO, this already exists among us ..
SETLOCAL EnableDelayedExpansion
for /f %%I in ('dir /b ^| findstr /r ".*[.]jpg$"') do set "x=%%I" & ren "%%I" "name-001!x:~0!"
pause

^ It renames all files, but when it is in a folder with them, I can't figure out how to move it outside the folder and specify the path. Well, and accordingly, as usual, does not rename by setting the numbering.
set dir="Result\*.jpg"
set new="new_??.jpg"
ren %dir% %new%
pause

^ Here it renames everything, but again I can't make it so that it sets the count (numbering) of the file.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Brendan Castaneda, 2020-11-08
@ae_ph

Here is the solution
This .bat file will rename the Jpg files from the folder like this file file-000.jpg, file-001.jpg, file-002.jpg

setlocal enabledelayedexpansion
cd /d Result
set "count=1000"
set a="*.jpg"
for /f "usebackq delims=*" %%f in (`dir /b /o:-d /tc %a%`) do (ren "%%f" file-!count:~1!.jpg
set /a count+=1
)
pause

SetLocal EnableDelayedExpansion Variable expansion through signs ( ! )
cd /d Result go to the Result folder
set "count=1000" variable in which I indicated the number of zeros file-001.jpg
set a="*.jpg" variable indicates which files we are looking for.
(`dir /b /o:-d /tc %a%`)
dir - Displays a list of files and subdirectories. Next we sort.
/b - Display only filenames.
/o:-d - Sort the list of displayed files in reverse order ( newest to oldest ).
/tc - Sort by file creation time
%a% - Call
usebackq variableSpecifies the ability to use quotes for file names Such as > " .
Specifies the execution of a string enclosed in back quotes as commands Such as > ` ,
and single-quoted strings as commands in a character string Such as > ' .
delims=xxx Specifies a set of delimiters .Replaces the default delimiter set of spaces and tabs.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question