Cmd 试图通过命令提示符ren command重命名一堆文件,但它';It’它没有给出期望的输出

Cmd 试图通过命令提示符ren command重命名一堆文件,但它';It’它没有给出期望的输出,cmd,Cmd,我试图通过命令提示符的ren命令重命名一堆htm文件,但它没有给出所需的输出 我的文件名为xyz_alb.htm,xyz_bla.htm…等等,它们位于不同的文件夹中,希望将它们重命名为zxy_alb.htm,zxy_bla.htm等等 我尝试了以下代码: for /r %x in (xyz*.htm) do ren "%x" zxy*.htm 但它替换了整个文件名,我得到的输出如下: zxy.htm, zxy.htm... 如何修改此代码以获得所需的输出?试试看 ren xyz_???.h

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

我的文件名为
xyz_alb.htm
xyz_bla.htm
…等等,它们位于不同的文件夹中,希望将它们重命名为
zxy_alb.htm
zxy_bla.htm
等等

我尝试了以下代码:

for /r %x in (xyz*.htm) do ren "%x" zxy*.htm
但它替换了整个文件名,我得到的输出如下:

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

试试看

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

不需要循环。第一个模式在xyz_u之后查找3个字符,后者匹配最多为“.”的任意数量的字符

对于当前目录类型的多个子目录:

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

在尝试了这么长时间之后,没有得到正确的解决方案,所以在我同事的帮助下,我尝试了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();
        }
    }
}
导入java.io.File;
/**
*此类使用开始文件字符序列进行搜索,并用新字符替换文件名。
*@作者nitin.choube
*
*/
公共类SearchAndReplaceFileName
{
公共静态void main(字符串[]args)
{
//开始搜索文件时的父文件路径
File dir=新文件(“D:\\WS\\Upload”);
//替换字符串
最后一个字符串replaceChar=“XYZ”;
//将字符串替换为
最终字符串replaceCharWtih=“ALB”;
//文件扩展名
最终字符串fileExtension=“.htm”;
if(dir.isDirectory()){
File[]children=dir.listFiles();
iterateFile(子项、replaceChar、replaceCharWtih、文件扩展名);
}       
}
/**
*此方法允许搜索和替换文件名。
*@param儿童
*@param replaceChar
*@param replaceCharWtih
*@param文件扩展名
*/
公共静态void iterateFile(文件[]子文件,字符串replaceChar,字符串replaceCharWtih,字符串文件扩展名){
试一试{

对于(int i=0;iforgot)来说,所有位于不同文件夹中的文件:(在我的电脑上工作的文件…(测试和试用)可能是双引号“%x”使您的命令发疯了…:D
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();
        }
    }
}