【问题标题】:Move files with folders使用文件夹移动文件
【发布时间】:2022-01-15 22:13:05
【问题描述】:

我的 Google 云端硬盘因未知原因创建了某些文件的多个副本,格式如下:文件名 (2021_02_08 11_22_39 UTC).ext

我想将这些文件移到另一个文件夹中,以防万一。 我发现这个线程here 看起来很相似。 我不是正则表达式的专家。你能帮我解决这个问题吗?

正则表达式:[a-zA-Z]+)。用于搜索:UTC)。

我想它应该是这样的:

$source = "C:\Dropbox"
$destination = "C:\DropboxDupes"
gci .\Dropbox -Recurse -File | ?{ $_.basename -match "[a-zA-Z]+\)\."} | % {

    $destination_filename = $_.fullname.Replace($source, $destination)
    $destination_dir = split-path $destination_filename -Parent
    if(-not (Test-Path $destination_dir -PathType Container)) {
        mkdir $destination_dir | out-null
    }
    move-item $_.fullname $destination_filename
}

【问题讨论】:

  • 您还可以将-Filter '????_??_?? ??_??_?? UTC.ext' 添加到您的搜索中以使其更快。
  • 这个八 (8) 年前的答案看起来很熟悉。 stackoverflow.com/a/18165832/447901

标签: regex powershell


【解决方案1】:

尝试'\([\d_\s]+UTC\)' 匹配数字、下划线和空格,后跟“UTC”并全部括在括号中。

脚本编写的最佳实践是不使用别名、使用参数名称并坚持大小写。

$source = "C:\Dropbox"
$destination = "C:\DropboxDupes"
Get-ChildItem -Path $source -Recurse -File |
    Where-Object { $_.BaseName -match '\([\d_\s]+UTC\)'} |
    ForEach-Object {
        $destination_filename = $_.FullName.Replace($source, $destination)
        $destination_dir = Split-Path -Path $destination_filename -Parent
        if (-not (Test-Path -Path $destination_dir -PathType Container)) {
            New-Item -Type Directory -Path $destination_dir | Out-Null
        }
        Move-Item -Path $_.FullName -Destination $destination_filename
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-25
    相关资源
    最近更新 更多