Search code examples
windowsbatch-filecmdwildcardrename

Rename multiple files in cmd


If I have multiple files in a directory and want to append something to their filenames, but not to the extension, how would I do this?

I have tried the following, with test files file1.txt and file2.txt:

ren *.txt *1.1.txt

This renames the files to file1.1.txt and file2.txt1.1.txt

I want the files to be file1 1.1.txt and file2 1.1.txt

Will this be possible from cmd or do I need to have a bat file to do this? What about PowerShell?


Solution

  • for /f "delims=" %%i in ('dir /b /a-d *.txt') do ren "%%~i" "%%~ni 1.1%%~xi"
    

    If you use the simple for loop without the /f parameter, already renamed files will be again renamed.