Hakanai
Hakanai

Reputation: 12680

How can I match files with a space in the pattern in Ant?

Given a directory of source files like this:

tester$ ls -l src
-rw-r--r--   1 tester  staff    0 24 Feb 11:28 File 1.txt
-rw-r--r--   1 tester  staff    0 24 Feb 11:28 File 2.txt
-rw-r--r--   1 tester  staff    0 24 Feb 11:28 File 3.txt
-rw-r--r--   1 tester  staff    0 24 Feb 11:30 FileFalse 1.txt
-rw-r--r--   1 tester  staff    0 24 Feb 11:30 FileFalse 2.txt
-rw-r--r--   1 tester  staff    0 24 Feb 11:30 FileFalse 3.txt

I might try to copy them all to another location by using a fileset:

<project name="test" default="copy">
  <target name="copy">
    <mkdir dir="build"/>
    <copy todir="build">
      <fileset dir="src" includes="File *.txt"/>
    </copy>
  </target>
</project>

But include= treats space (and comma) as a separator, so this is treated as including "File" and "*.txt" - so it actually copies every file. The docs don't mention how you would escape the character if you wanted to use the literal character in a pattern, and reading the source, it seems they didn't put in any escape mechanism at all.

We had this as a real problem in our build, but we were only matching one file, so as a workaround I just used <fileset file="..."/>.

In general, though, the number of files could be large, or you might not want to update the build every time the files change, so what is the proper way to do this?

Upvotes: 4

Views: 1504

Answers (1)

greg-449
greg-449

Reputation: 111162

Use a fileset with nested include:

<fileset dir="src">
  <include name="File *.txt"/>
</fileset>

The name argument to include is a single pattern so spaces are not treated as separators.

Upvotes: 5

Related Questions