Question

I would like to be able to change the title of the Command window at various points throughout my NAnt script.

I have tried to use the task to call 'title myTargetName' but it gives me the following error:

'title' failed to start.

The system cannot find the file specified

Is there a way to do this please? Thanks in advance!

Was it helpful?

Solution

You can set the console title in a custom task. If the task is defined in a script, the build file is self contained.

The console title will revert once nant finishes.

<project default="title">

    <target name="title">
        <consoletask title='step 1'/>
        <sleep minutes="1" />
        <consoletask title='step 2'/>
        <sleep minutes="1" />
        <consoletask title='step 3'/>
        <sleep minutes="1" />
    </target>

    <script language="C#">
        <code>
            [TaskName("consoletask")]
            public class TestTask : Task
            {
                private string title;

                [TaskAttribute("title", Required=true)]
                public string Title
                {
                    get { return title; }
                    set { title = value; }
                }

                protected override void ExecuteTask() {
                    System.Console.Title = title;
                }
            }
        </code>
    </script>
</project>

OTHER TIPS

If you compile this small program as a console app:

namespace SetTitle
{
    internal static class Program
    {
        private static void Main(string[] args)
        {
            System.Console.Title = string.Join(" ", args);
        }
    }
}

Then this would work:

<exec>SetTitle.exe "Step One"</exec>

<!-- Do some stuff -->

<exec>SetTitle.exe "Step Two"</exec>

You could do the same with a custom NAnt task, but the work involved would be more complicated and you'd still have to make your NAnt task assembly discoverable during the script's execution.

Try this:

' In your command prompt
title foobar

' The title now should say 'foobar' without quotes

' Now issue this...
cmd /k fubar

' The title now should say 'fubar' without quotes

So I guess you need to change it to like this:

<exec>cmd /k title one </exec>

Edit: At the end of the script, invoke the exit command to exit the nested levels of the cmd.exe command line processor...Suppose you have three 'exec' for the 'cmd /k', you would need three 'exit' commands in order to get back to the original cmd.exe shell, think of it like popping cmd.exe off the stack for the duration of the nant script...

Edit#2: As per Brett's comment...just a thought - why not do it this way....

<exec>cmd /k title one </exec>
<exec>exit</exec>

Add the 'exit' command immediately after setting the title of the window...?

Hope this helps, Best regards, Tom.

You could use a cmd or batch file to run the nant script containing this:

title %1 
%NANT_PATH%\nant.exe %1

This should work:

<exec>title Step One</exec>

<!-- Do some stuff -->

<exec>title Step Two</exec>

This uses a regular cmd.exe command.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top