How to remove files using Python (File Remover program included)

1. Problem Description:

After doing some recovery from hard drive the software has created thousand of files which I didn’t needed.

Around 200k txt files . Removing that huge number of files using Windows is very painful because the Windows Explorer crashed. Bill Gates can’t handle it!!!

So I have write some basic script to do it for me:

2. Program (use it at your own risk )

Program goes through  all files in specifies directory and remove specific file types eg: .txt – text file.

You can run this program from Python Gui

import os

indir=”H:\\marzena_zdjecia”

def file_remove(extension,indir):
counter = 0
for root, dirs, filenames in os.walk(indir):
#print(root)
#print(dirs)
for f in filenames:
full_path = root + “\\” + f
if f[-3:] == extension:
#print(full_path)
os.remove(full_path)
counter = counter + 1
print(“Removing ” + full_path)
print(“Removing type: ” + extension +” Number of files removed: “+ str(counter))

file_remove(‘txt’,indir)
file_remove(‘sys’,indir)
file_remove(‘swc’,indir)
file_remove(‘html’,indir)
file_remove(‘xml’,indir)
file_remove(‘chm’,indir)
file_remove(‘rtf’,indir)
file_remove(‘dll’,indir)
file_remove(‘cab’,indir)
file_remove(‘lnk’,indir)

How to iterate through every file in one directory using Ruby

There are many ways of doing it the one below is my favorite.

We use Dir.glob – function and we iterate through all directories. This might be useful when you need to remove thousand of .txt files after system recover for instance.

Dir.glob(“**/*.txt”) do |my_text_file| # note one extra “*”
puts “working on: #{my_text_file}…”
end