0

I have a .bat file next to the (Result) folder.
The folder contains .jpg files with different names.
I'm trying to write a .bat file so that it renames all these files in this folder.
It works, but not completely. Here's the problem:
It only works in the directory the files are in.
(I need a .bat file and a folder with files next to it. For this to work).

setlocal enabledelayedexpansion
set "count=1000"
set a="Result\*.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
2
  • Put "s around the second %%f
    – DavidPostill
    Nov 7, 2020 at 21:03
  • Yes I have placed. This only solved the first #1 problem. Remaining #2, #3
    – Артем
    Nov 7, 2020 at 21:11

2 Answers 2

1

Here is the solution..
You can just go to the folder by writing the path cd / d Result

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 Expansion of variables through signs (!)
cd / d Result go to the Result folder
set "count = 1000" variable in which I marked the number of zeros file-001.jpg
set a = "*. jpg" the 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 - Sorts the list of displayed files in reverse order (newest to oldest).
/tc - Sort by file creation time.
%a% - Call the variable.

usebackq Sets the ability to use quotes for filenames Like this> ".
Specifies the execution of a string, enclosed in back quotes, as commands Like this> `,
and strings in single quotes are like commands in a character string Like this> '.

delims = xxx Specifies a set of delimiters. Overrides the default space-tab delimiter set.

0

Your question is not very clear to me (my English is weak and poor), I don't understand what it would be:

It only works in the directory the files are in.
(I need a .bat file and a folder with files next to it. For this to work).

  • I suppose you want to rename recursively, here's an edit for that.
@echo off 


set "_cnt=1000" && cd /d "%~dp0Result"

for /f tokens^=* %%I in ('dir /o:-d /tc /b /s *.jpg
')do set /a "_cnt+=1" && cmd /v /c "ren "%%~fI" file-!_cnt:~-3!.jpg"
 
%__AppDir__%timeout.exe /t -1 & goto :EOF

1. Enter the folder where the bat is:

cd /d "%~dp0Result"

2. List your files recursively dir ... /b /s

for /f tokens^=* %%I in ('dir /o:-d /tc /b /s *.jpg

3. Increment your accountant

set /a "_cnt+=1"

4. Rename your file in a loop using your counter

cmd/v/c "ren "%%~fI" file-!_cnt:~-3!.jpg"

Obs. If I am distant from what you are looking for, please let me know...


  • An alternative based on your answer
@echo off 

setlocal enabledelayedexpansion
set "_cnt=100" && cd /d "%~dp0"

for /f tokens^=* %%I in ('dir/b/o:-d/tc/b/s "Result\*.jpg"
')do rename "%%~fI" file-!_cnt:~-3!.jpg & set /a "_cnt+=1" 

%__AppDir__%timeout.exe /t -1 & endlocal & goto :EOF
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.