Add folder name to beginning of filename - getting multiple renames

Posted by Flibble Wibble on Super User See other posts from Super User or by Flibble Wibble
Published on 2013-10-19T15:15:47Z Indexed on 2013/10/19 15:59 UTC
Read the original article Hit count: 218

Filed under:

I've used dbenham's excellent response to the question of how to add the folder name to the beginning of a filename in a cmd script.

@echo off
pushd "Folder"
for /d %%D in (*) do (
  for %%F in ("%%~D\*") do (
    for %%P in ("%%F\..") do (
      ren "%%F" "%%~nxP_%%~nxF"
    )
  )
)
popd

What I'm finding is that seemingly randomly (though it probably isn't) sometimes the script will run through several child folders and rename correctly but then it gets to a folder where it gets stuck in a loop and starts adding the folder name repeatedly to the file inside. I have 90,000 files in 300 folders to rename this weekend. Any chance you can guess the cause?

PS: Is there a maximum number of files that are acceptable in each folder?

© Super User or respective owner

Related posts about batch-rename