Groovy   发布时间:2022-04-12  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了重命名文件大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

下面是大佬教程code.js-code.com 通过网络收集整理的代码片段。

大佬教程小编现在分享给大家,也给大家做个参

//----------------------------------------------------------------------------------
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

大佬总结

以上是大佬教程为你收集整理的重命名文件全部内容,希望文章能够帮你解决重命名文件所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。