Added a Folder Manger Script. (#188)

* Added Folder_Manager

* Added Name to README.md

Co-authored-by: Ayush Bhardwaj <classicayush@gmail.com>
This commit is contained in:
Harsh Raj 2020-10-14 17:18:34 +05:30 committed by GitHub
parent bd9a89afd0
commit e08ff04f49
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 16982 additions and 0 deletions

View File

@ -0,0 +1,2 @@
Example.txt
example.exe

View File

@ -0,0 +1,127 @@
# Imports
import os
import json
import shutil
# Main
if __name__ == "__main__":
ext = 0
def folder_manager(path,exception_file,extensions):
global ext
# Doing intial checks whether all inputs are valid or not.
#----------------------------------------------------------------------------------------------------------------
# Importing content of Exception file into a set.
with open(exception_file,'r',encoding='utf-8') as exct_file:
execptions = exct_file.read()
execptions = set(execptions.split('\n'))
# Changing directory to give path.
if os.path.isdir(path):
os.chdir(path)
# Checking if input extension is list or not.
if type( extensions) is not list:
raise Exception('Expected a list object.')
extensions = set( extensions)
# Capitalizing all files except the Exceptions. (Folders remains untouched)
#----------------------------------------------------------------------------------------------------------------
# Generating a list of all files in path folder except Exceptions.
all_files = {file.lower() for file in os.listdir(path) if os.path.isfile(file)}
all_files = all_files - execptions
# Capitalizing all file names in all_files list.
for file in all_files:
_name, _ext = os.path.splitext(file)
os.rename(os.path.join(path,file),('.'.join([_name.title(),_ext[1:]])))
#----------------------------------------------------------------------------------------------------------------
# Generating a list of files which needs to be renamed as numbers. (i.e. is input extensions)
rename_files = {file for file in all_files if file.split('.')[1] in extensions}
# Creating a folder named according to the file extensions and dumping the files in the folder.
for file_ in rename_files:
# Needed Variables
name, ext = os.path.splitext(file_)
ext = ext[1:]
folder_name = ext
# Code that creates a folder and dump the files in it.
if ext == '':
continue
if os.path.exists(os.path.join(path,ext)):
os.rename(os.path.join(path,ext),os.path.join(path,ext))
shutil.move(os.path.join(path,file_),os.path.join(path,ext,file_))
else:
if os.path.exists(os.path.join(path,folder_name)):
shutil.move(os.path.join(path,file_),os.path.join(path,folder_name,file_))
else:
os.makedirs(os.path.join(path,folder_name))
shutil.move(os.path.join(path,file_),os.path.join(path,folder_name,file_))
# Deleting Empty Folders, Non-empty Folders are untouched and clearing up some mess created earlier.
#----------------------------------------------------------------------------------------------------------------------
for folder in os.listdir(path):
# Deleted Empty folders
if os.path.isdir(folder):
if len(os.listdir(os.path.join(path,folder))) == 0:
os.rmdir(os.path.join(path,folder))
continue
#----------------------------------------------------------------------------------------------------------------------
def code_runner():
# Taking user input for Path.
#----------------------------------------------------------------------------------------------------------------------
path = input('\nEnter the Path of folder you want to Manage.\nPlease make sure what this script does by reading the Readme.md file.\nEnter Here : ')
while os.path.isdir(path) == False:
print('The given path is not valid! Please enter a correct Path.')
path = input('\nEnter the Path of folder you want to Manage.\nPlease make sure what this script does by reading the Readme.md\nEnter Here : ')
if os.path.isdir(path) == True:
break
# Taking user input for Exception file.
#----------------------------------------------------------------------------------------------------------------------
exception_file = input('\nEnter the path of Exception file.\nEnter here : ')
while os.path.isfile(exception_file) == False:
print('The given path is not valid! Please enter a correct Path.')
exception_file = input('\nEnter the path of Exception file.\nEnter here : ')
if os.path.isfile(exception_file) == True:
break
# Taking user input for extensions.
#----------------------------------------------------------------------------------------------------------------------
with open('all-file-extensions.json','r') as json_pointer:
json_file_exts = json.load(json_pointer)
extensions = input('\nEnter extensions of files you want to dump.\nExample - \"dll,exe,txt\" .Don\'t enclose in Inverted commas and seperate extensions with comma.\nEnter here : ')
extensions = extensions.replace(' ','')
extensions = extensions.split(',')
for ext in extensions:
ext_json = ext.upper()
while ext_json not in json_file_exts:
print(f'{ext} is a Invalid extension! Please Enter a valid extension.')
extensions = input('\nEnter extensions of files you want to dump.\nExample - \"dll,exe,txt\" .Don\'t enclose in Inverted commas and seperate extensions with comma.\nEnter here : ')
extensions = extensions.replace(' ','')
extensions = extensions.split(',')
for ext in extensions:
ext_json = ext.upper()
if ext_json in json_file_exts:
break
folder_manager(path=path,exception_file=exception_file, extensions= extensions)
print('\nCompleted! Thanks for using this script.')
code_runner()

32
Folder_Manager/Readme.md Normal file
View File

@ -0,0 +1,32 @@
# Folder_Manager
## About
### This script is used to make your Folder tidy by capitalizing the first letters of your File names. And this program also dumps wanted files into a new folder named according to the file extensions. Please see the images give below to understand better:
### Before
![Before](https://i.imgur.com/CabHHHt.png)
### After
![After](https://i.imgur.com/kfqikco.png)
### As you can see the files are dumped to a new folder and all rest files are Capitalized. This program only dumps the file after asking a extension name of the file like if you want to dump <i>.txt and .dll</i> files then enter "txt,dll" when asked. This script doesn't touch Subfolders so don't worry to getting your data messed up and if you want to made some file as <i>Exception</i> then Enter the name of the files in <i>Exception.txt</i> separated by a newline character. Don't worry I know I'm going to comprehensive you can see the below image to clear your issues :
### Code ran in CMD.
![Code](https://i.imgur.com/54SR6YD.png)
### Exceptions File
![Exception.txt](https://i.imgur.com/0Vyqsly.png)
## Some Important Warnings
+ ### All files present in this directory is needed to run the code (except README.md). So don't delete or rename any files. Important files:-
- #### all-file-extensions.json
- #### Exception.txt
- #### Folder_Manager.py
+ ### Just follow the instructions carefully this program may throw errors if exploited. I did handle the most Errors I can but make sure to do follow the instructions.
+ ### And please don't do any changes in Main Code if you don't know what is it.
### After Reading README simply run the code in any terminal.

File diff suppressed because it is too large Load Diff

View File

@ -160,6 +160,7 @@ So far, the following projects have been integrated to this repo:
|[Remove-Duplicate-Files](Remove-Duplicate-Files)|[Aayushi Varma](https://github.com/aayuv17) |[Remove-Duplicate-Files](Remove-Duplicate-Files)|[Aayushi Varma](https://github.com/aayuv17)
|[PDF2text](PDF2text)|[QuangPH](https://github.com/quangph-1686a) |[PDF2text](PDF2text)|[QuangPH](https://github.com/quangph-1686a)
|[Image Watermarker (batch)](imageWatermarker)|[Remco Halman](https://github.com/remcohalman) |[Image Watermarker (batch)](imageWatermarker)|[Remco Halman](https://github.com/remcohalman)
|[Folder Manager](Folder_Manager)|[Harsh Raj](https://github.com/DeadProgrammer0)|
|[IMDBQuerier](IMDBQuerier)|[Burak Bekci](https://github.com/Bekci) |[IMDBQuerier](IMDBQuerier)|[Burak Bekci](https://github.com/Bekci)
|[URL shortener](url_shortener)|[Sam Ebison](https://github.com/ebsa491) |[URL shortener](url_shortener)|[Sam Ebison](https://github.com/ebsa491)