0

I've been trying to create a batch file to rename my files into:

"Foldername_01.jpg, Foldername_02.jpg, etc..."

but my output gets renames the first file as "%~dp00.jpg" and the rest returns an error of

"A duplicate file name exists, or the file cannot be found"

My code:

@echo off
set /a count = 0

for %%a in (*.jpg) do (
    set /a count+=1
    ren "%%a" "%%~dp0_%count%.jpg" 
    )

pause

I don't understand why the count variable is not adding 1 from every entry and that the file is being renamed as " %~dp0 + count + .ext" rather than its "folder name + count + .ext"

Blue
  • 137
  • 9
  • 1
    1. You need [delayed expansion](https://ss64.com/nt/delayedexpansion.html) for variable `count`. 2. `for` does not read the full list of files in advance, so there might some files become renamed twice; so use `for /F "eol=| delims=" %%F in ('dir /B /A:-D-H-S "*.jpg"') do set /A "COUNT+=1" & ren "%%F" "%~n0_!COUNT!.mp4"`. 3. `ren` only accepts names as the second argument (without paths). – aschipfl Jan 11 '20 at 12:35
  • @Compo, delayed expansion is only one issue of the code, so I'd not consider it as a duplicate (see my other comment)... – aschipfl Jan 11 '20 at 12:37
  • @Compo that was a mistake on my part, in editing the code, I was trying different file formats may be the cause – Blue Jan 11 '20 at 12:44
  • @Compo, true, I didn't recognise the deviating extensions at first (which I still don't understand since changing an extension doesn't convert the file, but maybe that's just a typo in the question?)... – aschipfl Jan 11 '20 at 12:45
  • @Compo the file results to %_nx0_1.jpg, %_nx0_2.jpg, etc. the count is working now. – Blue Jan 11 '20 at 12:57
  • @Blue, to clarify, you can use a nested for loop to capture the directory name. For example: line 1. `@Set "count=0"`, line 2. `@For /F "EOL=|Delims=" %%I In ('Dir /B/S/A-D-S "*.jpg" 2^>NUL')Do @(`, line 3. `For %%J In ("%%~pI.")Do @(Set /A count+=1`, line 4. `SetLocal EnableDelayedExpansion`, line 5. `Ren "%%I" "%%~nxJ_!count!%%~xI"`, line 6. `EndLocal))`, and line 7. `@Pause` – Compo Jan 11 '20 at 14:52
  • I have reopened your question Blue, because the fix for your naming issue is I suppose your main question, and the delayed expansion issue, I used as a [duplicate answer](https://stackoverflow.com/q/30282784), is technically only a part of the solution. If you'd like one or both of myself or aschpfl to put our comments into an official answer, please let us know. Thank you. – Compo Jan 11 '20 at 15:05
  • @Compo that's the answer, I got it to work that way. Can you put that as the official answer. And it should be noted that aschipfl's comment explained the problems in my initial code. Thanks for the help – Blue Jan 11 '20 at 15:31

1 Answers1

1

Your issue can be handled by using delayed expansion, for your incrementing variable, within a nested for loop, to capture the directory name.

Example:

@Set "count=0"
@For /F "Delims=" %%I In ('Dir /B/S/A-D-S "*.jpg" 2^>NUL')Do @(
    For %%J In ("%%~pI.")Do @(Set /A count+=1
        SetLocal EnableDelayedExpansion
        Ren "%%I" "%%~nxJ_!count!%%~xI"
        EndLocal))
@Pause
Compo
  • 36,585
  • 5
  • 27
  • 39