首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带有黑名单(排除)和白名单(包括)的Powershell复制文件

带有黑名单(排除)和白名单(包括)的Powershell复制文件
EN

Stack Overflow用户
提问于 2017-03-15 18:33:34
回答 2查看 1.5K关注 0票数 4

我正在将一些msbuild脚本翻译到powershell。

在msbuild中,我可以生成一个黑名单和/或我想要(递归)复制到目标文件夹的文件的白名单。

如下所示:

代码语言:javascript
复制
<?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“的文件(它是一个没有扩展名的文件,而不是一个目录)

代码语言:javascript
复制
$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

附录:

我试过这个:

代码语言:javascript
复制
$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

(没有结果,甚至没有扩展名的文件)

我试过这个:

代码语言:javascript
复制
$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

(没有结果,甚至没有扩展名的文件)

EN

回答 2

Stack Overflow用户

发布于 2017-03-15 19:32:40

使用我在这里找到的一个建议(“触摸”方法)

复制项应该创建目标目录结构吗?

我有下面的工作要做。

我会把这个问题作为“未回答”的问题留几天,也许有人有更好的东西。在我看来,新的项目似乎很奇怪,因为复制项目不应该在一个包含和排除列表IMHO中偶然出现。

(按照原来的问题使用变量设置器)

代码语言:javascript
复制
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)
    }
票数 3
EN

Stack Overflow用户

发布于 2017-03-15 19:39:52

为了保持一个概览,我缩短了您的vars (并更改了dst以适应我的env)

代码语言:javascript
复制
$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
}

编辑本质上与您的示例相同,可能会更密集一些

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42818014

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档