Search code examples
windowsbatch-filebatch-rename

Remove Prefix from all file in current folder as well as files in subfolder windows batch


i am trying to remove prefix from all files in current folder and subfolders i tryed following code which work only for current folder

setlocal enabledelayedexpansion
for %%F in (*) do (
  set "FN=%%F"
  set "FN=!FN:~15!"
  ren "%%F" "!FN!"
)
goto :eof

Please Help me to solve this


Solution

  • for /f "delims=" %%a in ('dir /b /a-d /s') do (
        set "fname=%%~nxa"
        set "fpath=%%~dpa"
        setlocal enabledelayedexpansion
        set "nname=!fname:~15!"
        ren "!fpath!!fname!" "!nname!"
        endlocal
    )
    

    This is the safe way to preserve exclamation marks.