I want to use NAnt's foreach to iterate files in a folder, how to force alphabetic iteration?
Asked Answered
H

3

5

I have an NAnt task "ship" to package my current .sql scripts into a build, then name the build with an incrementing int {######} and copy it to a build folder.

I have another NAnt task which executes those build scripts.

They must execute in order, but in my last attempt, they were not. Can I "force" NAnt to work alphabetically?

Hortensiahorter answered 24/2, 2010 at 17:26 Comment(1)
Could You please include more details and a code snippet. I tried looping over files in a directory and apparently the files are iterated in alphabetical order.Repulse
H
4

FAIL:

<fileset basedir="source\tsql\builds\" id="buildfiles">
  <include name="*.sql.template.sql" /> 
  <exclude name="*.sql" /> 
  <exclude name="*asSentTo*" />
</fileset>
<foreach item="File" property"filename">
  <in refid="buildfiles">
    <echo message="${filename}" /> 
  </in>
</foreach>

PASS:

<foreach item="File" property="filename" in="source\tsql\builds"> 
  <do> 
    <if test="${string::ends-with(filename,'.sql.template.sql')}"> 
      <echo message="${filename}" /> 
    </if> 
  </do> 
</foreach>
Hortensiahorter answered 18/3, 2010 at 19:47 Comment(3)
Duncan to format the XML or code you need to add at least 4 spaces before each line. You had 2. I edited to format it. You can also select the text and use the code block icon (ones and zeros on the icon). You can click edit to see what I did.Indefeasible
This syntax isn't working for me: <in refid="buildfiles"> nant 0.92 is erroring: Unexpected attribute "refid" on element <in>Lordan
This is eight years old, so I'm going to be rusty! Did you define "buildfiles" already?Hortensiahorter
D
2

Here is how you do it with a fileset

<fileset id="mySet">
  <include name="*.sql" />
</fileset>
<copy>  
  <fileset refid="mySet" />
</copy>
<foreach item="File" property="filename">
  <in>
    <items refid="mySet" />
  </in>
  <do>
    <echo message="Copied files: ${filename} to directory: ${Folder}." />
  </do>
</foreach>
Daedal answered 19/10, 2016 at 20:40 Comment(0)
R
1

To satisfy my curiosity I tried to reproduce the problem with this script:

<?xml version="1.0"?>
<project name="foreach.test" default="foreach.alpha">
  <target name="foreach.alpha">
    <foreach item="File" in="C:\foo" property="filename">
      <do>
        <echo message="${filename}" />
      </do>
    </foreach>
  </target>
</project>

The filenames are printed out in alphabetical order. So conventional use of foreach already seems to be the solution to the problem.

Repulse answered 28/2, 2010 at 18:34 Comment(1)
How did you prettify your XML?Hortensiahorter

© 2022 - 2024 — McMap. All rights reserved.