重命名文件

重命名文件,第1张

概述重命名文件

下面是内存溢出 jb51.cc 通过网络收集整理的代码片段。

内存溢出小编现在分享给大家,也给大家做个参考。

//----------------------------------------------------------------------------------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 filenamesop = args[0]println opfiles = 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.separatorext = { '.' + 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\srccaelp.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:/_beta./,casesensitive:'no')        chainedmapper{            packagemapper(from:'*pleac3.groovy',to:'*3.xml')            filtermapper(){ replacestring(from:'C:.',to:'') }        }        chainedmapper{            regexpmapper(from:/^(.*)4\.(.*)$/,to:/_4./)            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

以上是内存溢出(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。

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

总结

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

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1271947.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存