AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1643084
Accepted
LexPointOh
LexPointOh
Asked: 2021-04-20 14:35:03 +0800 CST2021-04-20 14:35:03 +0800 CST 2021-04-20 14:35:03 +0800 CST

如何不覆盖现有文件

  • 772

目前,我正在使用Copy-Item并想知道是否有一个简单的命令只能复制不存在的文件或按日期/时间为新文件的文件。在网上看,但我看到的一切似乎都在使用-WhatIf命令。此外,看到-Recurse被使用。我也不完全理解该命令的作用。有任何想法吗?

$From = "E:\Folder\*"
$To = "\\Server\Folder"
Copy-Item -Path $From -Destination $To -Verbose
powershell copy
  • 3 3 个回答
  • 3779 Views

3 个回答

  • Voted
  1. Best Answer
    postanote
    2021-04-20T17:16:03+08:002021-04-20T17:16:03+08:00

    同样,您所追求的内容很容易通过 来完成RoboCopy.exe,并且这个问题已在此处和许多其他问答网站多次提出。即使在苏这里。

    Robocopy 仅复制新文件夹和文件

    以及在 SO

    https://stackoverflow.com/questions/23303532/use-robocopy-to-copy-only-changed-files

    RC will only copy newer files. Files of the same age will be skipped.
    C:\SourceFolder D:\DestinationFolder ABC.dll /XO
    
    robocopy c:\users\valery\documents j:\robocopy /XO /E /MAXAGE:20131030 /XD
    # Result: A full folders tree is created. Only new files copied.
    

    因此,您的问题实际上与上述问题重复。

    否则,您最终必须知道并执行以下操作(如果您是新手,如您所说,在单个搜索或一组搜索中很难找到):

    Clear-Host
    $Source      = 'D:\Temp\Source'
    $Destination = 'D:\Temp\Destination'
    
    Get-ChildItem -Path $Source -Recurse | 
    ForEach-Object {
        If (Test-Path -Path "$Destination\$($PSItem.Name)")
        {
            Write-Warning -Message "`n$($PSItem.Name) already exists in $Destination. Checking timestamp`n"
    
            Try
            {
                "Copying file if $($PSItem.Name) is newer"
                Get-ChildItem -Path $Destination -Filter $PSItem.Name | 
                Where-Object LastWriteTime -lt $PSItem.LastWriteTime -ErrorAction Stop
                Copy-Item -Path $PSItem.FullName -Destination $Destination -Verbose -WhatIf
    
            }
            Catch {$PSItem.Exception.Message}
        }
        Else
        {
            Write-Host "`n$PSItem.Name does not Exist in $Destination`n" -ForegroundColor Cyan
            Copy-Item -Path $PSItem.FullName -Destination $Destination -Verbose -WhatIf
        }
    }
    # Results
    <#
    ...
    WARNING: 
    abc.txt already exists in D:\Temp\Destination. Checking timestamp
    ... 
    
    WARNING: 
    LexPointOh.txt already exists in D:\Temp\Destination. Checking timestamp
    Copying file if $($PSItem.Name) is newer
    
    
    -a----         10-Apr-21     00:00              0 LexPointOh.txt
    What if: Performing the operation 
    "Copy File" on target "Item: D:\Temp\Source\LexPointOh.txt 
    Destination: D:\Temp\Destination\LexPointOh.txt".
    
    mytest - Copy.txt.Name does not Exist in D:\Temp\Destination
    
    What if: Performing the operation "Copy File" on target 
    "Item: D:\Temp\Source\mytest - Copy.txt 
    Destination: D:\Temp\Destination\mytest - Copy.txt".
    ...
    #>
    

    只需删除-WhatIf它即可完成任务。

    所以,根据你的说法:

    我也不完全理解该命令的作用。

    既然如此;那么,我在上面展示的内容将更具挑战性。因此,为什么我在我的原始帖子中向您指出了帮助文件(培训站点、Youtube 等)。

    以上只是执行此操作的一种方法。PowerShell 提供了各种方法来做 X 或 Y 事情。例如,这是执行相同用例的另一种方法。

    Clear-Host
    
    $Source      = ($SourceFiles      = Get-ChildItem -Path 'D:\Temp\Source')[0].DirectoryName
    $Destination = ($DestinationFiles = Get-ChildItem -Path 'D:\Temp\Destination')[0].DirectoryName
    
    Compare-Object -ReferenceObject $SourceFiles -DifferenceObject $DestinationFiles -IncludeEqual | 
    ForEach-Object {
        If ($PSItem.SideIndicator -match '==|=>')
        {
            If (
                Get-ChildItem -Path $Destination -Filter $($PSItem.InputObject.Name) | 
                Where-Object LastWriteTime -LT  $PSItem.InputObject.LastWriteTime
            )       
            {
                Write-Warning -Message "`n$($PSItem.InputObject) already exists in $Destination. Checking timestamp`n"   
                Copy-Item -Path $PSItem.InputObject.FullName -Destination $Destination -ErrorAction SilentlyContinue -Verbose -WhatIf
            }
        }
        Else
        {
            Write-Host "`n$($PSItem.InputObject ) does not Exist in $Destination`n" -ForegroundColor Cyan
            Copy-Item -Path $PSItem.InputObject.FullName  -Destination $Destination -Verbose -WhatIf
        }
    }
    # Results
    <#
    WARNING: 
    abc.txt already exists in D:\Temp\Destination. Checking timestamp
    
    What if: Performing the operation "Copy File" on target "Item: D:\Temp\Source\abc.txt Destination: D:\Temp\Destination\abc.txt".
    WARNING: 
    LexPointOh.txt already exists in D:\Temp\Destination. Checking timestamp
    
    What if: Performing the operation "Copy File" on target "Item: D:\Temp\Source\LexPointOh.txt Destination: D:\Temp\Destination\LexPointOh.txt".
    
    mytest - Copy.txt does not Exist in D:\Temp\Destination
    
    What if: Performing the operation "Copy File" on target "Item: D:\Temp\Source\mytest - Copy.txt Destination: D:\Temp\Destination\mytest - Copy.txt".
    
    ...
    #>
    

    然而,任何时候你使用比较逻辑,在大多数情况下,你都不是在看简单的命令。

    因此,请使用正确的工具来完成这项工作。除非这是家庭作业,否则不要增加核心工作量/不要重新发明轮子。

    • 2
  2. Varun Master
    2021-04-20T15:36:29+08:002021-04-20T15:36:29+08:00

    您可以使用 cmdlet Get-ChildItem。你的脚本基本上应该有这个流程:

    1. 检查文件是否存在于目标文件夹中
    2. 如果存在,则跳过
    3. 如果不存在,则复制

    我在我的电脑上运行了一个非常粗略和简单的测试,这应该可以工作(记得根据你的用例修改它)

    $FROM = "T:\Files\Scripts1\BackItUp.ps1"
    $TO = "T:\Files2"
    
    if (Get-ChildItem -Path $TO "BackItUp.ps1") {
        Write-Host "file already exists..skipping"
    } else {
        Copy-Item $FROM -Destination $TO
    }
    
    • 0
  3. Romylussone
    2022-04-21T08:34:31+08:002022-04-21T08:34:31+08:00

    从堆栈溢出:

    Copy-Item (Join-Path $dir_from "*") $dir_to -Exclude (Get-ChildItem $dir_to)
    

    链接:为什么Copy-Item默认覆盖目标文件?

    • 0

相关问题

  • 如何将变量字符串放入powershell中的数组?

  • Powershell 和正则表达式:Notepad++“保存时备份”文件列表。编辑名称,按上次写入时间排序

  • 将前景颜色添加到 Powershell 配置文件?

  • 禁用后无法启用 Microsoft Print to PDF

  • 我可以让这个 PowerShell 脚本接受逗号吗?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    v15 为什么通过电缆(同轴电缆)的千兆位/秒 Internet 连接不能像光纤一样提供对称速度? 2020-01-25 08:53:31 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve