2

I have files from a GoPro Fusion camera that represent images and movies.  The filenames look like

GP                                          (for “GoPro”)
(two more letters of no particular significance)
(a series of digits; maybe four or six digits)
.                                           (a period)
(an extension)

Some of the extensions are common ones, like JPG, MP4, and WAV; others are uncommon.  Some example filenames are GPFR0000.jpg, GPBK0000.jpg, GPFR0000.gpr, GPFR1153.MP4, GPFR1153.THM and GPBK142857.WAV.  But the extensions aren’t relevant to this question.

For each image and movie there is a set of files whose names have the same series of digits right before the extension.  So, for example, GPFR1153.LRV and GPBK1153.MP4 belong to the same set.

I want all the files from each set to be grouped in a directory whose name is GP followed by the series of digits.  For example, if I have

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

all in one directory, the outcome should be

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

Would this be possible with a script (for Windows 10)?  I found this (PowerShell) script by mousio at Recursively move thousands of files into subfolders windows, but it addresses a slightly different problem, and I’d like help adapting it to my requirements (I’m an artist, not a programmer).

# 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 Answers1

0

Based on my (possibly flawed) understanding of what you want, you can do it with the following PowerShell script.  Note that this is derived from a work by mousio, posted at Recursively move thousands of files into subfolders windows.

# 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".
}