复制项目并排除文件夹

MJJ*_*JJM 5 powershell

我需要将所有c:\inetpub目录复制到新位置,但要排除以下文件夹及其子文件夹:

c:\inetpub\custerr
c:\inetpub\history
c:\inetpub\logs
c:\inetpub\temp
c:\inetpub\wwwroot
Run Code Online (Sandbox Code Playgroud)

到目前为止,我正在这样做:

# Directory name is created with a format string
$dirName = "\\servername\folder1 _ {0}\inetpub" -f (get-date).ToString("yyyy-MM-dd-hh-mm-ss")
$dirName # Check the output

# Create dir if needed
if(-not (test-path $dirName)) {
    md $dirName | out-null
} else {
    write-host "$dirName already exists!"
}

#Copy Backup File to Dir
Copy-Item "\\servername\c$\inetpub\*" $dirName -recurse
Run Code Online (Sandbox Code Playgroud)

bri*_*ler 13

哦,答案很简单,但看来我们都是 PowerShell 菜鸟。

New-Item -ItemType Directory -Force -Path $outDir # directory must exist
Copy-Item $inDir\* $outDir -Exclude @("node_modules",".yarn") -Recurse
Run Code Online (Sandbox Code Playgroud)

正是它\*使其发挥作用。

PowerShell 很棒,但是...


Mat*_*att 7

这是您可以做的一个简单示例。构建要排除的父文件夹的数组。由于您是通过UNC路径访问它们的,因此我们不能真正使用c:\​​路径(我们可以解决这个问题,但是我要显示的内容应该足够好。)。

然后使用Get-ChildItem来获取inetpub目录中的所有文件夹。使用过滤掉排除项,-notin然后将其余部分传递给Copy-Item

$excludes = "custerr","history","logs","temp","wwwroot"
Get-ChildItem "c:\temp\test" -Directory | 
    Where-Object{$_.Name -notin $excludes} | 
    Copy-Item -Destination $dirName -Recurse -Force
Run Code Online (Sandbox Code Playgroud)

您至少需要PowerShell 3.0才能运行。

  • 警告:如果 `$dirName` 尚不存在,它将被创建,**但**不会复制源中的第一个子目录。相反,它的内容将直接复制到`$dirName`,消除了一层深度。由于这会创建`$dirName`,第二个子目录将按预期复制。 (2认同)

小智 6

Copy-Item -Path (Get-Item -Path "$path\*" -Exclude ('Folder1', 'File.cmd', 'File.exe', 'Folder2')).FullName -Destination $destination -Recurse -Force
Run Code Online (Sandbox Code Playgroud)

代替:

$path 通过您的源文件夹

('Folder1', 'File.cmd', 'File.exe', 'Folder2') 通过您的特定文件/文件夹排除

$destination 通过您的目标文件夹

  • 仅当Folder1位于$path中时才有效,不适用于$path\sub1\sub2\Folder1 (6认同)

Han*_*abi 6

我写这个是为了日常使用,并将其打包在脚本模块中,它维护了所有目录结构并支持通配符:

function Copy-Folder {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [String]$FromPath,

        [Parameter(Mandatory)]
        [String]$ToPath,

        [string[]] $Exclude
    )

    if (Test-Path $FromPath -PathType Container) {
        New-Item $ToPath -ItemType Directory -ErrorAction SilentlyContinue | Out-Null
        Get-ChildItem $FromPath -Force | ForEach-Object {
            # avoid the nested pipeline variable
            $item = $_
            $target_path = Join-Path $ToPath $item.Name
            if (($Exclude | ForEach-Object { $item.Name -like $_ }) -notcontains $true) {
                if (Test-Path $target_path) { Remove-Item $target_path -Recurse -Force }
                Copy-Item $item.FullName $target_path
                Copy-Folder -FromPath $item.FullName $target_path $Exclude
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

只需致电Copy-Folder -FromPath inetpub -ToPath new-inetpub -Exclude custerr,history,logs,temp,wwwroot

和可以省略-FromPath-ToPath

Copy-Folder inetpub new-inetpub -Exclude custerr,history,logs,temp,wwwroot


小智 0

您可以按照以下方式做一些事情:

  ?{$_.fullname -notmatch '\\old\\'}
Run Code Online (Sandbox Code Playgroud)

当您掌握所有文件夹并对其进行过滤后。

此示例将排除名称中包含“old”的任何内容。您可以对要排除的目录执行此操作。

一个完整的例子:

C:\Example*" -include "*.txt -Recurse |
  ?{$_.fullname -notmatch '\\old\\'}|
    % {Copy-Item $_.fullname "C:\Destination\"}
Run Code Online (Sandbox Code Playgroud)

对于多个排除,您可以使用-And

C:\Example*" -include "*.txt -Recurse |
  ?{$_.fullname -notmatch '\\old\\' -And $_.fullname -notmatch '\\old2\\'}|
    % {Copy-Item $_.fullname "C:\Destination\"}
Run Code Online (Sandbox Code Playgroud)

  • 我开始想知道是否值得只运行副本,然后在删除 \\server\foldername\logs 后运行单独的删除命令,因为做一些我认为相对容易的事情似乎是一项艰巨的任务 (2认同)