I'm trying to rename a bunch of htm files through Command Prompt's ren command but it's not giving the desired output.

I have files called xyz_alb.htm, xyz_bla.htm...and so on located in different different folders and want to rename them as zxy_alb.htm, zxy_bla.htm and so on.

I have tried the below code:

for /r %x in (xyz*.htm) do ren "%x" zxy*.htm

But it's replacing the whole file name, I am getting the output like this:

zxy.htm, zxy.htm...

How can I modify to this code to get the desired output?

I am replacing 'NAMU_abl.htm' to 'KALKINSTA_abl.htm' in all folders and subfolder.

  • Which platform are you on? Windows or Linux?
    – pratnala
    Mar 13, 2013 at 9:46

3 Answers 3


I'm using the free tool StExBar for this, an explorer extension. Very comfortable and it has more features. Info and download: StExBar


The ren xyz*.htm zxy*.htm can only be correctly interpreted when the ren command does the wildcard processing itself. You are processing the wildcard with the for command, and so ren sees this each time:

ren xyz_alb.htm zxy*.htm

As it has no wildcard to match, it does not know which part of the source filename to use for the replacement, and so it resolves to:

ren xyz_alb.htm zxy.htm

So what you want to do is recurse through the directories, then do a rename in each one:

for /d /r %x in (*) do cd "%x" && ren xyz*.htm zxy*.htm

The /d means directories only, and the /r recurses as usual. %x becomes the directory name, and so we change into each directory and any files that match the first parameter will get renamed. You'll want to start this above the first folder that contains the files to be renamed.

  • I suggest using quotes around %x. If the directory names contain spaces, they will be parsed as delimiters and the wrong files might be renamed. Mar 13, 2013 at 12:00
  • @MarcksThomas Thanks - feel free to edit answers to increase quality
    – Paul
    Mar 13, 2013 at 12:19
  • @Paul.. I am getting the error that 'The syntax of the command is inccorect'
    – Kingk
    Mar 14, 2013 at 5:51
  • @Kingk Ooops, sorry, poor testing. The rename command doesn't accept directory paths, so I have changed it to cd into each directory first.
    – Paul
    Mar 14, 2013 at 6:36
  • No Luck Paul... it's still replacing the whole file name not a part of file name :(
    – Kingk
    Mar 14, 2013 at 6:54

after trying so long on this didn't got the proper solution so have tried through java with the help of my colleague..thanks to him.

Would like to share the same if someone need it.

import java.io.File;

 * This class is search with start file character sequence and replace the file name with new character. 
 * @author nitin.choube

public class  SearchAndReplaceFileName
    public static void main(String[] args) 
        //Parent file path from start searching files
        File dir = new File("D:\\WS\\Upload");
        // replace character string
        final String replaceChar="XYZ";
        // replace character string with
        final String replaceCharWtih="ALB";         
        // file extension
        final String fileExtension=".htm";


            File[] children = dir.listFiles();


     * This method is allow to search and replace file name.
     * @param children
     * @param replaceChar
     * @param replaceCharWtih
     * @param fileExtension
    public static void iterateFile(File[] children,String replaceChar,String replaceCharWtih,String fileExtension){
        try {

            for (int i=0; i<children.length; i++) {

                // Get filename of file or directory

                File file = children[i];

                System.out.println("Getting all files in " + file.getCanonicalPath() + " including those in subdirectories");


                      File[] child = file.listFiles();


                  }else if(file.isFile()){

                      String extension = file.getName().substring(file.getName().lastIndexOf("."));

                      System.out.println("extracted file name is "+file.getName()+" and extension is ="+extension);


                          String fileName=file.getName().substring(0,file.getName().lastIndexOf(fileExtension));


                              String newFileName=fileName.replace(replaceChar,replaceCharWtih);
                              file.renameTo(new File(file.getCanonicalPath().substring(0,file.getCanonicalPath().lastIndexOf("\\"))+"\\"+newFileName+fileExtension));

                          }else if(fileName.contains("_"+replaceChar+"_")){

                              String newFileName=fileName.replace(replaceChar,replaceCharWtih);
                              file.renameTo(new File(file.getCanonicalPath().substring(0,file.getCanonicalPath().lastIndexOf("\\"))+"\\"+newFileName+fileExtension));

        } catch (Exception e) {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.