【问题标题】:Exclude files with no extension with robocopy?使用 robocopy 排除没有扩展名的文件?
【发布时间】:2014-03-16 08:21:43
【问题描述】:

我是 Powershell 的新手(截至今天早上)。我已经整理了这个脚本(目录反映了本地测试):

$Destinations = Get-Content "C:\PowerShellConfigFiles\destinationPaths.txt"
$Source = [IO.File]::ReadAllText("C:\PowerShellConfigFiles\sourcePath.txt")
$ExcludeFiles = [IO.File]::ReadAllText("C:\PowerShellConfigFiles\xf.txt")
$ExcludeDirectories = [IO.File]::ReadAllText("C:\PowerShellConfigFiles\xd.txt")

foreach($Destination in $Destinations)
{
    robocopy $Source $Destination /E /xf $ExcludeFiles /xd $ExcludeDirectories
}

在我的ExcludeFiles 中有几个扩展名,例如.xlsx。我还想排除根本没有任何扩展名的文件。我还没有找到使用/xf 选项的方法。这可以做到吗,还是我需要探索另一种方法来处理没有扩展名的排除文件?

【问题讨论】:

  • 对不起我之前的回答。我太仓促了,误读了你的问题。我刚刚发布了一个你想要的。

标签: powershell robocopy


【解决方案1】:

使用正则表达式过滤 $Source 中的文件列表以获取不带扩展名的文件列表,并将该列表添加到 $ExcludeFiles。将 /xf $ExcludeFiles 替换为:

/xf ($ExcludeFiles + (Get-ChildItem -File $Source -Name | ?{$_ -notmatch '\.'}))    

为了与低于 3.0 的 PowerShell 版本兼容,不支持 Get-ChildItem-File 开关,请改用:

/xf ($ExcludeFiles + (Get-ChildItem $Source | ?{! $_.PSIsContainer} | select -ExpandProperty Name | ?{$_ -notmatch '\.'})) 

根据定义,带扩展名的文件是名称中带有点的文件,因此$_ -notmatch '\.' 只选择不带扩展名的名称。

【讨论】:

    猜你喜欢
    • 2011-09-26
    • 2016-03-24
    • 1970-01-01
    • 2016-12-24
    • 1970-01-01
    • 2021-12-13
    • 2018-09-13
    • 2012-12-24
    • 1970-01-01
    相关资源
    最近更新 更多