Moving files by starting letter in powershell, python or other scripting language running windows -
i need script can recursively traverse c:\somedir\ , move files c:\someotherdir\x\ - x starting letter of file.
can help?
ended one:
import os shutil import copy2 import uuid import random source = ".\\pictures\\" dest = ".\\pictures_ordered\\" path, dirs, files in os.walk(source): f in files: print(f) starting_letter = f[0].upper() source_path = os.path.join(path, f) dest_path = os.path.join(dest, starting_letter) if not os.path.isdir(dest_path): os.makedirs(dest_path) dest_fullfile = os.path.join(dest_path, f) if os.path.exists(dest_fullfile): periodindex = source_path.rfind(".") renamed_soruce_path = source_path[:periodindex] + "_" + str(random.randint(100000, 999999)) + source_path[periodindex:] os.rename(source_path, renamed_soruce_path) copy2(renamed_soruce_path, dest_path) os.remove(renamed_soruce_path) else: copy2(source_path, dest_path) os.remove(source_path)`
here's simple script want. doesn't tell it's doing, , overwrite old file if there 2 files same name.
import os shutil import copy2 source = "c:\\source\\" dest = "c:\\dest\\" # iterate recursively through files , folders under source directory path, dirs, files in os.walk(source): # each directory iterate on files f in files: # grab first letter of filename starting_letter = f[0].upper() # construct full path of current source file source_path = os.path.join(path, f) # construct destination path using first letter of # filename folder dest_path = os.path.join(dest, starting_letter) # create destination folder if doesn't exist if not os.path.isdir(dest_path): os.makedirs(dest_path) # copy file destination path + starting_letter copy2(source_path, dest_path)
Comments
Post a Comment