我正在将一些msbuild脚本翻译到powershell。
在msbuild中,我可以生成一个黑名单和/或我想要(递归)复制到目标文件夹的文件的白名单。
如下所示:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="AllTargetsWrapped">
<PropertyGroup>
<!-- Always declare some kind of "base directory" and then work off of that in the majority of cases -->
<WorkingCheckout>.</WorkingCheckout>
<WindowsSystem32Directory>c:\windows\System32</WindowsSystem32Directory>
<ArtifactDestinationFolder>$(WorkingCheckout)\ZZZArtifacts</ArtifactDestinationFolder>
</PropertyGroup>
<Target Name="AllTargetsWrapped">
<CallTarget Targets="CleanArtifactFolder" />
<CallTarget Targets="CopyFilesToArtifactFolder" />
</Target>
<Target Name="CleanArtifactFolder">
<RemoveDir Directories="$(ArtifactDestinationFolder)" Condition="Exists($(ArtifactDestinationFolder))"/>
<MakeDir Directories="$(ArtifactDestinationFolder)" Condition="!Exists($(ArtifactDestinationFolder))"/>
<Message Text="Cleaning done" />
</Target>
<Target Name="CopyFilesToArtifactFolder">
<ItemGroup>
<MyExcludeFiles Include="$(WindowsSystem32Directory)\**\EventViewer_EventDetails.xsl" />
</ItemGroup>
<ItemGroup>
<MyIncludeFiles Include="$(WindowsSystem32Directory)\**\*.xsl" Exclude="@(MyExcludeFiles)"/>
<MyIncludeFiles Include="$(WindowsSystem32Directory)\**\*.xslt" Exclude="@(MyExcludeFiles)"/>
<MyIncludeFiles Include="$(WindowsSystem32Directory)\**\*.png" Exclude="@(MyExcludeFiles)"/>
<MyIncludeFiles Include="$(WindowsSystem32Directory)\**\*.jpg" Exclude="@(MyExcludeFiles)"/>
</ItemGroup>
<Copy
SourceFiles="@(MyIncludeFiles)"
DestinationFiles="@(MyIncludeFiles->'$(ArtifactDestinationFolder)\%(RecursiveDir)%(Filename)%(Extension)')"
/>
</Target>
</Project>我可以在powershell中做同样的事情吗?
我尝试了下面的方法,但是它创建了一个名为"C:\work9\MsBuildExamples\FileCopyRecursive\PowershellResults“的文件(它是一个没有扩展名的文件,而不是一个目录)
$sourceDirectory = 'c:\windows\System32\*'
$destinationDirectory = 'C:\work9\MsBuildExamples\FileCopyRecursive\PowershellResults'
$excludeFiles = @('EventViewer_EventDetails.xsl')
$includeFiles = @('*.xsl','*.xslt','*.png','*.jpg')
Copy-Item $sourceDirectory $destinationDirectory -Recurse -Include $includeFiles -Exclude $excludeFiles
# -Container:$false附录:
我试过这个:
$sourceDirectory = 'c:\windows\System32'
$destinationDirectory = 'C:\work9\MsBuildExamples\FileCopyRecursive\PowershellResults'
$excludeFiles = @('EventViewer_EventDetails.xsl')
$includeFiles = @('*.xsl','*.xslt','*.png','*.jpg')
Copy-Item $sourceDirectory $destinationDirectory -Recurse -Include $includeFiles -Exclude $excludeFiles(没有结果,甚至没有扩展名的文件)
我试过这个:
$sourceDirectory = 'c:\windows\System32'
$destinationDirectory = 'C:\work9\MsBuildExamples\FileCopyRecursive\PowershellResults'
$excludeFiles = @('EventViewer_EventDetails.xsl')
$includeFiles = @('*.xsl','*.xslt','*.png','*.jpg')
Copy-Item $sourceDirectory $destinationDirectory -Recurse -Include $includeFiles -Exclude $excludeFiles -Container:$false(没有结果,甚至没有扩展名的文件)
发布于 2017-03-15 19:32:40
使用我在这里找到的一个建议(“触摸”方法)
我有下面的工作要做。
我会把这个问题作为“未回答”的问题留几天,也许有人有更好的东西。在我看来,新的项目似乎很奇怪,因为复制项目不应该在一个包含和排除列表IMHO中偶然出现。
(按照原来的问题使用变量设置器)
IF (Test-Path -Path $destinationDirectory -PathType Container)
{
Write-Host "$destinationDirectory already exists" -ForegroundColor Red
}
ELSE
{
New-Item -Path $destinationDirectory -ItemType directory
}
# the two scripts below are same except for "New-Item" vs "Copy-Item" , thus the "New-Item" trick-it-to-work voodoo
Get-ChildItem $sourceDirectory -Include $includeFiles -Exclude $excludeFiles -Recurse -Force |
New-Item -ItemType File -Path {
Join-Path $destinationDirectory $_.FullName.Substring($sourceDirectory.Length)
} -Force
Get-ChildItem $sourceDirectory -Include $includeFiles -Exclude $excludeFiles -Recurse -Force |
Copy-Item -Destination {
Join-Path $destinationDirectory $_.FullName.Substring($sourceDirectory.Length)
}发布于 2017-03-15 19:39:52
为了保持一个概览,我缩短了您的vars (并更改了dst以适应我的env)
$src = 'c:\windows\System32'
$dst = 'Q:\Test\2017-03\15\PowershellResults'
$exc = @('EventViewer_EventDetails.xsl')
$inc = @('*.xsl','*.xslt','*.png','*.jpg')
Get-ChildItem -Path $src -Rec -Inc $inc -Exc $exc -ea silentlycontinue|ForEach{
$DestPath = $_.DirectoryName -replace [regex]::escape($src),$dst
If (!(Test-Path $DestPath)){MkDir $DestPath}
$_|Copy-Item -Destination $DestPath -Force
}编辑本质上与您的示例相同,可能会更密集一些
https://stackoverflow.com/questions/42818014
复制相似问题