0

I'm trying to rename the extension for multiple files at once using batch. However I am not sure I am going in the right direction. I am learning batch scripting.

Example ABCDEFRGGT.word.docx --> ABCDEFRGGT.docx

I have tried this so far but it does not work.

cd /d C:\Users\XXXX\Documents\rename
ren '*.word.docx' *.docx
aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • Not a solution, but: replace `'` by `"`! – aschipfl Nov 24 '17 at 12:48
  • 1
    Possible duplicate of [Rename all files in a directory with a Windows batch script](https://stackoverflow.com/questions/9383032/rename-all-files-in-a-directory-with-a-windows-batch-script) – MatSnow Nov 24 '17 at 12:53
  • 2
    Just as a comment: you are _not_ "renaming the extension". The files have the same extension after renamed: `.docx` – Aacini Nov 24 '17 at 14:45

1 Answers1

1

Use a for loop and its variable reference ~ modifiers to split off file name extensions, like this:

for %%I in ("%USERPROFILE%\Documents\rename\*.word.docx") do (
    rem // `%%~nI` returns the file name with the (last) extension removed:
    for %%J in ("%%~nI") do (
        rem /* `%%~nJ` returns the file name with the next-to-last extension removed too;
        rem    `%%~xI` returns the original (last) file name extension: */
        ren "%%~I" "%%~nJ%%~xI"
    )
)
aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • 1
    @user3758867, you don't need to include your `cd` command if you include it within the first set of parentheses. Here it is as a single batch file line, `For %%I In ("%UserProfile%\Documents\rename\*.word.docx") Do For %%J In ("%%~nI") Do Ren "%%~I" "%%~nJ%%~xI"`. – Compo Nov 24 '17 at 15:17