Skip to content

Instantly share code, notes, and snippets.

@MrNegativeTW
Created February 28, 2024 00:06
Show Gist options
  • Save MrNegativeTW/0fdf8918fa2b9e74cf86795c2a775010 to your computer and use it in GitHub Desktop.
Save MrNegativeTW/0fdf8918fa2b9e74cf86795c2a775010 to your computer and use it in GitHub Desktop.
Find filenames certain characters and move the file to it's folder

Fuck Windows

Find filenames certain characters and move the file to it's folder

Why?

For instance, you have lots of files copied from the dash cam, the front lens file is named with the suffix "A", and the rear is "B".

Now try searching "A" using Windows 10's Explorer, it just simply tells you "no files"

Fuck windows.

How to

  1. Copy path name from your explorer
  2. Change the folder_path in the python script
  3. Run it
  4. Fuck Microsoft
import os
# Folders that contains the camcorder files.
folder_path = "W:/2024_0224" #2024_0224
### DO NOT MODIFY AFTER THIS LINE ###
suffixList = ["A", "B"]
suffixFrontLens = "A"
suffixRearLens = "B"
def search_filenames(folder_path, search_char="B"):
"""
This function searches for filenames containing the specified character in a given folder.
Args:
folder_path: The path to the folder to search.
search_char: The character to search for (default is "B").
Returns:
A list of filenames containing the search character.
"""
print("[Info] Searching for files contains \"%s\"" % search_char)
matching_filenames = []
for filename in os.listdir(folder_path):
file = os.path.join(folder_path, filename)
if os.path.isdir(file):
print("[Info] Skip, the %s is dir" % filename)
continue
if search_char in filename:
matching_filenames.append(filename)
return matching_filenames
if __name__ == "__main__":
for suffix in suffixList:
files = search_filenames(folder_path, suffix)
newFolder = os.path.join(folder_path, suffix)
if os.path.exists(newFolder) == False:
print("[Info] Creating \"%s\"" % newFolder)
os.makedirs(newFolder)
for file in files:
print("[Info] Moving %s to %s" % (file, newFolder))
os.rename(
os.path.join(folder_path, file),
os.path.join(folder_path, suffix, file)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment