重命名文件
发布时间:2020-12-14 17:10:13 所属栏目:大数据 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 //----------------------------------------------------------------------------------names = ['Pleac/src/abc.java','Pleac/src/def.groovy']nam
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 //---------------------------------------------------------------------------------- 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 => Pleacsrc9caelp.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:ProjectsGroovyExamplesPleacsrcpleac1.groovy.bak // C:ProjectsGroovyExamplesPleacsrcpleac2_beta.groovy // Projects.GroovyExamples.Pleac.src.3.xml // pleac_four.groovy 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |