Cmd batch rename subfolders / files

how can I rename files from folder A, which contains subfolders and files with this name of the format filename_ex.doc and make them look like filename.doc?

I tried to do this for a while, and it was an epic failure. please, help.

+3
source share
1 answer

I assume that you will also want to rename the files in the subfolders in which you had problems sending your second comment to Martin James. I assume you tried this code with the DIR / S option. But you were so close :-) You just had to use 2 cycles!

Change - fixed code . Once the result looks right, drop ECHO to make it work.

@echo off
for /r %%D in (.) do (
  pushd "%%~fD"
  for /f "tokens=1-3 delims=_." %%A in ('dir /b *_ex.ext 2^>nul') do echo ren "%%A_%%B.%%C" "%%A.%%C"
  popd
)

, _ ., , _ex.ext. , .

, ( Unicode). . , , . 3 . ECHO , .

@echo off
setlocal disableDelayedExpansion
for /f "delims=" %%F in ('dir /b /s *_ex.ext') do (
  set "old=%%F"
  set "new=%%~nF"
  setlocal enableDelayedExpansion
  echo ren "!old!" "!new:~0,-3!.ext"
  endlocal
)
+3

All Articles