Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I can use this to loop over files:

for %%a in ("%directory%\*.%extension%") do (
    echo FILE: %%~fa
)
goto :eof

What if I also rename the files, like this (:rename is a subroutine that renames the files):

for %%a in ("%directory%\*.%extension%") do (
    echo FILE: %%~fa
    call :rename "%%~fa"
)
goto :eof

In that case, some of the files are renamed (and echoed) two or even three times. I think it's because after they are renamed, in some cases they are regarded as new files that also need to be looped through. However, this does not happen with all the renamed files.

How can I overcome this? I want every file to be renamed only once.

Solution:

:: remove quotes:
:: http://www.dostips.com/?t=Snippets.TrimQuotes

for /f "useback tokens=*" %%a in ('%extension%') do set extension=%%~a

for /f "useback tokens=*" %%a in ('%directory%') do set directory=%%~a

for /f "delims=" %%a in ('dir /b /a-d "%directory%\*.%extension%" ^| sort /r') do (
    echo FILE: %directory%\%%a
    call :renamingSubroutine "%directory%\%%a"
)
goto :eof

OR:

:: remove quotes:
:: http://www.dostips.com/?t=Snippets.TrimQuotes

for /f "useback tokens=*" %%a in ('%extension%') do set extension=%%~a

for /f "useback tokens=*" %%a in ('%directory%') do set directory=%%~a

for /f "delims=" %%a in ('dir /b /a-d /s /on "%directory%\*.%extension%"') do (
    echo FILE: %%~fa
    call :renamingSubroutine "%%~fa"
)
goto :eof
share|improve this question
add comment (requires an account with 50 reputation)

1 Answer

up vote 1 down vote accepted

try this:

for /f "delims=" %%a in ('dir /b /a-d /s "%directory%\*.%extension%"^|sort /r') do (
    echo FILE: %%~fa
    call :rename "%%~fa"
)
goto :eof

BTW you shouldn't give batch functions the names of cmd commands (rename).

share|improve this answer
Thanks.. I actually didn't call it rename in actual code, but thanks for mentioning this. I think what prevents your code from fully working is that I want the dir to output full path as well, otherwise %%a takes the path of batch file (which is wrong). I tried changing the switch /a-d to /ad as shown here: helgeklein.com/blog/2010/08/… however I only suddenly got File Not Found... – rapt May 8 at 17:15
Even in command line, if I try dir /b /ad "J:\Temp\*.jpg" I get File Not Found, but if I try dir /b "J:\Temp\*.jpg" files are listed... why is that? – rapt May 8 at 17:16
Ok, I figured out the manual was somewhat misleading. By directories they meant subdirectories... I've updated my question above with the solution. Thanks Endoro. – rapt May 8 at 17:41
1  
Yes, you are right, full path is needed (added /s). – Endoro May 8 at 17:48
Thanks a lot, that's even better. – rapt May 8 at 19:40
add comment (requires an account with 50 reputation)

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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