powershell - Move-Item的通配符问题

标签 powershell wildcard

我正在尝试根据文件夹名称的第一部分将所有文件夹及其内容移动到一个文件夹中。例如,以2018开头的所有文件夹到名为2018的文件夹。所有文件夹的命名结构均为年月日(xxxx-xx-xx),因此为2018-01-01、2018-01-02等。因此,我尝试将Move-Item与通配符*和?一起使用。年复一年。

Move-Item . -Include 2018* .\2018


Move-Item . -Include 2018?????? .\2018

但是我得到这个错误:

Move-Item : Cannot move item because the item at 'F:\My Share\One\More\Folder' does not exist.
At line:1 char:1
+ Move-Item . -Include 2018* .\2018
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Move-Item], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.MoveItemCommand


我与任何一个都遇到相同的错误。我希望我提供了足够的信息,以使您能启发这个新角。 :-)

提前致谢!

最佳答案

S.TECHS,

如果该目录不存在,则Move-Item不会成功。好吧,它将不会在win7ps5.1上出现-可能会在win10上出现,因为那里有一些改进。

下面的代码在win7ps5.1上工作,方法是检查目标目录并使其不存在。 [咧嘴]

$SourceDir = $env:TEMP
$Filter = '20??-*'

$FileList = Get-ChildItem -LiteralPath $SourceDir -Filter $Filter

foreach ($FL_Item in $FileList)
    {
    $Year = $FL_Item.BaseName.Split('-')[0]
    $DestDir = Join-Path -Path $SourceDir -ChildPath $Year
    $FullDestFileName = Join-Path -Path $DestDir -ChildPath $FL_Item.Name

    if (-not (Test-Path $DestDir))
        {
        # suppress unwanted output of New-Item
        $Null = New-Item -Path $DestDir -ItemType Directory
        }

    Move-Item -LiteralPath $FL_Item.FullName -Destination $FullDestFileName
    }

希望能有所帮助,

关于powershell - Move-Item的通配符问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52658450/

相关文章:

winforms - 类中的 Powershell winform 事件处理程序导致范围问题

c - 将 fnmatch 与 char 指针一起使用

java - 字符串 "Slot-Extraction"

bash - 如何在bash中捕获和修改正则表达式识别的数字

Sql通配符: performance overhead?

powershell - Get-Module -ListAvailable 不显示我的模块

powershell - Powershell @ {}管道错误

bash - 使用带前缀的 bash 通配符

powershell - 将变量超出脚本范围,以便在控制台 session 中访问

python - 在windows powershell中模拟python3 shell命令行为