In nant, how to delete contents of a directory, but not the directory itself?
Asked Answered
G

3

13

Suppose I were scripting a deployment using nant on a Windows server to a file share: \\server\share. I want a nant script to delete all files from the share then copy in new files.

I have this code to delete the files, but I'm getting an error that it can't delete "\server\share". But I didn't want to delete the share, just the contents in it.

<delete>
   <fileset basedir="\\server\share">
      <include name="**/**" />
   </fileset>
</delete>

Output:

BUILD FAILED

D:\code\xxx\xxx.deploy(177,8):
Cannot delete directory '\\server\share'.
    Access to the path '\\server\share' is denied.

If I modified it to instead delete contents of a directory in the share, say \\server\share\somedir, it'll delete "somedir" without error. But still, I didn't want to delete the dir, just the contents. Is there a way?

Gunthar answered 22/9, 2010 at 14:33 Comment(0)
P
26

This works for me - no workarounds required:

<delete>
    <fileset basedir="\\server\share">
        <include name="**\*" />
    </fileset>
</delete>
Premiere answered 14/10, 2010 at 8:38 Comment(1)
Tried this and it worked! Funny, I never saw this syntax in the nant docs.Gunthar
L
2

You could introduce an "exclude" tag and exclude a dummy file. That'll leave the root folder intact.

I'm using the following:

  <target name="clean">
    <delete>
      <fileset basedir="${DeployTo}">
        <include name="**/*" />
        <exclude name="**/aspnet_client/**" />
      </fileset>
    </delete>
  </target>
Lichter answered 22/9, 2010 at 14:42 Comment(0)
G
0

Taking cue from nsr81, I was able to come up with this workaround that works for me:

<touch file="${DeployTo}/deleteme" />
<delete>
   <fileset basedir="${DeployTo}">
      <include name="**/**" />
      <exclude name="deleteme" />
   </fileset>
</delete>
<delete file="${DeployTo}/deleteme" />
Gunthar answered 22/9, 2010 at 15:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.