# This script is designed for Microsoft Windows and UNIX systems. # Script information: # Creator: Nitin Arora (with lots of help from various Internet sites!) # Date: September 26, 2009 import os, fnmatch, sys, shutil, webbrowser, time br = ("\n") tab = ("\t") # Define functionFileAndFolders. def functionFileAndFolders(): if os.path.isdir("./MyFolderListFolder"): print br,br,"Error: I needed to make a folder called 'MyFolderListFolder'." print "Alas, it already exists."; raw_input("\n\nPress the Enter key to exit.") sys.exit() here = os.path.isfile("./MyFolderList.txt") if here ==True: print br,br,"Error: I needed to create a file called 'MyFolderList.txt'." print "Alas, it already exists."; raw_input("\n\nPress the Enter key to exit.") sys.exit() i=1 myFile = "MyFolderList.txt" fo = open(myFile, "w") print br; rootDir = raw_input("Please type or paste in the full path to the directory to mimick.\nYou *MUST* leave off the final slash: / or \\\n"); here2 = os.path.isdir(rootDir) if here2 !=True: print br,br,"Error: This is not a valid folder." print "Please remember to delete the file I made called 'MyFolderList.txt'."; fo.close() raw_input("\n\nPress the Enter key to exit.") sys.exit() print br,"Here are the folders I found ... ",br for dirname, dirnames, filenames in os.walk(rootDir): for subdirname in dirnames: myArray1 = os.path.join(dirname, subdirname) s0 = str(i) s1 = "MyFolderListFolder/" for value in myArray1: mySlice = len(rootDir)+1 s2=str(myArray1)[mySlice::] print s0,s2 fo.write(s1); fo.write(s2); fo.write(br); i=i+1 fo.close() myFile = "./MyFolderList.txt" fo = open(myFile, "r") os.mkdir("./MyFolderListFolder") lines = fo.readlines() for line in lines: if line[-1] == '\n': line2 = line[:-1] os.mkdir(line2) if line[-1] != '\n': line3 = line os.mkdir(line3) print br, br,"I'm done.",br; print "There's now a folder called MyFolderListFolder.",br; print "It contains a folder for each of the folders I listed above.",br; print "Please remember to delete the file I made called 'MyFolderList.txt'.",br,br; fo.close() raw_input("\n\nPress the Enter key to exit.") sys.exit() # Define functionFoldersOnly. def functionFoldersOnly(): if os.path.isdir("./MyFolderListFolder"): print br,br,"Error: I needed to make a folder called 'MyFolderListFolder'." print "Alas, it already exists."; raw_input("\n\nPress the Enter key to exit.") sys.exit() here = os.path.isfile("./MyFolderList.txt") if here !=True: print br,br,"Error: I needed your file called 'MyFolderList.txt'." print "Alas, it doesn't exist."; raw_input("\n\nPress the Enter key to exit.") sys.exit() userFile = "./MyFolderList.txt" fo = open(userFile, "r") os.mkdir("./MyFolderListFolder") lines = fo.readlines() if len(lines) == 0: print br,br,"Error: Your file called 'MyFolderList.txt' has problems." print "It has no data." print "Please fix that error, then try again." os.rmdir("MyFolderListFolder") raw_input("\n\nPress the Enter key to exit.") sys.exit() if len(lines) != len(set(lines)): print br,br,"Error: Your file called 'MyFolderList.txt' has problems." print "It has duplicates or empty rows." print "Please fix that error, then try again." shutil.rmtree("MyFolderListFolder") raw_input("\n\nPress the Enter key to exit.") sys.exit() for line in lines: s1 = "./MyFolderListFolder/" s2 =str(lines) if s2[2:20]=="MyFolderListFolder": print br,br,"Error: Your file called 'MyFolderList.txt' has problems." print "It's telling me to create a folder called 'MyFolderListFolder'." print "That conflicts with my need to have created that folder automatically." os.rmdir("MyFolderListFolder") raw_input("\n\nPress the Enter key to exit.") sys.exit() if line == '\n': print br,br,"Error: Your file called 'MyFolderList.txt' has problems." print "It has line breaks after empty rows." print "Please fix that error, then try again." shutil.rmtree("MyFolderListFolder") raw_input("\n\nPress the Enter key to exit.") sys.exit() for line in lines: s1 = "MyFolderListFolder/" if line[-1] == '\n': line2 = line[:-1] os.mkdir(s1+line2) if line[-1] != '\n': line3 = line os.mkdir(s1+line3) print br, br,"I'm done.",br; print "There's now a folder called MyFolderListFolder.",br; print "It contains a folder for each of the lines in your text file.",br; print "And please remember to delete or move *your* file called 'MyFolderList.txt'.",br,br; fo.close() raw_input("\n\nPress the Enter key to exit.") sys.exit() # Define functionDie. def functionDie(): print br,br,"Farewell."; raw_input("\n\nPress the Enter key to exit.") sys.exit() # User input section. print "Greetings.",br; print "I am MyFolderMaker." print "I make folders on Microsoft Windows and UNIX systems." print "I am written in Python.", br,br; print "Please select the number for the statement that you agree with:",br,br; print "1) I want to mimick the folder structure of an existing folder.",br; print " *I understand that MyFolderMaker won't work well on an entire drive"; print " or folders with permissions restrictions.",br; print "2) I have a file called 'MyFolderList.txt' and I want to create a folder"; print " named after every line of data in it.",br,br; print tab,"In other words, my 'MyFolderList.txt' file is akin to this:",br; print tab,tab,"cats",br; print tab,tab,"dogs",br; print tab,tab,"dogs/poodles",br; print tab,tab,"etc.",br,br; print tab,"And I want to make folders named 'cats', 'dogs', 'dogs/poodles', etc.",br,br; print " *I understand this file needs to be in the same folder as MyFolderMaker.",br; print "3) I simply wish to learn more about MyFolderMaker and to view the source code.",br; print "4) I wish to leave.",br,br; answer = raw_input("Please enter a number:\t"); if answer == "1": functionFileAndFolders(); if answer == "2": functionFoldersOnly(); if answer == "3": print br,br,"Check your web browser.",br; print "Farewell."; time.sleep(2) webbrowser.open('http://blog.humaneguitarist.org/?p=275') sys.exit() if answer == "4": functionDie(); else : print br,br,"That was not an option." raw_input("\n\nPress the Enter key to exit.") sys.exit()