Kingk
Kingk

Reputation: 1023

Trying to rename a bunch of files through command prompt ren command but it's not giving the desired output

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?

Upvotes: 0

Views: 423

Answers (2)

Kingk
Kingk

Reputation: 1023

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";

        if(dir.isDirectory()){

            File[] children = dir.listFiles();
            iterateFile(children,replaceChar,replaceCharWtih,fileExtension);

        }       
    }

    /**
     * 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");

                  if(file.isDirectory()){

                      File[] child = file.listFiles();

                      iterateFile(child,replaceChar,replaceCharWtih,fileExtension);

                  }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);

                      if(extension.equals(fileExtension)){

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

                          if(fileName.startsWith(replaceChar)){

                              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) {
            e.printStackTrace();
        }
    }
}

Upvotes: 0

Volkan
Volkan

Reputation: 2210

try

ren xyz_???.htm zxy_???.htm

or

ren xyz_*.* zxy_*.*

no loops required. The first pattern looks for 3 characters after xyz_ the latter matches any number of characters up to '.' .

For multiple subdirectorys at your current directory type:

for /r %d in (xyz_*.*) do ren %d zxy_*.*

Upvotes: 1

Related Questions