tags:

views:

456

answers:

5

Hello there,

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!

A: 

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

title %1 
%NANT_PATH%\nant.exe %1
zoidbeck
How I am taking your suggestion is that the cmd file changes the title and then runs a particular script. Is that what you mean?What I mean, is that during the running of a NAnt script within a Cmd windows, I want to be able to rename the title of the running window on an adhoc basis, as a way of indicating which part of the script the system is currently processing, as each script can run for 5 minutes or so.
Brett Rigby
You could make a cmd or batch that one after one builds the targets you want and on every new target call sets the title. Not very comfortable i must admit but should run without any additional requirements.
zoidbeck
A: 

This should work:

<exec>title Step One</exec>

<!-- Do some stuff -->

<exec>title Step Two</exec>

This uses a regular cmd.exe command.

Drew Noakes
No, sorry - I get this error back from NAnt...'program' is a required attribute of <exec ... />.
Brett Rigby
You're right. `title` is not an executable as such, but a special command recognised by `cmd.exe`.
Drew Noakes
+1  A: 

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.

Drew Noakes
Nope - tried this and it doesn't work, sorry.
Brett Rigby
+1  A: 

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.

tommieb75
Hmm - tried this. It's ok if the command window is in a control environment, but inside a NAnt script, this could be 'iffy', as if the build script crashes half way through, my new cmd session is still left open.
Brett Rigby
@Brett: hmmm ok, good point there!....see my amended answer above...
tommieb75
+2  A: 

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>
Lachlan Roche
Bang on the money! Good effort! Exactly what I was looking for.
Brett Rigby