使用File :: Exists的条件不起作用(Condition using File::Exists not working)

编程入门 行业动态 更新时间:2024-10-12 05:44:20
使用File :: Exists的条件不起作用(Condition using File::Exists not working)

我目前正在创建我的第一个MSBuild脚本。

我有一个标签“文件夹”,它在给定的根路径中查找所有目录:

<?xml version="1.0" encoding="utf-8"?> <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0"> <Target Name="Build"> <PropertyGroup> <RootFolder>tmp</RootFolder> </PropertyGroup> <ItemGroup> <Folders Include="$([System.IO.Directory]::GetDirectories(&quot;$(RootFolder)&quot;))"/> </ItemGroup> <Message Text="@(Folders -> '%(FullPath)\Bin\Debug\%(Filename)%(Extension).dll', ';')"/> </Target> </Project>

这很完美。 我的问题是我只需要指定文件所在的目录。 我试过这样的条件

Condition="$([System.IO.File]::Exists(&quot;%(FullPath)\\Bin\\Debug\\%(Filename)%(Extension).dll&quot;))"

对于文件夹标签。

此脚本运行时没有任何错误,但我的列表为空。 为什么?

有没有其他解决方案来检查文件?

我使用此解决方案是因为它使用C#而我是C#开发人员。

<?xml version="1.0" encoding="utf-8"?> <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0"> <Target Name="Build"> <PropertyGroup> <RootFolders>tmp</RootFolders> </PropertyGroup> <GetFiles rootFolders="$(RootFolders)"> <Output PropertyName="Files" TaskParameter="Files" /> </GetFiles> <Message Text="$(Files)" /> </Target> <UsingTask TaskName="GetFiles" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll"> <ParameterGroup> <rootFolders ParameterType="System.String" Required="true" /> <files ParameterType="System.String" Output="true" /> </ParameterGroup> <Task> <Using Namespace="System" /> <Using Namespace="System.IO" /> <Using Namespace="System.Linq" /> <Code Type="Fragment" Language="cs"> <![CDATA[ Func<string, string> BuildFilePath = path => path + @"\Bin\Debug\" + Path.GetFileName(path) + ".dll"; var dirs = Directory.GetDirectories(rootFolders).Where(x => File.Exists(BuildFilePath(x))); files = string.Join("\n", dirs.Select(BuildFilePath)); ]]> </Code> </Task> </UsingTask> </Project>

I currently create my first MSBuild-script.

I've a tag "Folders" that findes all Directories in a given root path:

<?xml version="1.0" encoding="utf-8"?> <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0"> <Target Name="Build"> <PropertyGroup> <RootFolder>tmp</RootFolder> </PropertyGroup> <ItemGroup> <Folders Include="$([System.IO.Directory]::GetDirectories(&quot;$(RootFolder)&quot;))"/> </ItemGroup> <Message Text="@(Folders -> '%(FullPath)\Bin\Debug\%(Filename)%(Extension).dll', ';')"/> </Target> </Project>

That works perfect. My problem is that I only need directories where the specified file exists. I tried a condition like that

Condition="$([System.IO.File]::Exists(&quot;%(FullPath)\\Bin\\Debug\\%(Filename)%(Extension).dll&quot;))"

for the folder tag.

This script runs without any error but my list is empty. Why?

Are there any other solutions to check for a file?

I used this solution because it uses C# and I'm a C#-developer.

<?xml version="1.0" encoding="utf-8"?> <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0"> <Target Name="Build"> <PropertyGroup> <RootFolders>tmp</RootFolders> </PropertyGroup> <GetFiles rootFolders="$(RootFolders)"> <Output PropertyName="Files" TaskParameter="Files" /> </GetFiles> <Message Text="$(Files)" /> </Target> <UsingTask TaskName="GetFiles" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll"> <ParameterGroup> <rootFolders ParameterType="System.String" Required="true" /> <files ParameterType="System.String" Output="true" /> </ParameterGroup> <Task> <Using Namespace="System" /> <Using Namespace="System.IO" /> <Using Namespace="System.Linq" /> <Code Type="Fragment" Language="cs"> <![CDATA[ Func<string, string> BuildFilePath = path => path + @"\Bin\Debug\" + Path.GetFileName(path) + ".dll"; var dirs = Directory.GetDirectories(rootFolders).Where(x => File.Exists(BuildFilePath(x))); files = string.Join("\n", dirs.Select(BuildFilePath)); ]]> </Code> </Task> </UsingTask> </Project>

最满意答案

AFAIK,就是执行Condition并检查整个项目声明(即<Folders ..>标签)。

我认为,您需要遍历集合(例如,使用目标/任务批处理)并检查文件是否存在于集合中的每个文件夹文件夹中。 然后,如果文件存在 - 将其包含在新的<FoldersFiletered>项集合中。

注意:我现在没有时间测试代码,但这是大致的想法:

<Target Name="FilterFolders" Inputs="@(Folders)" Outputs="%(FullPath)"> <ItemGroup> <FoldersFiltered Include="@(Folders->'%(FullPath)')" Condition="$([System.IO.File]::Exists(&quot;@(Folders->'%(FullPath)'\\Bin\\Debug\\YourFile.dll&quot;))" /> </ItemGroup> </Target>

AFAIK, the thing is Condition is executed and checked for the whole declaration of Items (i.e. <Folders ..> tag).

I think, you need to loop through the collection (e.g. using target/task batching) and check the file to exist in every single folder folder in the collection. Then if the file exists - include it in the new <FoldersFiletered> items collection.

NB: I don't have time to test the code now, but this is the idea roughly:

<Target Name="FilterFolders" Inputs="@(Folders)" Outputs="%(FullPath)"> <ItemGroup> <FoldersFiltered Include="@(Folders->'%(FullPath)')" Condition="$([System.IO.File]::Exists(&quot;@(Folders->'%(FullPath)'\\Bin\\Debug\\YourFile.dll&quot;))" /> </ItemGroup> </Target>

更多推荐

本文发布于:2023-07-14 19:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1106736.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   条件   Exists   File   Condition

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!