将文件移动到以其文件名的一部分命名的文件夹中

将文件移动到以其文件名的一部分命名的文件夹中

我有来自 GoPro Fusion 相机的文件,其中包含图像和影片。文件名如下

GP                                          (代表“GoPro”)
(另外两封没有特别意义的信)
(一系列数字;可能是四位或六位数字)
                                           (一段时间)
(延伸)

有些扩展名很常见,例如JPGMP4WAV;其他则不常见。一些示例文件名是GPFR0000.jpgGPBK0000.jpgGPFR0000.gprGPFR1153.MP4和。但扩展名与这个问题无关GPFR1153.THMGPBK142857.WAV

对于每张图片和每部电影,都有一组文件,其名称在扩展名前有相同的一系列数字。例如,GPFR1153.LRVGPBK1153.MP4 属于同一组。

我希望将每组中所有文件分组到一个目录中,该目录的名称后跟GP一系列数字。例如,如果我有

GPFR0000.jpg
GPBK0000.jpg
GPFR0000.gpr
GPFR0000.gpr
GPFR1153.LRV
GPFR1153.MP4
GPFR1153.THM
GPBK1153.WAV
GPBK1153.MP4
GPQZ142857.FOO

全部放在一个目录中,结果应该是

GP0000\GPFR0000.jpg
GP0000\...
GP1153\GPFR1153.LRV
GP1153\GPFR1153.MP4
GP1153\...
GP142857\GPQZ142857.FOO

可以使用脚本(适用于 Windows 10)来实现吗?我找到了这个(PowerShell)脚本穆西奥递归移动数千个文件到子文件夹窗口中,但它解决了一个稍微不同的问题,我希望得到帮助以使它适应我的要求(我是一名艺术家,而不是程序员)。

# if run from "P:\Gopro\2018", we can get the image list
$images = dir *.jpg

# process images one by one
foreach ($image in $images)
{
    # suppose $image now holds the file object for "c:\images\GPBK1153.*"

    # get its file name without the extension, keeping just "GPBK1153"
    $filenamewithoutextension = $image.basename

    # group by 1 from the end, resulting in "1153"
    $destinationfolderpath = 
        $filenamewithoutextension -replace '(....)$','\$1'

    # silently make the directory structure for "1153 GPBK1153"
    md $destinationfolderpath >$null

    # move the image from "c:\images\1234567890.jpg" to the new folder "c:\images\1\234\567\890\"
    move-item $image -Destination $destinationfolderpath

    # the image is now available at "P:\Gopro\2018\1153\GPBK1153.*"
}

答案1

根据我对您想要的内容的理解(可能有缺陷),您可以使用以下 PowerShell 脚本来实现。请注意,这是从穆西奥,发布于 递归移动数千个文件到子文件夹窗口中

# If run from "P:\Gopro\2018", we can get the file list.
$images = dir GP*

# Process files one by one.
foreach ($image in $images)
{
    # Suppose $image now holds the file object for "P:\Gopro\2018\GPBK1153.FOO"

    # Get its file name without the extension, keeping just "GPBK1153".
    $filenamewithoutextension = $image.basename

    # Grab the first two characters (which we expect to be "GP"),
    # skip the next two characters (which we expect to be letters; e.g., "BK"),
    # then grab all the characters after that (which we expect to be digits; e.g., "1153")
    # and put them together, resulting in "GP1153".
    $destinationfolderpath = 
        $filenamewithoutextension -replace '(..)..(.*)','$1$2'

    # Silently make the directory structure for "GP1153".
    md $destinationfolderpath > $null 2>&1

    # Move the file from "P:\Gopro\2018\GPBK1153.FOO" to the new folder "P:\Gopro\2018\GP1153"
    move-item $image -Destination $destinationfolderpath

    # The file is now available at "P:\Gopro\2018\GP1153\GPBK1153.FOO".
}

相关内容