Guest User

Untitled

a guest
Apr 24th, 2014
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.09 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import os, subprocess
  4.  
  5.  
  6. def main():
  7.  
  8.     prompt = raw_input("Enter a directory name to search for: ")
  9.     captureFolder = "\\" + prompt #add the escaped slash to the folder name
  10.     shareList = ["","",""] #list of arbitrary share names
  11.  
  12.    
  13.     for share in shareList:
  14.         pathCheck(share, captureFolder) #check the path's existence, map the drive and offer a prompt to delete if it does exist.
  15.    
  16.         subprocess.call("net use x: /d /YES", stdout=subprocess.PIPE) #clean up mapped drive after each iteration over the share list so the drive letter is available again
  17.        
  18.        
  19.        
  20.        
  21. def pathCheck(sharename, folder):
  22.  
  23.     #add the creds and paths for mappings here
  24.    
  25.     if sharename == "": #check which share name we're dealing with
  26.         user = "" #provide appropriate credentials for mapping a drive
  27.         passwd = ""
  28.         path = '"\\\\<path without leading slashes>"' #path that corresponds to this share name for the drive mapping
  29.        
  30.         mapDrive(path, user, passwd, sharename, folder) #call mapDrive to set up the mapping
  31.                
  32.     elif sharename == "":
  33.         user = ""
  34.         passwd = ""
  35.         path = '"\\\\<path without leading slashes>"'
  36.        
  37.         mapDrive(path, user, passwd, sharename, folder)
  38.        
  39.     # etc
  40.    
  41.     else:
  42.         print("Share not recognised")
  43.  
  44.        
  45. def mapDrive(pathToMap, credUser, credPass, nameOfShare, dir):     
  46.     try:
  47.         subprocess.call("net use x: {} {} /user:{}".format(pathToMap, credPass, credUser), stdout=subprocess.PIPE) #map the drive, suppressing output
  48.     except:
  49.         print("Can't map drive to {}\n".format(nameOfShare))
  50.        
  51.     if os.path.exists('x:\\'+ dir):
  52.         print(nameOfShare + " exists")
  53.         pathRemove(nameOfShare, dir) #if the folder exists, give the option to remove it by calling pathRemove
  54.     else:
  55.         print(nameOfShare + " doesn't exist")
  56.        
  57.        
  58.        
  59.        
  60. def pathRemove(namedShare, directory):
  61.  
  62.     confirm = raw_input('Do you want to remove {} from {}? (y/n):  '.format(directory, namedShare))
  63.    
  64.     if confirm == 'y':
  65.         subprocess.call('rmdir /S /Q x:\\{}'.format(directory),shell=True, stdout=subprocess.PIPE)
  66.         print('Removed')
  67.     else:
  68.         print('')
  69.            
  70.        
  71.        
  72. if __name__ == "__main__":
  73.     main()
Advertisement
Add Comment
Please, Sign In to add comment