Create folder named with current's date

Hi, there:

I’m new to Python and thought it was easy but no luck yet…

Plan to write the code that create folder in Library with current date as it’s name by context menu selection, like those examples (Create Share Library and Create Batch, etc.). Is it possible to name the folder as what token_name do (i.e.

)?

Hoping to save some time when checking the date and typing on every folder creation…Any hint or suggestion will be very welcome~

You could use something like this to get the date:

import datetime
folder_name = datetime.datetime.now().strftime('%y%m%d')
print folder_name

Thanks Mike for your great input :hugs: ! Glad to be able to continue the Python journey~

Due to my “embryo” status of Python, I can only follow those examples from Flame document and Mike’s hint, now it works when any Library selected. Although it’s not exactly as what I want but a starting point so I share here in case…

def get_media_panel_custom_ui_actions():

    def scope_libraries(selection):
        import flame
        for item in selection:
            if isinstance(item, flame.PyLibrary):
                return True
        return False

    def create_folder(selection):
        import flame
        import datetime
	folder_name = datetime.datetime.now().strftime('%Y%m%d')
        
	for item in selection:
		folder = item.create_folder(folder_name)

    return [
        {
            "name": "CUSTOM: Libraries",
            "actions": [
                {
                    "name": "Create Folder",
                    "execute": create_folder
                }
            ]
        }
    ]

Replace (’%y%m%d’) with (’%Y%m%d%H%M%S’) if you want the folder name longer :wink:

Thanks again for all the support !

1 Like

Hey @JeffChin,

if you don’t want to create the folders in the currently selected library, but always in the same (in my case I call it “EXPORT”), you can do something like this:

EXPORT_LIB = "EXPORT"

def current_workspace():
	"""Get the current workspace."""
	return flame.project.current_project.current_workspace


def create_export_lib():
	"""Create a new library for the export or return an existing one.

	Returns:
		PyLibrary: The library for the exports.
	"""
	workspace = current_workspace()
	for lib in workspace.libraries:
		if lib.name.get_value() == EXPORT_LIB:
			return lib

	return workspace.create_library(EXPORT_LIB)


# usage:
export_lib = create_export_lib()
# now you can create your folder here with your previous methods
export_lib.create_folder(folder_name)
2 Likes

Hi @claussteinmassl
Great thanks for your input. It’s looked way more advanced than my current ability :sweat_smile: but good for studying and learning from. Appreciate!!