2

So the script works well, I usually run it in the folder with the files I modify (Mainly .mp3's) to trim the first X characters from the filename (Numbers spaces dashes ect) of multiple files.

@echo off setLocal enableDelayedExpansion set X=5 set FOLDER_PATH=. pushd %FOLDER_PATH% for %%f in (*) do if %%f neq %~nx0 ( set "filename=%%~nf" set "filename=!filename:~%X%!" ren "%%f" "!filename!%%~xf" ) popd 

But running it on my SD Card (Formatted for Galaxy S4 w./ Android 4.3) that I have plugged in it works perfectly for 70% of the files. But for some it runs twice, and for a very rare few it acts like it hasn't run at all. I'm completely befuddled by it, is it just the SD Card?

1
  • Sorry about the android tag, it was recommended and looking back it was irrelevant. My bad Commented Feb 17, 2014 at 23:45

1 Answer 1

2

The simple FOR command with wildcards begins iterating files before it finishes reading the entire directory. It buffers a block of files and iterates them, then picks up where it left off. When you rename a file, the renamed file may sort later in the list, and be picked up in a later block of files. Hence the double renaming.

Any time you have a loop that modifies the directory listing you should use FOR /F with the DIR /B command instead of the simple FOR.

for /f "eol=: delims=" %%F in ('dir /b /a-d-h-s') do ... 

The EOL=: is just in case a file name begins with ; (unlikely, but possible). Other valid options with the same result are EOL=* or EOL=? - All are characters that cannot begin a file name or path.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, your answer is appreciated. It'll help a lot in other areas too!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.