【问题标题】:PowerShell script isn't copying like I wantPowerShell 脚本没有像我想要的那样复制
【发布时间】:2019-05-17 07:14:42
【问题描述】:

一开始我应该注意到我是一个该死的初学者,因为我不能参加我年级的课程。

我想创建一个 PowerShell 脚本,它将从

复制所有内容

C:\Users\Robert\Desktop\test(很多文件夹)

C:\Users\Robert\Desktop\neu(很多文件夹名称与上面完全相同)\price

作为一个绝对的初学者,我认为可以将变量文件夹名称替换为$_Name,因为两者的名称相同,但我显然错了,不知道为什么。 这是我的尝试

Copy-Item "C:\Users\Robert\Desktop\test\$_name\*" -Destination "C:\Users\Robert\Desktop\neu\$_Name\price" -Recurse

它正在复制一些东西,但在“neu”的新文件夹中的一个包中。

我无法避免创建这个脚本,因为我至少需要两到三天才能手动完成。 我也很抱歉我的英语不好

谢谢

【问题讨论】:

标签: powershell scripting directory subdirectory copy-item


【解决方案1】:

$_ 代表当前的管道 项。我没有看到那里的管道... [grin]

以下工作通过抓取源目录及其子目录中的每个文件,并将该结构复制到目标目录。它使用Splatting 整齐地构造参数。

$SourceDir = "$env:TEMP\Apps - Copy"
$DestDir = "$env:TEMP\Apps - Copy - Two"

$CI_Params = @{
    LiteralPath = $SourceDir
    Destination = $DestDir
    Force = $True
    Recurse = $True
    }
Copy-Item @CI_Params

【讨论】:

    【解决方案2】:

    如果我的理解是正确的:

    $src = 'C:\Users\Robert\Desktop\test'
    $dst = 'C:\Users\Robert\Desktop\neu\{0}\price'
    
    Get-ChildItem $src -Directory | ForEach-Object {
        Copy-Item -Path "$($_.FullName)\*" -Destination ($dst -f $_.BaseName) -Recurse -Force -WhatIf
    }
    

    删除 -WhatIf 以实际执行它。

    【讨论】:

    • 我已经尝试过了,但它不断粉碎我已经位于那里的其他文件。在某些文件夹之后它也会停止。
    猜你喜欢
    • 2019-07-08
    • 1970-01-01
    • 2020-12-24
    • 2016-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多