Priya
Priya

Reputation:

Nant reading file in reverse order

I have a text file. I need to read the content of the file from the reverse order (from EOF). Please let me know how I can achieve it using Nant script.

Thanks, Priya.R

Upvotes: 1

Views: 325

Answers (2)

Lothar
Lothar

Reputation: 880

You could use the Windows program sort /R to create a sorted copy of the file.

Upvotes: 0

Nikolaos Georgiou
Nikolaos Georgiou

Reputation: 2874

You could write it in C# inside your NAnt script like this:

<target name="read">
    <script language="C#" prefix="myprefix" >
        <code>
            <![CDATA[
                [Function("reverse-lines")]
                public static string ReverseLines( string s )
                {
                    string[] lines = s.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                    string result = "";
                    foreach (string line in lines)
                    {
                        result = line + "\r\n" + result;
                    }

                    return result;
                }

            ]]>
        </code>
    </script>

    <loadfile file="myfile.txt" property="contents" />
    <echo message="File contents in correct order:" />
    <echo message="${contents}" />
    <echo message="File contents in reverse order:" />
    <echo message="${myprefix::reverse-lines(contents)}" />
</target>

Upvotes: 1

Related Questions