Question

I need a script that will allow me to strip filenames to files that are placed within folders, and apply the folder names to the files, and add an incremental number to the end of each filename.

So the filenames would look something like this in their original state:

gdgeregdja34gtj.jpg

And then look like this after the script is executed:

foldername>foldername001.jpg
foldername>foldername002.jpg

I have this script, which allows the folder name to prefix any filename of files within folders. But it doesn't strip the filenames.

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

Solution

This will echo the rename commands to the screen, so if it does what you want then remove the echo to make it work.

Filenames with ! characters will not be renamed.

@echo off
 setlocal enabledelayedexpansion
  for /d /r %%a in (*) do (
    set n=0
    pushd "%%a"
      for /f "delims=" %%b in (' dir /b /a-d 2^>nul ') do (
        set /a n=n+1
           set num=0000!n!
           echo ren "%%b" "%%~nxa!num:~-4!%%~xb"
    )
   popd
  )
pause
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top