Created
October 8, 2019 02:39
-
-
Save bunlongheng/c217e1353380cfd0f75a54013c2cf20c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -------------------------------------------------------------- | |
# Goal : Remove file base on input match | |
# Run : curl 45.55.88.57/code/fileModifier.py | python3 | |
import os | |
import sys | |
rootdir = os.path.abspath(os.curdir) | |
print(rootdir) | |
#Give string to remove from folder names. Ensure that removing a string doens't make the folder name empty. It wont work | |
removeStringFromFolderName = input('Remove this from folder names :') | |
while removeStringFromFolderName == '': | |
print('Empty string not allowed') | |
removeStringFromFolderName = input('Remove this file if contain : ') | |
count = 0 | |
subdir = [x for x in os.walk(rootdir)] | |
toRemove = [] | |
for chunk in subdir: | |
folders = chunk[1] | |
if len(folders) > 0: | |
for aDir in folders: | |
if removeStringFromFolderName in aDir: | |
toRemove.append((chunk[0], aDir)) | |
toRemove.reverse() | |
for folders in toRemove: | |
oldPath = (os.path.join(folders[0], folders[1])) | |
newPath = (os.path.join(folders[0], folders[1].replace(removeStringFromFolderName,''))) | |
os.rename(oldPath, newPath) | |
count +=1 | |
subdir = [x for x in os.walk(rootdir)] | |
for chunk in subdir: | |
folders = chunk[1] | |
if len(folders) > 0: | |
for aDir in folders: | |
if removeStringFromFolderName in aDir: | |
print(os.path.join(chunk[0], aDir)) | |
oldPath = (os.path.join(chunk[0], aDir)) | |
newPath = (os.path.join(chunk[0], aDir.replace(removeStringFromFolderName,''))) | |
os.rename(oldPath, newPath) | |
count +=1 | |
print('Renamed', count, 'files') | |
count = 0 | |
#Give string to delete files which contain this string | |
removeThisFileNameIfContain = input('Enter string to delete files which contain this string: ') | |
if removeThisFileNameIfContain == '' or removeThisFileNameIfContain == ' ': | |
print('Exiting') | |
else: | |
confirm = input ('Are you sure you want to delete files containg the string "' + removeThisFileNameIfContain +'" ? ') | |
if confirm.lower() == 'yes': | |
for subdir, dirs, files in os.walk(rootdir): | |
for aFile in files: | |
if '.py' in aFile: | |
continue | |
if removeThisFileNameIfContain in aFile: | |
#os.remove(os.path.join(subdir, aFile)) | |
count += 1 | |
print('Deleted', count, 'files') | |
else: | |
print('Exiting') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment