首页 > 解决方案 > 复制 findstr 找到的文件?

问题描述

我试图将我用 findstr 找到的文件复制到另一个文件夹。这就是我所做的,列出了我要复制的所有文件

dir * /a /b  |findstr ^FALSE[0-9][0-9]\.txt$

如何将此列表重定向到复制命令?我试图不使用批处理文件。

标签: regexwindowscmddirectoryfindstr

解决方案


对我来说,这在 PowerShell 中更容易。如果您在受支持的 Windows 系统上,则可以使用 PowerShell。当您确信正确的文件将被复制到正确的位置时,请删除-WhatIffrom Copy-Item.

Get-ChildItem -File -Path 'C:\src\t' -Filter 'FALSE*.txt' |
    Where-Object { $_.Name -match '^FALSE[0-9]{2}\.txt$' } |
    ForEach-Object { Copy-Item -Path $_.FullName -Destination 'C:\the\other\place' -WhatIf }

如果这仅在命令外壳中,您可以使用:

gci -File 'C:\src\t\FALSE*.txt'|?{$_.Name -match '^FALSE[0-9]{2}\.txt$'}|%{ Copy-Item $_.FullName 'C:\the\other\place' -WhatIf }

推荐阅读