[Python] Divide Switch albums into folders by game

Overview

--Get the name of the file and sort the files by directory

Do you enjoy the game?

Apparently, Sotono is scorned by an acquaintance who played Splatoon 2 for 1000 hours at Joycon. I haven't been splatooning lately, but when I play other games, ** screenshots and videos are accumulated and SD becomes fluffy **. ~~ Buy a new SD ~~

That shouldn't be the case, but I don't want to erase it ... That's why I transfer the SD card to my PC and manage the screenshots, but I don't like this because it's only classified by date **.

That's why if you look closely at the file names, different IDs are assigned to each game, so it's easier to manage personally if you use this to divide the files into folders for each game!

Let's do it now.

Analysis of image names

The file names recorded in the Switch album are generally in the following format.

[Date]-[Game ID]. [Extension]

--Date and time --As you can see, there are too many digits, so I think it records up to comma seconds (appropriate) --Game ID ――Different IDs are assigned to each main dish and game this time. --Be careful because different IDs are assigned between the trial version and the product version. ――It cannot be denied that there is a possibility that it will be different in another case, so let's check it properly.

Digression --Since it refers to the date and time set in Swtich, if you do a time investigation, it will be off by that amount, so be careful of time travel. --- In the mysterious folder called ʻExtra`, the videos made with Smash Bros. are saved, so be careful because the save location has been changed.

By the way, when I analyzed it, I found that ** different IDs were assigned to each game **, so I will use this to sort them immediately.

Write sorting in Python

~~ Because I can only use Python ... ~~

Associate the game ID with the game title

You can use CSV or anything, so link the game ID with the game name. If you don't do this, you'll end up managing with a randomly assigned ID, so it's hard to understand ...

That's why I was stupid and crazy, so I decided to manage it with a dictionary. ~~ It's annoying to use around CSV ... ~~

dict = {
    'hogehoge': 'Splatoon2',
    'fugafuga': 'Super Smash Bros. SP',
}

Sorting production

Assemble the following folder hierarchy appropriately

.
└── (Arbitrary folder name A) 
    └── sw_hogehoge(Game title)
        ├── img
            └── ~.jpg
        └── movie
            └── ~.mp4

With this, if you put the contents of the ʻAlbum folder in the Switch as it is in * 1`, the rest will be sorted.

The general flow is like this.

--Move only the images and video files in the Album to the After folder --Get the images in the After folder and sort them into the corresponding folders. --If the folder does not exist, create a new one

import glob
import re
import os
import shutil

PYDIR = os.path.dirname(os.path.abspath(__file__))
INBOX = os.path.dirname(PYDIR)
INBOX_AFTER = f'{INBOX}/After'

ARCHIVE = os.path.dirname(INBOX)
MOV = '/movie'
IMG = '/img'


def main():

    # -----Move from Album to After
    before = f'{INBOX}/Album/????/??/??/*'
    for i in glob.glob(before):
        shutil.move(i, INBOX_AFTER)

    # -----Scan through After to the game folder
    dir_game = ARCHIVE + '/sw_'
    dir_inbox_glob = f'{INBOX_AFTER}/*'

    for file in glob.glob(dir_inbox_glob):
        for id in fold_id.dict:
            if id in file:
                to = dir_game + fold_id.dict[id]

                #Create if there is no game folder
                if os.path.exists(to) is False:
                    FOLD_NAMES = ('', MOV, IMG)
                    [os.mkdir(to + fold_name) for fold_name in FOLD_NAMES]

                to += MOV if '.mp4' in file else IMG
                shutil.move(file, to)


if __name__ == '__main__':
    main()

When executed, a folder will be created in Arbitrary A with the following configuration.

.
└── sw_hogehoge(Game title)
    ├── img
        └── ~.jpg
    └── movie
        └── ~.mp4

Customize the folder hierarchy as you like.

Create your own strongest screenshot management system! !!

That's why I tried the basics of Python file management with Switch. Did this person really play with Python for two years? It's over, I'm going back to the uninhabited island ...

Recommended Posts

[Python] Divide Switch albums into folders by game
Divide into teams by combinatorial optimization
Divide Japanese (katakana) into syllable units [Python]
Switch Python versions
Divide into teams by combinatorial optimization (minimize average deviation)
Divide into teams by combinatorial optimization (simulated annealing method)