1

I want to delete all the files of certain type in in a folder. However, I only have the absolute path to the folder. What can I do?

For example, I might want to delete on the .txt files in C:\example.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
GC_
  • 1,673
  • 6
  • 23
  • 39

1 Answers1

0

This should do it:

<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="deletetxtfiles" name="Deletefiles">
    <target name="deletetxtfiles">
        <delete>
            <fileset dir="C:/Example" includes="*.txt"/>            
        </delete>
    </target> 
</project>

It deletes only txt files from C:\Example. Folders, other file types or txt files in subfolders will not be affected.

Susanne Muris
  • 276
  • 2
  • 10
  • If I were to run this ant task from C:\ant_project_home. Wouldn't the files I delete be C:\ant_project_home\C:\Example\\*.txt – GC_ Feb 16 '13 at 22:01
  • I think there has to be something to convert from relative path to absolute path. – GC_ Feb 16 '13 at 22:01
  • I don't think so. I'm calling ant from C:\antexample. This folder contains the build.xml. It deletes the files in C:\example. – Susanne Muris Feb 16 '13 at 22:12
  • Ant can detect drive letters and whether something is an absolute path. Because `/` is right after the drive letter, Ant knows it's an absolute path. – David W. Jul 02 '15 at 20:56