尝试通过命令提示符ren命令重命名一堆文件,但它没有提供所需的输出

时间:2013-03-13 06:58:12

标签: cmd

我正在尝试通过命令提示符的ren命令重命名一堆htm文件,但它没有提供所需的输出。

我的文件名为xyz_alb.htmxyz_bla.htm ...等位于不同的不同文件夹中,并希望将其重命名为zxy_alb.htmzxy_bla.htm等等。

我尝试过以下代码:

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

但它正在替换整个文件名,我得到的输出如下:

zxy.htm, zxy.htm...

如何修改此代码以获得所需的输出?

2 个答案:

答案 0 :(得分:1)

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

ren xyz_*.* zxy_*.*

不需要循环。第一个模式在xyz_之后查找3个字符,后者匹配任意数量的字符,直到'。'

对于当前目录中的多个子目录,请键入:

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

答案 1 :(得分:0)

经过这么长时间的尝试,没有得到正确的解决方案,所以在我的同事的帮助下尝试了java ..谢谢他。

如果有人需要,可以分享同样的内容。

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