【问题标题】:Move files into alphabetically named folders将文件移动到按字母顺序命名的文件夹中
【发布时间】:2013-11-25 04:13:40
【问题描述】:

只是想知道是否可以让脚本根据字母表将电影文件移动到特定文件夹?

例如 Scream 4 将被移动到 e:\movies\s\ Avatar 将被移动到 e:\movies\a\

我启动了一个如下所示的脚本: 但是结果并不好!脚本尝试用文件名创建目录...

$a = new-object -comobject wscript.shell
$b = Get-Location

    foreach($file in (dir $b -file -recurse)) { 
        New-Item -Path $b -Name (Split-Path $file.fullname -Leaf).Replace($file.extension,"") -ItemType Directory -Confirm
        Move-Item -Path $file.fullname -Destination "$b\$((Split-Path $file.fullname -Leaf).Replace($file.Extension,''))" -Confirm
        }

一个想法? 非常感谢! 克雷格

【问题讨论】:

    标签: powershell


    【解决方案1】:

    在您运行命令之前,目标文件夹必须存在:

    dir $b -file -recurse | Move-Item -Destination {"e:\movies\$($_.Name[0])"} 
    

    这将在运行时创建文件夹:

    dir $b -File -Recurse | foreach{
        $folder = Join-Path e:\movies $_.Name[0]
        md $folder -force | Out-Null
        $_ | Move-Item -Destination $folder
    }  
    

    【讨论】:

    • 太棒了!完美,非常感谢 :) 如果我可以“夸大其词”,您知道如何将所有以数字开头的文件存储在同一目录中(例如 #)吗?
    • @Kreg 您可能希望将此标记为答案,然后将您的新问题作为新的 SO 帖子打开。
    猜你喜欢
    • 1970-01-01
    • 2014-08-23
    • 2016-06-28
    • 1970-01-01
    • 2017-11-13
    • 1970-01-01
    • 2010-11-08
    • 1970-01-01
    • 2016-06-22
    相关资源
    最近更新 更多