File Class

MSBuild Extension Pack

File Class MSBuild Extension Pack Help 4.0.12.0
Valid TaskActions are:

AddAttributes (Required: Files)

AddSecurity (Required: Users, AccessType, Path or Files Optional: Permission

CheckContainsContent (Required: Files, RegexPattern Optional: RegexOptionList Output: Result)

Concatenate (Required: Files, TargetPath)

CountLines (Required: Files Optional: CommentIdentifiers, MazSize, MinSize Output: TotalLinecount, CommentLinecount, EmptyLinecount, CodeLinecount, TotalFilecount, IncludedFilecount, IncludedFiles, ExcludedFilecount, ExcludedFiles, ElapsedTime)

Create (Required: Files Optional: Size). Creates file(s)

GetChecksum (Required: Path Output: Checksum)

GetTempFileName (Output: Path)

FilterByContent (Required: Files, RegexPattern Optional: RegexOptionList Output: IncludedFiles, IncludedFilecount, ExcludedFilecount, ExcludedFiles)

Move (Required: Path, TargetPath)

RemoveAttributes (Required: Files)

RemoveLines (Required: Files, Lines Optional: RegexOptionList, AvoidRegex, MatchWholeLine). This will remove lines from a file. Lines is a regular expression unless AvoidRegex is specified

RemoveSecurity (Required: Users, AccessType, Path or Files Optional: Permission

Replace (Required: RegexPattern Optional: Replacement, Path, TextEncoding, Files, RegexOptionList)

SetAttributes (Required: Files)

WriteLines (Required: Files, Lines). This will add lines to a file if the file does NOT contain them. The match is case insensitive.

Remote Execution Support: No

Inheritance Hierarchy

SystemObject  Microsoft.Build.UtilitiesTask
    MSBuild.ExtensionPackBaseTask
      MSBuild.ExtensionPack.FileSystemFile

Namespace: MSBuild.ExtensionPack.FileSystem
Assembly: MSBuild.ExtensionPack (in MSBuild.ExtensionPack.dll) Version: 4.0.0.0

The File type exposes the following members.

Constructors

  NameDescription
Public methodFile
Top
Properties

  NameDescription
Public propertyAccessType
Set the AccessType. Can be Allow or Deny. Default is Allow.
Public propertyAvoidRegex
Set to true to avoid using Regular Expressions. This may increase performance for certain operations against large files.
Public propertyChecksum
Gets the file checksum
Public propertyCodeLinecount
Gets the number of code lines countered. This is calculated as Total - Comment - Empty
Public propertyCommentIdentifiers
Sets characters to be interpreted as comment identifiers. Semi-colon delimited. Only single line comments are currently supported.
Public propertyCommentLinecount
Gets the number of comment lines counted
Public propertyElapsedTime
Gets the time taken to count the files. Value in seconds.
Public propertyEmptyLinecount
Gets the number of empty lines countered. Whitespace is ignored.
Public propertyExcludedFilecount
Gets the number of excluded files
Public propertyExcludedFiles
Item collection of files Excluded from the count.
Public propertyFiles
An ItemList of files to process. If calling SetAttributes, RemoveAttributes or AddAttributes, include the attributes in an Attributes metadata tag, separated by a semicolon.
Public propertyIncludedFilecount
Gets the number of included files
Public propertyIncludedFiles
Item collection of files included after filtering operations
Public propertyLines
Sets the Lines to use. For WriteLines this is interpreted as plain text. For RemoveLines this is interpreted as a regular expression
Public propertyMatchWholeLine
Used with AvoidRegex. Set to true to match the whole line. The default is false i.e. a line.Contains operation is used.
Public propertyMaxSize
Sets the maximum size of files to count
Public propertyMinSize
Sets the minimum size of files to count
Public propertyPath
A path to process or get. Use * for recursive folder processing. For the GetChecksum TaskAction, this indicates the path to the file to create a checksum for.
Public propertyPermission
A comma-separated list of FileSystemRights.
Public propertyRegexOptionList
Sets the Regular Expression options, e.g. None|IgnoreCase|Multiline|ExplicitCapture|Compiled|Singleline|IgnorePatternWhitespace|RightToLeft|RightToLeft|ECMAScript|CultureInvariant Default is RegexOptions.Compiled
Public propertyRegexPattern
Sets the regex pattern.
Public propertyReplacement
The replacement text to use. Default is string.Empty
Public propertyResult
Gets the result
Public propertySize
Sets the size of the file in bytes for TaskAction="Create". This can be overridden by using a metadata tag called size on the Files items.
Public propertyTargetPath
Sets the TargetPath for a renamed file or to save concatenated files
Public propertyTextEncoding
The file encoding to write the new file in. The task will attempt to default to the current file encoding.
Public propertyTotalFilecount
Gets the number of files counted
Public propertyTotalLinecount
Gets the total number of lines counted
Public propertyUsers
Sets the users collection. Use the Permission metadata tag to specify permissions. Separate pemissions with a comma.
Remarks

The Permission metadata is only used if the Permission property is not set.
<UsersCol Include="AUser">
    <Permission>Read,etc</Permission>
</UsersCol>
Top
Examples

<Project ToolsVersion="4.0" DefaultTargets="Default" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
    <PropertyGroup>
        <TPath>$(MSBuildProjectDirectory)\..\MSBuild.ExtensionPack.tasks</TPath>
        <TPath Condition="Exists('$(MSBuildProjectDirectory)\..\..\Common\MSBuild.ExtensionPack.tasks')">$(MSBuildProjectDirectory)\..\..\Common\MSBuild.ExtensionPack.tasks</TPath>
    </PropertyGroup>
    <Import Project="$(TPath)"/>
    <ItemGroup>
        <FilesToParse Include="c:\demo\file.txt"/>
        <FilesToCount Include="C:\Demo\**\*.cs"/>
        <AllFilesToCount Include="C:\Demo\**\*"/>
        <AtFiles Include="c:\demo\file1.txt">
            <Attributes>ReadOnly;Hidden</Attributes>
        </AtFiles>
        <AtFiles2 Include="c:\demo\file1.txt">
            <Attributes>Normal</Attributes>
        </AtFiles2>
        <MyFiles Include="C:\demo\**\*.csproj"/>
        <FilesToSecure Include="C:\demo\file1.txt" />
        <FilesToSecure Include="C:\demo\file2.txt" />
        <Users Include="MyUser" />
        <UsersWithPermissions Include="MyUser">
            <Permission>Read,Write</Permission>
        </UsersWithPermissions>
        <FilesToWriteTo Include="C:\a\hosts"/>
        <LinesToRemove Include="192\.156\.236\.25 www\.myurl\.com"/>
        <LinesToRemove Include="192\.156\.234\.25 www\.myurl\.com"/>
        <LinesToRemove Include="192\.156\.23sss4\.25 www\.myurl\.com"/>
        <Lines Include="192.156.236.25 www.myurl.com"/>
        <Lines Include="192.156.234.25 www.myurl.com"/>
        <FilesToCreate Include="d:\a\File1-100.txt"/>
        <FilesToCreate Include="d:\a\File2-100.txt"/>
        <FilesToCreate Include="d:\a\File3-5000000.txt">
            <size>5000000</size>
        </FilesToCreate>
        <FilesToCreate Include="d:\a\File4-100.txt"/>
        <FilesToCheck Include="d:\a\*.*"/>
        <FilesToConcatenate Include="c:\a\*.proj"/>
    </ItemGroup>
    <Target Name="Default">
        <!-- Concatenate Files -->
        <MSBuild.ExtensionPack.FileSystem.File TaskAction="Concatenate" Files="@(FilesToConcatenate)" TargetPath="c:\concatenatedfile.txt"/>
        <!-- Check whether files contain matching content -->
        <MSBuild.ExtensionPack.FileSystem.File TaskAction="CheckContainsContent" Files="@(FilesToCheck)" RegexPattern="Hello">
            <Output TaskParameter="Result" PropertyName="TheResult"/>
        </MSBuild.ExtensionPack.FileSystem.File>
        <Message Text="$(TheResult)"/>
        <!-- Create some files. Defaults the size to 1000 bytes, but one file overrides this using metadata -->
        <MSBuild.ExtensionPack.FileSystem.File TaskAction="Create" Files="@(FilesToCreate)" Size="1000"/>
        <!-- Write lines to a file. Lines only added if file does not contain them -->
        <MSBuild.ExtensionPack.FileSystem.File TaskAction="WriteLines" Files="@(FilesToWriteTo)" Lines="@(Lines)"/>
        <!-- Remove lines from a file based on regular expressions -->
        <MSBuild.ExtensionPack.FileSystem.File TaskAction="RemoveLines" Files="@(FilesToWriteTo)" Lines="@(LinesToRemove)"/>
        <!-- adding security -->
        <MSBuild.ExtensionPack.FileSystem.File TaskAction="AddSecurity" Path="C:\demo\file3.txt" Users="@(Users)" AccessType="Allow" Permission="Read,Write" />
        <MSBuild.ExtensionPack.FileSystem.File TaskAction="AddSecurity" Files="@(FilesToSecure)" Users="@(UsersWithPermissions)" AccessType="Deny" />
        <!-- remove security -->
        <MSBuild.ExtensionPack.FileSystem.File TaskAction="RemoveSecurity" Path="C:\demo\file4.txt" Users="@(Users)" AccessType="Allow" Permission="Read,Write" />
        <MSBuild.ExtensionPack.FileSystem.File TaskAction="RemoveSecurity" Files="@(FilesToSecure)" Users="@(UsersWithPermissions)" AccessType="Deny" />
        <!-- Get a temp file -->
        <MSBuild.ExtensionPack.FileSystem.File TaskAction="GetTempFileName">
            <Output TaskParameter="Path" PropertyName="TempPath"/>
        </MSBuild.ExtensionPack.FileSystem.File>
        <Message Text="TempPath: $(TempPath)"/>
        <!-- Filter a collection of files based on their content -->
        <Message Text="MyProjects %(MyFiles.Identity)"/>
        <MSBuild.ExtensionPack.FileSystem.File TaskAction="FilterByContent" RegexPattern="Microsoft.WebApplication.targets" Files="@(MyFiles)">
            <Output TaskParameter="IncludedFiles" ItemName="WebProjects"/>
            <Output TaskParameter="ExcludedFiles" ItemName="NonWebProjects"/>
            <Output TaskParameter="IncludedFileCount" PropertyName="WebProjectsCount"/>
            <Output TaskParameter="ExcludedFileCount" PropertyName="NonWebProjectsCount"/>
        </MSBuild.ExtensionPack.FileSystem.File>
        <Message Text="WebProjects: %(WebProjects.Identity)"/>
        <Message Text="NonWebProjects: %(NonWebProjects.Identity)"/>
        <Message Text="WebProjectsCount: $(WebProjectsCount)"/>
        <Message Text="NonWebProjectsCount: $(NonWebProjectsCount)"/>
        <!-- Get the checksum of a file -->
        <MSBuild.ExtensionPack.FileSystem.File TaskAction="GetChecksum" Path="C:\Projects\MSBuildExtensionPack\Solutions\Main3.5\SampleScratchpad\SampleBuildBinaries\AssemblyDemo.dll">
            <Output TaskParameter="Checksum" PropertyName="chksm"/>
        </MSBuild.ExtensionPack.FileSystem.File>
        <Message Text="$(chksm)"/>
        <!-- Replace file content using a regular expression -->
        <MSBuild.ExtensionPack.FileSystem.File TaskAction="Replace" RegexPattern="regex" RegexOptionList="IgnoreCase|Singleline" Replacement="iiiii" Files="@(FilesToParse)"/>
        <MSBuild.ExtensionPack.FileSystem.File TaskAction="Replace" RegexPattern="regex" Replacement="idi" Path="c:\Demo*"/>
        <!-- Count the number of lines in a file and exclude comments -->
        <MSBuild.ExtensionPack.FileSystem.File TaskAction="CountLines" Files="@(FilesToCount)" CommentIdentifiers="//">
            <Output TaskParameter="CodeLinecount" PropertyName="csharplines"/>
            <Output TaskParameter="IncludedFiles" ItemName="MyIncludedFiles"/>
            <Output TaskParameter="ExcludedFiles" ItemName="MyExcludedFiles"/>
        </MSBuild.ExtensionPack.FileSystem.File>
        <Message Text="C# CodeLinecount: $(csharplines)"/>
        <Message Text="MyIncludedFiles: %(MyIncludedFiles.Identity)"/>
        <Message Text="MyExcludedFiles: %(MyExcludedFiles.Identity)"/>
        <!-- Count all lines in a file -->
        <MSBuild.ExtensionPack.FileSystem.File TaskAction="CountLines" Files="@(AllFilesToCount)">
            <Output TaskParameter="TotalLinecount" PropertyName="AllLines"/>
        </MSBuild.ExtensionPack.FileSystem.File>
        <Message Text="All Files TotalLinecount: $(AllLines)"/>
        <!-- Set some attributes -->
        <MSBuild.ExtensionPack.FileSystem.File TaskAction="SetAttributes" Files="@(AtFiles)"/>
        <MSBuild.ExtensionPack.FileSystem.File TaskAction="SetAttributes" Files="@(AtFiles2)"/>
        <!-- Move a file -->
        <MSBuild.ExtensionPack.FileSystem.File TaskAction="Move" Path="c:\demo\file.txt" TargetPath="c:\dddd\d\oo\d\mee.txt"/>
    </Target>
</Project>
See Also

Reference