如何使用Ant重命名文件和文件夹 [英] How to rename files and folders with Ant

查看:113
本文介绍了如何使用Ant重命名文件和文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Ant重命名许多文件和文件夹?对于文件,我知道我可以这样做. 问题

How to rename many files and folders with Ant? For files I know I can do it like this; question

如何对文件夹执行相同的操作?

How to do the same thing for folders?

例如 文件夹集(输入)

For e.g. Set of folders (Input)

com.google.appengine.eclipse.sdkbundle_1.5.2.r37v201107211953
com.google.gdt.eclipse.designer.doc.user_2.3.2.r37x201107161328
com.google.gdt.eclipse.designer.hosted.2_0.webkit_win32_2.3.2.r37x201107161253
org.eclipse.acceleo.common_3.1.0.v20110607-0602.jar

输出:

com.google.appengine.eclipse.sdkbundle_1.5.2
com.google.gdt.eclipse.designer.doc.user_2.3.2
com.google.gdt.eclipse.designer.hosted.2_0.webkit_win32_2.3.2
org.eclipse.acceleo.common_3.1.0.jar

推荐答案

对于复杂的操作,我使用

For complex operations I use the groovy ANT task.

以下示例将使用正则表达式重命名文件和目录:

The following example will rename your files and directories, using regular expressions:

<project name="demo" default="rename">

    <target name="bootstrap">
        <mkdir dir="${user.home}/.ant/lib"/>
        <get dest="${user.home}/.ant/lib/groovy-all.jar" src="http://search.maven.org/remotecontent?filepath=org/codehaus/groovy/groovy-all/2.0.6/groovy-all-2.0.6.jar"/>
    </target>

    <target name="rename">
        <taskdef name="groovy" classname="org.codehaus.groovy.ant.Groovy"/>

        <fileset id="filesToBeRenamed" dir="build"/>
        <dirset  id="dirsToBeRenamed" dir="build"/>

        <groovy>
            project.references.filesToBeRenamed.each { 
                String origName = it
                String newName = origName.replaceAll(/_[0-9\.]+[a-z0-9\-]+/, "")

                if (origName != newName) {
                    ant.move(file:origName, tofile:newName, verbose:"true")
                }
            }

            project.references.dirsToBeRenamed.each { 
                String origName = it
                String newName = origName.replaceAll(/_[0-9\.]+[a-z0-9\-]+/, "")

                if (origName != newName) {
                    ant.move(file:origName, tofile:newName, verbose:"true")
                }
            }
        </groovy>
    </target>
</project>

注意:

  • 引导"目标仅需要运行一次.它将从Maven Central下载groovy jar

这篇关于如何使用Ant重命名文件和文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆