转自: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

ANT使用 - 用for和foreach的方法遍历一个文件夹,查找到某个文件并删除的更多相关文章

  1. PHP5实现foreach语言结构遍历一个类的实例

    PHP5实现foreach语言结构遍历一个类 创建一个类集成Iterator接口,并实现Iterator里面的方法即可,下面见实例代码实现 <?php class Test implements ...

  2. java:多层文件夹情况下,判断文件夹下是否有文件夹,并获取到没有文件夹的名字的方法

    业务问题案例 在公司遇到的一个问题,本以为很小很好解决,没想到花了一下午时间.图给的是文件路径,page1下有10个文件夹,每个有的有文件夹或者文件,要求得到page1下(即:123456789,10 ...

  3. C#获取文件夹下的所有文件的方法

    目录 #基础知识 #只获取目录下一级的文件夹与文件 # 递归地输出当前运行程序所在的磁盘下的所有文件名和子目录名 正文   #基础知识 1.获得当前运行程序的路径 1 string rootPath ...

  4. VC下遍历文件夹中的所有文件的几种方法

    一.使用::FindFirstFile和::FindNextFile方法 #include "StdAfx.h" #include <windows.h> #inclu ...

  5. win10锁屏壁纸文件夹Assets中无文件问题的解决方法

    一.前言 win10在锁屏时会有很多精美的壁纸,在网上查找到win10锁屏壁纸存放目录为 : C:\Users\你的用户名\AppData\Local\Packages\Microsoft.Windo ...

  6. C/C++不同文件夹下包含头文件的方法及#include的使用

    转自:http://blog.sina.com.cn/s/blog_6e0693f70100so42.html 本文主要介绍了如何不同文件夹下使用预处理器指示符#include. 假设我们有如下一个工 ...

  7. Python引用(import)文件夹下的py文件的方法

    Python的import包含文件功能就跟PHP的include类似,但更确切的说应该更像是PHP中的require,因为Python里的import只要目标不存在就报错程序无法往下执行.要包含目录里 ...

  8. Node.js 内置模块fs的readdir方法 查看某个文件夹里面包含的文件内容

    fs.readdir(path[, options], callback) 例: "use strict"; const fs = require("fs"); ...

  9. VS App_Code文件夹下的类文件不能直接被调用的解决方法

    如下图所示,新建的类不能直接使用,会显示报错,检查命名空间什么的,未果 通过百度搜索,发现这么一篇文章:https://blog.csdn.net/younghaiqing/article/detai ...

随机推荐

  1. Registering RHEL6 Clients into spacewalk

    Before Starting(login to spacwalk server) 1.Create a base channel within Spacewalk (Channels > Ma ...

  2. HDU1251统计难题---Trie Tree

    map巧过 #include <stdio.h> #include <string.h> #include <map> #include <string> ...

  3. 行为型设计模式之迭代器模式(Iterator)

    结构 意图 提供一种方法顺序访问一个聚合对象中各个元素, 而又不需暴露该对象的内部表示. 适用性 访问一个聚合对象的内容而无需暴露它的内部表示. 支持对聚合对象的多种遍历. 为遍历不同的聚合结构提供一 ...

  4. matlab7 + sqlitejdbc-v056.jar出现错误

    conn=database('data.db','','','org.sqlite.JDBC','jdbc:sqlite:C:/MATLAB7/work/del_man_voice_from_wave ...

  5. 颜色混合opengl--glBlendFunc函数

    http://www.cnblogs.com/ylwn817/archive/2012/09/07/2675285.html 颜色混合opengl--glBlendFunc函数 原文:http://b ...

  6. Error:Execution failed for task ':app:transformClassesWithDexForDebug'. > com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.Exec

    Error:Execution failed for task ':app:transformClassesWithDexForDebug'.> com.android.build.api.tr ...

  7. Reporting Services的简单使用

    最近公司的功能需要使用报表,用的是微软自带的报表,谈一谈我们的做法,希望可以给想学习的人一些指导 1:新建報表所需的數據源DataSet.cs using System; using System.C ...

  8. HDU 2546 饭卡【贪心+01背包】

    饭卡 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submiss ...

  9. Codeforces 1010D Mars rover

    题目大意:对于一个不完全二分图,根节点为1,叶节点值为0或1,非叶节点包含一个操作(and,or,xor,not),求改变各个叶节点的值时(即0改为1,1改为0),根节点的值是多少 解法:遍历图求各节 ...

  10. 数学【p1658】 购物

    题目描述 你就要去购物了,现在你手上有N种不同面值的硬币,每种硬币有无限多个.为了方便购物,你希望带尽量少的硬币,但要能组合出1到X之间的任意值. 分析: 看到题解做法没有说出原理,所以尝试解释一下. ...