Sergio Ramos
Sergio Ramos

Reputation: 123

How to add date while creating a folder Nant

How to add current date while creating a folder in nant? I have <mkdir dir="${Test.Dir}" /> and as a result I need to get the folder created with name "TestDir-04-04-2022".

Any advise? Thanks.

Upvotes: 1

Views: 53

Answers (1)

Kiryl
Kiryl

Reputation: 180

In my case I only found solution to use embeded C# code with custom function. Possible solutions:

Variant 1:

<?xml version="1.0" encoding="utf-8"?>
<project name="Deployment" default="Deploy">
    <script language="C#" prefix="datetime">
        <code>
            <![CDATA[
                [Function("now-formatted")]
                public static string GetNowAsString(string format) {
                    return DateTime.Now.ToString(format);
                }
            ]]>
            </code>
    </script>
    <target name="DateTimeTest">
        <property name="Test.Dir" value="${'TestDir-' + datetime::now-formatted('dd-MM-yyyy')}" />
        <echo message="${Test.Dir}" />
        <mkdir dir="${Test.Dir}" />
    </target>
</project>

Variant 2:

<?xml version="1.0" encoding="utf-8"?>
<project name="Deployment" default="Deploy">
    <script language="C#" prefix="datetime">
        <code>
            <![CDATA[
                [Function("format")]
                public static string GetNowAsString(DateTime datetime, string format) {
                    return datetime.ToString(format);
                }
            ]]>
            </code>
    </script>
    <target name="DateTimeTest">
        <property name="Test.Dir" value="${'TestDir-' + datetime::format(datetime::now(), 'dd-mm-yyyy')}" />
        <echo message="${Test.Dir}" />
        <mkdir dir="${Test.Dir}" />
    </target>
</project>

Note: datetime::now - is standard nant function.


Output:

DateTimeTest:

     [echo] TestDir-08-23-2022
    [mkdir] Creating directory 'C:\Test\TestDir-08-23-2022'.

BUILD SUCCEEDED

Total time: 0.2 seconds.

Upvotes: 0

Related Questions