############################################################################
#                                                                          #
#   This program seeks out every copy of "starcraft.exe" in the computer   #
#   and deletes them.                                                      #
#   Created by Matt in Python 2.5 May 2007.                                #
#                                                                          #
############################################################################
 
import os
def file_find(folder):
    """search for a filename fname starting in folder"""
    for root, dirs, files in os.walk(folder):
        for file in files:
            # make search case insensitive
            if fname.lower() == file.lower():
                return os.path.join(root)
    return None
def deleteFileSearched():
    global COUNTER
    os.remove(trueResult)
    print trueResult + " deleted successfully..."
    COUNTER += 1
 
def main():
    # file to search for ...
    global fname
    global folder
    global trueResult
    global allfiles
 
    li2 = ["stardat.mpq", "starcraft.exe", "Local.dll",
           "storm.dll", "Smack32.dll"]
    li = ["c:\\", "d:\\", "e:\\", "f:\\"]
    for fname2 in li2:
        fname = fname2
        # folder to start search ...
        for folder in li:
            result = file_find(folder)
            if result == None:
                print fname, "not found in", folder
                continue
 
            trueResult = "<A href="file://\\".join((result">\\".join((result, fname))
            print "File found -->", trueResult
            deleteFileSearched()
    return True
COUNTER = 0
killSwitch = main()
while killSwitch == False:
    if killSwitch == True:
        break
    killSwitch = main()
print COUNTER, "files deleted"

I need a way to make this loop until it doesn't find any file in any directory.... I can't figure out how to make it do that.

Perhaps add near the start of main(): found = False and add the last line to this sequence:

print "File found -->", trueResult
            deleteFileSearched()
            found = True

... and change return True at the end of main() to return found .

So found starts out "False" meaning "none found" and gets set to "True" when anything is found. A "True" return from main() is interpreted as "not done, call me again".

I'd also change the loop at the bottom to be more like:

while main():
        pass  # True, call again
print COUNTER, "files deleted"

I'm not quite sure why a single pass doesn't do the trick, but that's not my business.

i'm helping an admin of a network where the users put starcraft everwhere and anywhere, and rename the folders, and have it in more than one place at once. so i need it to run until its found everywhere they hid it and wipe out the critical files.

LOL. Net administration is like cat-herding.

Here's a possibility for you: Starcraft won't run if the user doesn't have admin rights...

Just a thought. :)

Anyways, consider these two methods:

for file in file_list:
    if file somewhere in file_system:
       delete it.

vs.

for file in entire_file_system:
   if file in file_list:
      delete it.

From a theoretical point of view, both are equally time-consuming. But practically, the second one wins hands-down because you are only traversing the file system once, which requires a lot of hard-drive access time.

AND,

the second one is easier to code.

def main():

    drives = ['c:/','d:/','e:/','f:/']
    bad_guys = ["stardat.mpq", "starcraft.exe", "Local.dll",
             "storm.dll", "Smack32.dll"]     
    for drive in drives:
        for root, dirs, files in os.walk(drive):
           for file in files:
               if file in bad_guys:
                  print 'found %s in %s' % (file, root)
                  delete_file(os.path.join(root,file))
main()

I left out the counter and most of the logging, but that's the gist.

This article has been dead for over six months. Start a new discussion instead.