0

I'm writing a script to produce some artefacts from my build so I want to clean up unwanted files first. I'm using CleanDirectory(dirPath, predicate).

I'm finding it disturbingly hard to work out the directory for a file. If I use GetDirectoryName() that seems to just get me the immediate parent, not the full directory path.

Func<IFileSystemInfo, bool> predicate =
        fileSystemInfo => {

            // Dont filter out any directories
            if (fileSystemInfo is IDirectory)
                return false;

           var path = fileSystemInfo.Path.FullPath;

           var directory = ((DirectoryPath)path).GetDirectoryName();
           ...
}

Obviously I can use the .NET Framework System.IO classes to do this easily but then I get strings with the slashes in the wrong direction, and things do not smoothly inter-operate with Cake which uses POSIX paths.

Jack Ukleja
  • 13,061
  • 11
  • 72
  • 113

1 Answers1

0

OK I've worked out a solution. The key to IFileSystemInfo is to try and cast the Path to various derived types/interfaces, which then provide the functionality you are probably looking for. Example:

 Func<IFileSystemInfo, bool> predicate =
        fileSystemInfo => {

            // Dont filter out any directories
            if (fileSystemInfo is IDirectory)
                return false;

            // We can try and cast Path as an FilePath as know it's not a directory
            var file = (FilePath) fileSystemInfo.Path;

            if (file.FullPath.EndsWith("Help.xml", StringComparison.OrdinalIgnoreCase))
                        return false;

            // GetDirectory() returns a Path of type DirectoryPath
            var directory = file.GetDirectory().FullPath;
            ...
    }
Jack Ukleja
  • 13,061
  • 11
  • 72
  • 113