Rename all files in a directory with a Windows batch script

后端 未结 2 434
温柔的废话
温柔的废话 2020-11-29 05:17

How would I write a batch or cmd file that will rename all files in a directory? I am using Windows.

Change this:

750_MOT_Forgiving_120x90.jpg
751_MO         


        
2条回答
  •  情歌与酒
    2020-11-29 05:55

    A FOR statement to loop through the names (type FOR /? for help), and string search and replace (type SET /? for help).

    @echo off
    setlocal enableDelayedExpansion
    for %%F in (*120x90.jpg) do (
      set "name=%%F"
      ren "!name!" "!name:120x90=67x100!"
    )
    


    UPDATE - 2012-11-07

    I've investigated how the RENAME command deals with wildcards: How does the Windows RENAME command interpret wildcards?

    It turns out that this particular problem can be very easily solved using the RENAME command without any need for a batch script.

    ren *_120x90.jpg *_67x100.*
    

    The number of characters after the _ does not matter. The rename would still work properly if 120x90 became x or xxxxxxxxxx. The important aspect of this problem is that the entire text between the last _ and the . is replaced.

提交回复
热议问题