返回顶部

收藏

重命名文件

更多
//----------------------------------------------------------------------------------
names = ['Pleac/src/abc.java', 'Pleac/src/def.groovy']
names.each{ name -> new File(name).renameTo(new File(name + '.bak')) }

// The Groovy way of doing rename using an expr would be to use a closure
// for the expr:
// groovySimpleRenameScript:
#!/usr/bin/groovy
// usage rename closure_expr filenames
op = args[0]
println op
files = args[1..-1]
shell = new GroovyShell(binding)
files.each{ f ->
    newname = shell.evaluate("$op('$f')")
    new File(f).renameTo(new File(newname))
}

// this would allow processing such as:
//% rename "{n -> 'FILE_' + n.toUpperCase()}" files
// with param pleac9.groovy => FILE_PLEAC9.GROOVY
//% rename "{n -> n.replaceAll(/9/,'nine') }" files
// with param pleac9.groovy => pleacnine.groovy
// The script could also be modified to take the list of
// files from stdin if no args were present (not shown).

// The above lets you type any Groovy code, but instead you might
// decide to provide the user with some DSL-like additions, e.g.
// adding the following lines into the script:
sep = File.separator
ext = { '.' + it.tokenize('.')[-1] }
base = { new File(it).name - ext(it) }
parent = { new File(it).parent }
lastModified = { new Date(new File(it).lastModified()) }
// would then allow the following more succinct expressions:
//% rename "{ n -> parent(n) + sep + base(n).reverse() + ext(n) }" files
// with param Pleac/src/pleac9.groovy => Pleac\src\9caelp.groovy
//% rename "{ n -> base(n) + '_' + lastModified(n).year + ext(n) }" files
// with param pleac9.groovy => pleac9_07.groovy

// As a different alternative, you could hook into Ant's mapper mechanism.
// You wouldn't normally type in this from the command-line but it could
// be part of a script, here is an example (excludes the actual rename part)
ant = new AntBuilder()
ant.pathconvert(property:'result',targetos:'windows'){
    path(){ fileset(dir:'Pleac/src', includes:'pleac?.groovy') }
    compositemapper{
        globmapper(from:'*1.groovy', to:'*1.groovy.bak')
        regexpmapper(from:/^(.*C2)\.(.*)$/, to:/\1_beta.\2/, casesensitive:'no')
        chainedmapper{
            packagemapper(from:'*pleac3.groovy', to:'*3.xml')
            filtermapper(){ replacestring(from:'C:.', to:'') }
        }
        chainedmapper{
            regexpmapper(from:/^(.*)4\.(.*)$/, to:/\1_4.\2/)
            flattenmapper()
            filtermapper(){ replacestring(from:'4', to:'four') }
        }
    }
}
println ant.antProject.getProperty('result').replaceAll(';','\n')
// =>
// C:\Projects\GroovyExamples\Pleac\src\pleac1.groovy.bak
// C:\Projects\GroovyExamples\Pleac\src\pleac2_beta.groovy
// Projects.GroovyExamples.Pleac.src.3.xml
// pleac_four.groovy

标签:groovy

收藏

0人收藏

支持

0

反对

0

相关聚客文章
  1. TiuVe2 发表 2018-09-04 14:15:19 按钮条件逻辑配置化的可选技术方案
  2. 炒饭 发表 2015-11-25 03:20:12 用git布署网站的方法
  3. 炒饭 发表 2015-11-25 03:20:12 用git布署网站的方法
  4. 博主 发表 2018-04-25 06:46:00 Groovy Grape
  5. 博主 发表 2018-04-26 12:23:00 Groovy Ratpack
  6. 博主 发表 2018-04-26 12:23:00 Groovy Ratpack
  7. 博主 发表 2018-04-25 06:46:00 Groovy Grape
  8. 博主 发表 2018-02-07 09:05:17 IDEA 通过数据库生成 Nutz 实体类
  9. TiuVe2 发表 2018-01-06 13:23:06 使用 yaml+groovy 实现 Java 代码可配置化
  10. 博主 发表 2015-10-02 00:00:00 精通 Groovy
  11. 炒饭 发表 2015-11-25 03:20:12 用git布署网站的方法
  12. 炒饭 发表 2015-11-25 03:20:12 用git布署网站的方法

发表评论