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

    在Java5之后,并发线程这块发生了根本的变化,最重要的莫过于新的启动.调度.管理线程的一大堆API了.在Java5以后,通过 Executor来启动线程比用Thread的start()更好.在新特征 ...

  2. android 跨进程通信

    转自:http://www.androidsdn.com/article/show/137 由于android系统中应用程序之间不能共享内存.因此,在不同应用程序之间交互数据(跨进程通讯)就稍微麻烦一 ...

  3. ef unitofwork 主从表更新

    readonly UnitOfWork _u = new UnitOfWork(); public M Get(int id) { return _u.T_MtnContractRepository( ...

  4. Activity介绍

    1.Activity使用方法(跳转): (1)写Activity类继承Activity package com.example.test2; import android.app.Activity; ...

  5. Appium+python自动化9-SDK Manager【转载】

    前言 SDK Manager到有哪些东西是必须安装的呢? 一.SDK Manager 1.双击打开SDK Manager界面

  6. [BZOJ1260][CQOI2007]涂色paint 区间dp

    1260: [CQOI2007]涂色paint Time Limit: 30 Sec  Memory Limit: 64 MB Submit: 1575  Solved: 955 [Submit][S ...

  7. uva11019矩阵匹配器D316

    #include<iostream> #include<cstring> #include<cstdio> #include<cmath> #inclu ...

  8. HTML5面向对象的游戏开发简单实例总结

    在阅读一本HTML5游戏开发相关书籍时发现一个很好的例子,通过这个例子可以对面向对象的开发进行更深入的理解.这个对象要实现的是:将一个CSS sprite中的图像绘制到canvas中.首先创建一个Sp ...

  9. PyCharm配置gitHub远程仓储

    在一个团队里,编码不能是闭门造车,git学起来: 1. GIT的基本介绍.安装及使用教程- @廖雪峰 2. pycharm配置github远程仓储- @谢小小XH

  10. COCOS2d 标准 android.MK

    LOCAL_PATH := $(call my-dir) include$(CLEAR_VARS) LOCAL_MODULE := game_shared PP_CPPFLAGS := -frtti ...