can't figure out loop

Reply

Join Date: Jun 2006
Posts: 187
Reputation: Matt Tacular is an unknown quantity at this point 
Solved Threads: 7
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Unverified User

can't figure out loop

 
0
  #1
May 30th, 2007
  1. ############################################################################
  2. # #
  3. # This program seeks out every copy of "starcraft.exe" in the computer #
  4. # and deletes them. #
  5. # Created by Matt in Python 2.5 May 2007. #
  6. # #
  7. ############################################################################
  8.  
  9. import os
  10. def file_find(folder):
  11. """search for a filename fname starting in folder"""
  12. for root, dirs, files in os.walk(folder):
  13. for file in files:
  14. # make search case insensitive
  15. if fname.lower() == file.lower():
  16. return os.path.join(root)
  17. return None
  18. def deleteFileSearched():
  19. global COUNTER
  20. os.remove(trueResult)
  21. print trueResult + " deleted successfully..."
  22. COUNTER += 1
  23.  
  24. def main():
  25. # file to search for ...
  26. global fname
  27. global folder
  28. global trueResult
  29. global allfiles
  30.  
  31. li2 = ["stardat.mpq", "starcraft.exe", "Local.dll",
  32. "storm.dll", "Smack32.dll"]
  33. li = ["c:\\", "d:\\", "e:\\", "f:\\"]
  34. for fname2 in li2:
  35. fname = fname2
  36. # folder to start search ...
  37. for folder in li:
  38. result = file_find(folder)
  39. if result == None:
  40. print fname, "not found in", folder
  41. continue
  42.  
  43. trueResult = "<A href="file://\\".join((result">\\".join((result, fname))
  44. print "File found -->", trueResult
  45. deleteFileSearched()
  46. return True
  47. COUNTER = 0
  48. killSwitch = main()
  49. while killSwitch == False:
  50. if killSwitch == True:
  51. break
  52. killSwitch = main()
  53. print COUNTER, "files deleted"
  54.  
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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 311
Reputation: BearofNH is on a distinguished road 
Solved Threads: 40
BearofNH's Avatar
BearofNH BearofNH is offline Offline
Posting Whiz

Re: can't figure out loop

 
0
  #2
May 30th, 2007
Perhaps add near the start of main():
found = False
and add the last line to this sequence:
  1. print "File found -->", trueResult
  2. deleteFileSearched()
  3. 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:
  1. while main():
  2. pass # True, call again
  3. print COUNTER, "files deleted"

I'm not quite sure why a single pass doesn't do the trick, but that's not my business.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 187
Reputation: Matt Tacular is an unknown quantity at this point 
Solved Threads: 7
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Unverified User

Re: can't figure out loop

 
0
  #3
May 30th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 149
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: can't figure out loop

 
0
  #4
May 30th, 2007
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:

  1. for file in file_list:
  2. if file somewhere in file_system:
  3. delete it.
vs.

  1. for file in entire_file_system:
  2. if file in file_list:
  3. 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.

  1. def main():
  2.  
  3. drives = ['c:/','d:/','e:/','f:/']
  4. bad_guys = ["stardat.mpq", "starcraft.exe", "Local.dll",
  5. "storm.dll", "Smack32.dll"]
  6. for drive in drives:
  7. for root, dirs, files in os.walk(drive):
  8. for file in files:
  9. if file in bad_guys:
  10. print 'found %s in %s' % (file, root)
  11. delete_file(os.path.join(root,file))
  12. main()


I left out the counter and most of the logging, but that's the gist.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC