-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileOperations.py
More file actions
222 lines (178 loc) · 9.89 KB
/
FileOperations.py
File metadata and controls
222 lines (178 loc) · 9.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# This is the ultimate script to handle files in bulk
# Especially if you want to prefix/suffix all files in a folder
import os, re
from utils.textStyle import (
printGreen, printRed, printYellow, printCyan, printGrey,
printBlinking, printNegative, printCrossed,
)
if os.name == "nt":
os.system("cls")
else:
os.system("clear")
# ---------------------------------------------------------------------------- #
# Functions #
# ---------------------------------------------------------------------------- #
def recursiveFileList(cwd):
fileList = []
for root, dirnames, filenames in os.walk(cwd):
fileList.append(filenames)
return fileList[0]
# ---------------------------------------------------------------------------- #
# ███╗░░░███╗░█████╗░██████╗░██╗░░░██╗██╗░░░░░███████╗░██████╗
# ████╗░████║██╔══██╗██╔══██╗██║░░░██║██║░░░░░██╔════╝██╔════╝
# ██╔████╔██║██║░░██║██║░░██║██║░░░██║██║░░░░░█████╗░░╚█████╗░
# ██║╚██╔╝██║██║░░██║██║░░██║██║░░░██║██║░░░░░██╔══╝░░░╚═══██╗
# ██║░╚═╝░██║╚█████╔╝██████╔╝╚██████╔╝███████╗███████╗██████╔╝
# ╚═╝░░░░░╚═╝░╚════╝░╚═════╝░░╚═════╝░╚══════╝╚══════╝╚═════╝░
# ---------------------------------------------------------------------------- #
# Suffix all files in this folder #
# ---------------------------------------------------------------------------- #
def suffix():
suffix = input(printYellow("Specify suffix (ex. _SUFFIX): "))
confirm = input(printYellow(f"A total of {printYellow(len(filesToSearch))} files will be renamed to have the suffix {printYellow(suffix)}. Continue [Y/N]?"))
if confirm.upper() == "Y":
for fileName in filesToSearch:
split = os.path.splitext(fileName)
os.rename(fileName, f"{split[0]}{suffix}{split[1]}")
exit(printGreen("Done!"))
else:
exit("Operation cancelled. Exiting...")
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
# Prefix all files in this folder #
# ---------------------------------------------------------------------------- #
def prefix():
prefix = input(printYellow("Specify prefix (ex. PREFIX_): "))
confirm = input(printYellow(f"A total of {printYellow(len(filesToSearch))} files will be renamed to have the prefix {printYellow(prefix)}. Continue [Y/N]?"))
if confirm.upper() == "Y":
for fileName in filesToSearch:
os.rename(fileName, f"{prefix}{fileName}")
exit(printGreen("Done!"))
else:
exit("Operation cancelled. Exiting...")
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
# Search for text #
# ---------------------------------------------------------------------------- #
def search():
"""Search for text"""
search = input(printYellow("Search for: "))
matchesFound = 0
for fileName in filesToSearch:
# print(f"Searching in {printCyan(fileName)}")
with open(fileName) as file:
for n, line in enumerate(file):
if search in line:
matchesFound += 1
print(f"[{fileName}:{n+1}] {line}".replace(search, printGreen(search)))
print(f"A total of {printCyan(matchesFound)} matches were found in {printCyan(cwd)}")
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
# SEARCH AND REPLACE TEXT #
# ---------------------------------------------------------------------------- #
def searchreplace():
search = input(printYellow("Search for: "))
replace = input(printYellow("Replace with: "))
matchesFound = 0
for fileName in filesToSearch:
# print(f"Searching in {printCyan(fileName)}")
with open(fileName) as file:
for n, line in enumerate(file):
if search in line:
matchesFound += 1
currentContent = file.read()
newContent = re.sub(search, replace)
if matchesFound > 0:
confirm = input(f"A total of {printCyan(matchesFound)} matches were found in {printCyan(cwd)} are you sure you want to replace them [Y/N]?")
if confirm.upper() == "Y":
print(f"Performing {matchesFound} replacements...")
else:
exit("Not making any changes... Exiting")
else:
exit(printRed("No matches found... Exiting"))
# ---------------------------------------------------------------------------- #
# ---------------------------------------------------------------------------- #
# SEARCH FILENAMES #
# ---------------------------------------------------------------------------- #
def searchfilenames():
search = input(printYellow("Search for: "))
matchesFound = 0
for fileName in filesToSearch:
if search.lower() in fileName.lower():
matchesFound += 1
print(fileName.replace(search, printGreen(search)))
# ---------------------------------------------------------------------------- #
modules = [
# Name of module Category Shortname (function call)
["Suffix all files in this folder" , "FILENAMES" , "suffix"] ,
["Prefix all files in this folder" , "FILENAMES" , "prefix"] ,
["Change/Remove prefix from files in this folder", "FILENAMES" , "crprefix"] ,
["Change/Remove suffix from files in this folder", "FILENAMES" , "crsuffix"] ,
["Search filenames" , "FILENAMES" , "searchfilenames"] ,
["Search and replace filenames" , "FILENAMES" , "searchreplacefilenames"],
["Search for text in files" , "FILE CONTENT", "search"] ,
["Search and replace text in files" , "FILE CONTENT", "searchreplace"] ,
["Move files/folders to different location" , "LOCATION" , "move"] ,
["Copy files/folders to different location" , "LOCATION" , "copy"] ,
]
cwd = input("Specify a folder: ")
if os.path.isdir(cwd) != True:
exit(printRed("The folder you have specified does not exist. Unable to continue. Exiting..."))
os.chdir(cwd)
askRecursive = False
for file in os.listdir(cwd):
if os.path.isdir(file):
askRecursive = True
# -------------------------------- Helper text ------------------------------- #
print(f"""
Current script : {printCyan(__file__)}
Current working directory: {printCyan(os.getcwd())}
{printGreen("Green = Available function")}
{printGrey("Grey")} or {printRed("Red")} = Unavailable function
""")
currentCategory = ""
caller = {}
moduleName = {}
moduleCat = {}
for n, module in enumerate(modules):
moduleName[n+1] = thisModuleName = module[0]
moduleCat[n+1] = thisCatName = module[1]
caller[n+1] = module[2] # this will assign the operation number to a function
thisFunctionPointer = None if module[2] not in globals() else globals()[module[2]]
# Category name
if thisCatName != currentCategory:
print(f"{printCyan(thisCatName)}")
currentCategory = thisCatName
# Verify that function is defined
if callable(thisFunctionPointer):
print(f"[{printGreen(n+1)}] - {thisModuleName}")
else:
print(f"[{printRed(n+1)}] - {printGrey(thisModuleName)} {printRed('(Coming soon!)')}")
action = int(input(printYellow("\nChoose an operation: ")))
recursive = input(printYellow("Do you want to perform the selected operation recursively (include subfolders) [Y/N]?")) if askRecursive == True else "N"
# -------------------------- Modify recursive value -------------------------- #
if recursive == "Y":
recursive = "Yes"
else:
recursive = "No"
# -------------------------------- Search CWD -------------------------------- #
if recursive == "Yes":
filesToSearch = recursiveFileList(cwd)
else:
filesToSearch = os.listdir(cwd)
print(f"""
Current working directory: {printCyan(cwd)}
Action selected : [{printCyan(action)}] {printCyan(moduleName[action])}
Files in this folder : {printCyan(len(filesToSearch))}
Recursive : {printCyan(recursive)}
""")
# Prepare the selected function
funcToRun = caller[action]
if funcToRun in globals() and any(funcToRun in subarray for subarray in modules):
globals()[funcToRun]()
else:
exit(printRed("The action you chose is not available"))
# print(f"""
# Function name to run: {funcToRun}
# Global memory address: {globals()[funcToRun]}
# """)