Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell:仅复制具有一个特定子项的文件夹_Powershell_Copy Item - Fatal编程技术网

Powershell:仅复制具有一个特定子项的文件夹

Powershell:仅复制具有一个特定子项的文件夹,powershell,copy-item,Powershell,Copy Item,我正在尝试使用仅包含特定文件格式子项的powershell将文件夹复制到另一个位置 我有一个文件夹ec-alert-service,其中有子文件夹和文件,它有一个.txt文件。我只想用txt文件将此文件夹复制到另一个位置 所以我写了这个命令,但它不是复制文本文件,而是空文件夹。因此基本上,-过滤器不起作用 Copy-Item -Path D:\Pawan\Gitlab\ec-alert-service -Filter *.txt -Destination D:\Pawan\GitlabBuild

我正在尝试使用仅包含特定文件格式子项的powershell将文件夹复制到另一个位置

我有一个文件夹
ec-alert-service
,其中有子文件夹和文件,它有一个
.txt
文件。我只想用
txt
文件将此文件夹复制到另一个位置

所以我写了这个命令,但它不是复制文本文件,而是空文件夹。因此基本上,
-过滤器
不起作用

Copy-Item -Path D:\Pawan\Gitlab\ec-alert-service -Filter *.txt -Destination D:\Pawan\GitlabBuildFiles

我做错了什么?

你试过添加-Recurse吗?试一下:
复制项目-Path D:\Pawan\Gitlab\ec alert service\*-Filter*.txt-目的地D:\Pawan\Gitlab buildfiles
@GertJanKraaijeveld-它只是复制了文件而不是文件夹。@PalleDue-是的。递归复制所有内容
$splatPrm = @{
    sourceDirectory = "d:\tmp\001"
    destinationDirectory = "D:\tmp\002"
    wildCardArray = @("*.txt") # or @("*.txt", "*.csv") or other
}

& {
    param(
        [System.IO.DirectoryInfo]$sourceDirectory,
        [System.IO.DirectoryInfo]$destinationDirectory,
        [string[]]$wildCardArray
    )
    $pathToCopy = [System.IO.Path]::Join($destinationDirectory.FullName, $sourceDirectory.Name)
    if (-not [System.IO.Directory]::Exists($pathToCopy)){
        [System.IO.Directory]::CreateDirectory($pathToCopy)
    }
    foreach ($wildCard in $wildCardArray){
        foreach ($file in $sourceDirectory.GetFiles($wildCard)){
            $file.CopyTo([System.IO.Path]::Join($pathToCopy, $file.Name))
        }
    }
} @splatPrm