ANT使用 - 用for和foreach的方法遍历一个文件夹,查找到某个文件并删除
转自:http://www.cnblogs.com/QAZLIU/p/3732329.html?utm_source=tuicool&utm_medium=referral
build.xml
<?xml version="1.0"?>
<project name="ForTest" default="build" >
<property file="build.properties"></property>
<!-- import the ant contrib package for using the for or foreach -->
<taskdef resource="net/sf/antcontrib/antlib.xml"/>
<!-- for achieving the traversal of folder with "foreach" tag -->
<target name="foreach">
<echo message="Folders in the directory are:"/>
<foreach target="delete_file" param="dir.name">
<path>
<!--<dirset dir="${file.dir}" includes="*"/>-->
<fileset dir="${file.dir}" includes="jar.zip" ></fileset>
</path>
</foreach>
</target>
<!-- for achieving the traversal of folder with "for" tag -->
<target name="for">
<echo message="Folders in the directory are:"/>
<for param="dir.name">
<path>
<dirset dir="${file.dir}" includes="*" />
<fileset dir="${file.dir}" includes="*" ></fileset>
</path>
<sequential>
<echo message="@{dir.name}"/>
</sequential>
</for>
</target>
<!-- print the file under the folder-->
<target name="list.dirs">
<echo message="${dir.name}"/>
</target>
<!---delete file -->
<target name="delete_file">
<delete file="${dir.name}">
</delete>
</target>
<target name="build" depends="foreach" description="Test For loop"/>
</project>
build.properties
file.dir=G:\\_files
我先解释一下这个ant的运行顺序:
<project name="ForTest" default="build" >
先由这句入口找到build这个target。
也就是
<target name="build" depends="foreach" description="Test For loop"/>
这一句依赖foreach这个target,会找到
<target name="foreach">
一句一句执行,当执行到
<foreach target="delete_file" param="dir.name">
回去找delete_file这个target,也就是
<target name="delete_file">。
注意:
> <fileset dir="${file.dir}" includes="jar.zip" ></fileset>这句是要找出想要删除的zip包,在这里也就是jar.zip
> 现在脚本中用的遍历方式是ant contrib包下的foreach的方式遍历的。for的方式没有用到但是还是写出来了。
> <taskdef resource="net/sf/antcontrib/antlib.xml"/> 加上这一句才可以用for或者是foreach的方式遍历(有多种方法引入,share一个网址:http://blog.csdn.net/sodino/article/details/16923615)
> 这里面可能还比较疑惑的就是:红色标注的地方,这个参数也就是遍历的时候用到的。
<delete file="${dir.name}">这一句中的dir.name用的是用
<foreach target="delete_file" param="dir.name">遍历出来的文件名。
其他:http://blog.csdn.net/u012398902/article/details/51363549
最新文章
- MySQL数据库权限操作指南
- JavaScript-navigator_userAgent-编写一段代码能够区分浏览器的主流和区分
- springmvc入门基础之注解和参数传递
- SQL SERVER--单回话下的死锁
- Jenkins 插件 CIFS
- mongodb 监控分析命令
- Linux内核中双向链表的经典实现
- sdut Message Flood(c++ map)
- CSDN博客导出工具 Mac By Swift
- Jquery 类似新浪微博,鼠标移到头像,用浮动窗口显示用户信息,已做成一个jquery插件
- 创建 vxlan 并部署 instance - 每天5分钟玩转 OpenStack(147)
- 【转载】小tips: PC端传统网页试试使用Zepto.js进行开发
- LeetCode 548. Split Array with Equal Sum (分割数组使得子数组的和都相同)$
- freemarker常用值格式化方法
- Photoshop给人像加上个性裂纹肌肤
- 合并多个Excel
- Kettle实现数据抽取、转换、装入和加载数据-数据转移ETL工具
- 前端大神讲解,初级程序与高级程序写表格变色的区别,dom 0 与dom 1
- gitlab数据迁移
- Office word 2007不能另存为pdf格式的解决方法