## Title: movefiles.py
## Author: Shawn Kirsch (www.shawnkirsch.com)
## Description: Recoursively delete all sub directorys while preserving
## files to the root directory.
## Version: 1.1
## Date: Sept, 28th 2010
import os, shutil
rootdir = os.getcwd()
scriptname = 'movefiles.py'
numofdirs = 0
numofiles = 0
def printcount():
print '-' * 40
print "Number of Directories: " + str(numofdirs)
print "Number of Files: " + str(numofiles)
print '-' * 40
def exploretree():
global numofdirs, numofiles
for x in os.listdir(os.getcwd()):
if os.path.isdir(x):
##print 'Dir : ' + x
numofdirs += 1
os.chdir(x)
exploretree()
os.chdir('..')
else:
##print 'File: ' + x
if(x != scriptname):
numofiles += 1
def movefiles():
for x in os.listdir(os.getcwd()):
if os.path.isdir(x):
os.chdir(x)
movefiles()
os.chdir('..')
if os.path.isfile(x) and os.getcwd() != rootdir:
shutil.move(x, '..')
def deletedirs():
global numofdirs
for x in os.listdir(os.getcwd()):
if os.path.isdir(x):
os.chdir(x)
deletedirs()
os.chdir('..')
try:
os.rmdir(x)
#print 'Removed Directory: ' + x
numofdirs -= 1
except:
pass
#print "Couldn't Delete Directory: " + x
exploretree()
printcount()
while(numofdirs!=0):
movefiles()
deletedirs()
print '.',
print 'Finished: All Files moved to the root folder.'
Diff to Previous Revision
--- revision 1 2010-09-28 17:41:59
+++ revision 2 2010-09-28 18:43:51
@@ -1,19 +1,50 @@
-#Recoursively Move all files up to root level.
+## Title: movefiles.py
+## Author: Shawn Kirsch (www.shawnkirsch.com)
+## Description: Recoursively delete all sub directorys while preserving
+## files to the root directory.
+## Version: 1.1
+## Date: Sept, 28th 2010
import os, shutil
-print os.getcwd()
-root = os.getcwd()
-
-endstring = 'C:\Users\shawn\Desktop\DATA\Programming\Python'
-scriptname = 'recourse.py'
+rootdir = os.getcwd()
+scriptname = 'movefiles.py'
numofdirs = 0
numofiles = 0
+def printcount():
+ print '-' * 40
+ print "Number of Directories: " + str(numofdirs)
+ print "Number of Files: " + str(numofiles)
+ print '-' * 40
+
+def exploretree():
+ global numofdirs, numofiles
+
+ for x in os.listdir(os.getcwd()):
+ if os.path.isdir(x):
+ ##print 'Dir : ' + x
+ numofdirs += 1
+ os.chdir(x)
+ exploretree()
+ os.chdir('..')
+ else:
+ ##print 'File: ' + x
+ if(x != scriptname):
+ numofiles += 1
+
+def movefiles():
+ for x in os.listdir(os.getcwd()):
+ if os.path.isdir(x):
+ os.chdir(x)
+ movefiles()
+ os.chdir('..')
+ if os.path.isfile(x) and os.getcwd() != rootdir:
+ shutil.move(x, '..')
+
def deletedirs():
global numofdirs
- print 'entering deletedirs | current dir: ' + str(os.getcwd())
- y = 0
+
for x in os.listdir(os.getcwd()):
if os.path.isdir(x):
os.chdir(x)
@@ -21,54 +52,19 @@
os.chdir('..')
try:
os.rmdir(x)
- print 'Removed Directory: ' + x
+ #print 'Removed Directory: ' + x
numofdirs -= 1
- y += 1
except:
- print "Couldn't Delete Directory: " + x
- print "Num of Directory's Removed: " + str(y)
- print "Number of Directories: " + str(numofdirs)
-
-
-def recourseprint():
- global numofdirs, numofiles
- for x in os.listdir(os.getcwd()):
- if os.path.isdir(x):
- print 'Dir : ' + x
- numofdirs += 1
- else:
- print 'File:' + x
- numofiles += 1
- #print x + "\t | dir: " + str(os.path.isdir(x))
- if os.path.isdir(x):
- os.chdir(x)
- recourseprint()
- os.chdir('..')
- #print os.getcwd()
- print '-' * 40
- print "Number of Directories: " + str(numofdirs)
- print "Number of Files: " + str(numofiles)
- print '-' * 40
-
-def recoursemove():
- for x in os.listdir(os.getcwd()):
- if os.getcwd() == root:
- print "Back To root"
- if x == scriptname:
- print 'at recourse.py'
- if os.path.isdir(x):
- os.chdir(x)
- recoursemove()
- os.chdir('..')
- if os.path.isfile(x) and os.getcwd() != root:
- shutil.move(x, '..')
+ pass
+ #print "Couldn't Delete Directory: " + x
-
+exploretree()
+printcount()
-recourseprint()
+while(numofdirs!=0):
+ movefiles()
+ deletedirs()
+ print '.',
-while(numofdirs != 0):
- recoursemove()
-
- deletedirs()
+print 'Finished: All Files moved to the root folder.'