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.*"
}
6
  • (1) Your script shows filenames that look like Windows filenames.  Are you running Windows?  Please edit you question to say so, specifying version, in both the question body and the tags.  (2) You say your files follow a pattern, and you show a few examples, but you don’t explain it.  You show filenames that are four letters, four digits, a period, and a three letter extension.  Is the pattern that all files with the same four digits are related?  So BLUE1742.FOO and IRON1742.BAR go together?  … (Cont’d) Commented Nov 7, 2018 at 17:54
  • (Cont’d) …  (3) Please explain the result that you want.  (Your question title hints at your goals, but you give no details.)  (4) Where did you get that script?  If somebody else wrote it, you must identify the source.  (5) Where’s the rest of the script?  You show a { but no }, so you’re missing part.  (6) What is your question?  (7) Why aren’t you asking the author of the script for help?  … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … … …  Please do not respond in comments; edit your question to make it clearer and more complete. Commented Nov 7, 2018 at 17:54
  • (1) Yes Win 10 (2) Yes 4 digits in rare cases more (6) (3) a folderstructure containing each image set wit named folder by digits (4),(5) source was added Sorry (6) could you help me doing this script (7) i don't know why. I just thought this to be the right way since there is also many other people using this camere and having to solve having this issue. i'm sorry i just googled this and tried to find answers... please do not get offended. I can of course delete this if this is not the appropirate Commented Nov 7, 2018 at 19:37
  • I wasn’t offended; I’m sorry if I gave you that impression.  The problem is that we can’t read your mind, and we can’t answer your question if we don’t understand what you want.  Also, copying something written by somebody else without identifying the original author, even within the Stack Exchange family, is considered plagiarism and is strictly forbidden. … (Cont’d) Commented Nov 8, 2018 at 8:03
  • (Cont’d) …  While I’m not offended, I am frustrated, because I asked you what result you wanted, and you gave me two different answers.  Your comment says that you want files moved into a directory (folder) whose name is the digits from the filename, and the example comments that you put into the script say that you want files GPBK1153.* moved into directory 1153.  But, in your examples at the beginning of the question, you say that the directory name should be GP1152.  (And the fact that you switched from 1152 to 1153 confused me for a moment.) … (Cont’d) Commented Nov 8, 2018 at 8:03

1 Answer 1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .