Search code examples
batch-filebatch-rename

Change extension of specific filetype on current folder and subdirectories with .bat


I would like to rename all the .log as .ok from a particular folder and subdirectories


Solution

  • The following will usually work just fine:

    @echo off
    for /r "PathToYourFolderHere" %%F in (.) do ren "%%F\*.log" *.ok
    

    But the above can have problems if short file names are enabled on your drive and you have extensions longer than 3 characters. It will also rename files like name.log2 because the short name will have an extension of .log.

    The following will only rename true .log files:

    @echo off
    for /f "eol=: delims=" %%F in (
      '"dir /b /s /a-d PathToYourFolder\*.log|findstr /lie .log"'
    ) do ren "%%F" *.ok
    

    Note: The rules for how RENAME treats wildcards can be found at How does the Windows RENAME command interpret wildcards?