| 1. |
Solve : sorting folders according to content file extension!? |
|
Answer» Hello .. guys.. can some one tell me if this is possible in a msdos batch file ..yes, its possible. My solution is not batch, so if you can install Python, here's a script you can use Code: [Select]import os import shutil root="C:\\" dirsortedpath=os.path.join(root,"test","sorted") # put sorted path here dirunsortedpath=os.path.join(root,"test","unsorted") # unsorted path os.chdir(dirsortedpath) dirsorted = [ d.lower() for d in os.listdir(".") if os.path.isdir(d) ] for r,d,f in os.walk(dirunsortedpath): for files in f: ext=files[-3:].lower() #get the extension if ext in dirsorted: try: shutil.move(r, os.path.join(dirsortedpath,ext)) except Exception,e: print e else: print "moved .." save as myscript.py on the command line Code: [Select]c:\test> python myscript.py If not, you can wait for your batch solution.that would be no problem. thanx !!!! gone chek it out later this evening thanx... it seems that it moves only the file not the folder so i get root\sorted\te1\xxxxxx.te1 not root\sorted\te1\folder 3\xxxxx.te1 do i have to change more than only the sorted path and unsorted path. thanx.. alot for this piece of code... i use cygwin to do some linux like stuf and it has python installed on default. Quote from: mazhive on September 09, 2010, 05:22:41 AM it seems that it moves only the file not the folder you can always debug your script by using print . First comment out shutil.move line , then just insert a print statement Code: [Select]print "Moving " r , " to ", os.path.join(dirsortedpath,ext) # shutil.move(r, os.path.join(dirsortedpath,ext)) see if its what you want. If yes, then the shutil.move should not be the problem. sorry but i get an error message. print "Moving " r , " to ", os.path.join(dirsortedpath,ext) ^ IndentationError: unident does not match any outer indentation level ####BOF import os import shutil root="C:\\" dirsortedpath=os.path.join(root,"TEMP","batch","root","sorted") # put sorted path here dirunsortedpath=os.path.join(root,"TEMP","batch","root","unsorted") # unsorted path os.chdir(dirsortedpath) dirsorted = [ d.lower() for d in os.listdir(".") if os.path.isdir(d) ] for r,d,f in os.walk(dirunsortedpath): for files in f: ext=files[-3:].lower() #get the extension if ext in dirsorted: try: print "Moving " r , " to ", os.path.join(dirsortedpath,ext) # shutil.move(r, os.path.join(dirsortedpath,ext)) except Exception,e: print e else: print "moved .." ###EOF I am using Nano to add or change a line of the python script. Python is particular about indentation, so indent properly(use spaces, not tabs) Code: [Select]import os import shutil root="C:\\" dirsortedpath=os.path.join(root,"TEMP","batch","root","sorted") # put sorted path here dirunsortedpath=os.path.join(root,"TEMP","batch","root","unsorted") # unsorted path os.chdir(dirsortedpath) dirsorted = [ d.lower() for d in os.listdir(".") if os.path.isdir(d) ] for r,d,f in os.walk(dirunsortedpath): for files in f: ext=files[-3:].lower() #get the extension if ext in dirsorted: try: print "Moving " r , " to ", os.path.join(dirsortedpath,ext) #shutil.move(r, os.path.join(dirsortedpath,ext)) except Exception,e: print e else: print "moved .." thanx i didn't know,... i am not that familiar with python scripting. but now i got this,... print "Moving " r , " to ", os.path.join(dirsortedpath,ext) ^ SyntaxError: Invalid syntax i greatly appreciate your help and patience. Quote from: mazhive on September 09, 2010, 05:21:08 PM thanx i didn't know,... i am not that familiar with python scripting. sorry, it should be Code: [Select]print "Moving ", r , " to ", os.path.join(dirsortedpath,ext) Now proceed to look at the Python documentations. Take the tutorial if you are not familiarthanx ,... hmm i feel like stupid right now ,.... now it doesn't move anything. and no error... Quote from: mazhive on September 09, 2010, 06:13:35 PM thanx ,... hmm i feel like stupid right now ,....of course it doesn't because the shutil.move line is commented right ?? Uncomment the line when you are ready to move, but in the meantime, just use the print statements to see if you have got the correct files And what does your output SHOW? If you have print statements in your script, it will show when you run the script. If it did not show, then it shows that the Code: [Select]if ext in dirsorted: is not executed. Like i told you, if you are not sure, put in print statements to show the execution Code: [Select].... for r,d,f in os.walk(dirunsortedpath): for files in f: print "now doing file: " , os.path.join(r,files) ext=files[-3:].lower() #get the extension print "Extension is ", ext ..... Just a question, will the folders only contain 1 file type?oke,.. well some TIMES it has 3 files but only 1 is important for me to decide the actual extensions would be ,t64 ,d64 ,crt ,p00 ,prg with these files, sometime a txt file or nfo or doc are within that folder but that does not make my decision to move it in a folder so these te1 or te2 are just for experimental environment so i can use it in my actual folder and files later. ghostdog... well thats the point it does do a output but doesn't move the folder now i put the other piece of code in it and it gives me output but no moved folders yet it moved the files. ###BOF import os import shutil root="C:\\" dirsortedpath=os.path.join(root,"TEMP","batch","root","sorted") # put sorted path here dirunsortedpath=os.path.join(root,"TEMP","batch","root","unsorted") # unsorted path os.chdir(dirsortedpath) dirsorted = [ d.lower() for d in os.listdir(".") if os.path.isdir(d) ] for r,d,f in os.walk(dirunsortedpath): for files in f: print "now doing file: " , os.path.join(r,files) ext=files[-3:].lower() #get the extension print "Extension is ", ext for r,d,f in os.walk(dirunsortedpath): for files in f: ext=files[-3:].lower() #get the extension if ext in dirsorted: try: print "Moving ", r , " to ", os.path.join(dirsortedpath,ext) shutil.move(r, os.path.join(dirsortedpath,ext)) except Exception,e: print e else: print "moved .." ###EOF |
|