1.

Solve : sorting folders according to content file extension!?

Answer» <html><body><p>Hello .. guys.. <br/><br/>As i cannot find any batchfile wich can do my job,and <br/>since i got a unsorted folder with more then 20000 files to be sorted, i am <a href="https://interviewquestions.tuteehub.com/tag/tired-661145" style="font-weight:bold;" target="_blank" title="Click to know more about TIRED">TIRED</a> of doing it manualy<br/>can or is it possible to batch this. <br/>can some one tell me if this is possible in a msdos batch file ..<br/>folders has more then one files but particular file extensions makes my decision where to put the folder. <br/>this is the situation in unsorted <br/> <br/>\---root<br/>    +---sorted<br/>    |   +---te1<br/>    |   +---te2<br/>    |   \---te3<br/>    \---unsorted<br/>        +---folder1<br/>        |       pre.te3<br/>        |       <br/>        +---folder2<br/>        |       pre.te1<br/>        |       <br/>        \---<a href="https://interviewquestions.tuteehub.com/tag/folder3-2085922" style="font-weight:bold;" target="_blank" title="Click to know more about FOLDER3">FOLDER3</a><br/>                pre.te2 <br/><br/>this is what i want to end up sorted all files in the corrsponding folders.<br/><br/>|   <br/>\---root<br/>    +---sorted<br/>    |   +---te1<br/>    |   |   \---folder2<br/>    |   |           pre.te1<br/>    |   |           <br/>    |   +---te2<br/>    |   |   \---folder3<br/>    |   |           pre.te2<br/>    |   |           <br/>    |   \---te3<br/>    |       \---folder1<br/>    |               pre.te3<br/>    |               <br/>    \---unsorted<br/><br/><br/>In short i wanne sort files  but keep the folder names in the new sorted map structure. <br/><br/>i have tried to use different aproaches like dir *.te1 /D/B/S &gt; te1.log<br/>and use this log file to sort, but got stuck on how to use this log in a "for" environement<br/><br/>help is appreciateddo you actually just need to view the output (at your screen) as sorted, or do you actually want to move the files from "unsorted" folder into the the "sorted" folder, then display them ? define your problem clearlymoving the folders as shown above.<br/>or copy  and use the unsorted as backup.<br/><br/>i do not need to view the output <br/>as i said in my first post just moving/copying into the sorted folder<br/><br/>The folder structure shown above is only to clearify my problem. Since words are sometimes not enough and misunderstood.<br/><br/> Quote from: mazhive on September 06, 2010, 12:27:02 PM</p><blockquote>can some one tell me if this is possible in a msdos batch file ..<br/></blockquote> yes, its possible. My solution is not batch, so if you can install Python, here's a script you can use<br/><br/> Code: <a>[Select]</a>import os<br/>import shutil<br/>root="C:\\"<br/>dirsortedpath=os.path.join(root,"test","sorted")  # put sorted path here<br/>dirunsortedpath=os.path.join(root,"test","unsorted")  # unsorted path<br/>os.chdir(dirsortedpath)<br/>dirsorted = [ d.lower() for d in os.listdir(".") if os.path.isdir(d) ]<br/>for r,d,f in os.walk(dirunsortedpath):<br/>   for files in f:<br/>      ext=files[-3:].lower() #get the extension<br/>      if ext in dirsorted:<br/>         try:<br/>            shutil.move(r, os.path.join(dirsortedpath,ext))<br/>         except Exception,e:<br/>            print e<br/>         else:<br/>            print "moved .."<br/>save as myscript.py on the command line<br/> Code: <a>[Select]</a>c:\test&gt; python myscript.py<br/><br/><br/>If not, you can wait for your batch solution.that would be no problem.<br/>thanx !!!! gone chek it out later this evening thanx... it seems that it moves only the file not the folder <br/><br/>so i get root\sorted\te1\xxxxxx.te1<br/>not  root\sorted\te1\folder 3\xxxxx.te1<br/><br/>do i have to change more than only the sorted path and unsorted path.<br/><br/>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 <a href="https://interviewquestions.tuteehub.com/tag/09-256415" style="font-weight:bold;" target="_blank" title="Click to know more about 09">09</a>, 2010, 05:22:41 AM<blockquote>it seems that it moves only the file not the folder <br/><br/>so i get root\sorted\te1\xxxxxx.te1<br/>not  root\sorted\te1\folder 3\xxxxx.te1<br/><br/>do i have to change more than only the sorted path and unsorted path.<br/><br/>thanx.. alot for this piece of code... i use cygwin to do some linux like stuf and it has python installed on default. <br/></blockquote> <br/>you can always debug your script by using print . First comment out shutil.move line , then just insert a print statement<br/> Code: <a>[Select]</a>print "Moving " r , " to ", os.path.join(dirsortedpath,ext)<br/># shutil.move(r, os.path.join(dirsortedpath,ext))<br/><br/>see if its what you want. If yes, then the shutil.move should not be the problem.<br/>sorry but i get an error message.<br/><br/>print "Moving " r , " to ", os.path.join(dirsortedpath,ext)<br/>                                                                        ^<br/>IndentationError: unident does not match any outer indentation level<br/><br/>####BOF<br/><br/>import os<br/>import shutil<br/>root="C:\\"<br/>dirsortedpath=os.path.join(root,"TEMP","batch","root","sorted")  # put sorted path here<br/>dirunsortedpath=os.path.join(root,"TEMP","batch","root","unsorted")  # unsorted path<br/>os.chdir(dirsortedpath)<br/>dirsorted = [ d.lower() for d in os.listdir(".") if os.path.isdir(d) ]<br/>for r,d,f in os.walk(dirunsortedpath):<br/>   for files in f:<br/>      ext=files[-3:].lower() #get the extension<br/>      if ext in dirsorted:<br/>      try:<br/>   print "Moving " r , " to ", os.path.join(dirsortedpath,ext)<br/>#            shutil.move(r, os.path.join(dirsortedpath,ext))<br/>         except Exception,e:<br/>            print e<br/>         else:<br/>            print "moved .."<br/><br/>###EOF<br/><br/><br/>I am using Nano to add or change a line of the python script.<br/> Python  is particular about indentation, so indent properly(use spaces, not tabs)<br/><br/> Code: <a>[Select]</a>import os<br/>import shutil<br/>root="C:\\"<br/>dirsortedpath=os.path.join(root,"TEMP","batch","root","sorted")  # put sorted path here<br/>dirunsortedpath=os.path.join(root,"TEMP","batch","root","unsorted")  # unsorted path<br/>os.chdir(dirsortedpath)<br/>dirsorted = [ d.lower() for d in os.listdir(".") if os.path.isdir(d) ]<br/>for r,d,f in os.walk(dirunsortedpath):<br/>   for files in f:<br/>      ext=files[-3:].lower() #get the extension<br/>      if ext in dirsorted:<br/>          try:<br/>              print "Moving " r , " to ", os.path.join(dirsortedpath,ext)<br/>             #shutil.move(r, os.path.join(dirsortedpath,ext))<br/>          except Exception,e:<br/>              print e<br/>          else:<br/>              print "moved .."<br/><br/>thanx i didn't know,... i am not that familiar with python scripting.<br/><br/>but now i got this,...<br/><br/>print "Moving " r , " to ", os.path.join(dirsortedpath,ext)<br/>                   ^<br/>SyntaxError: Invalid syntax<br/><br/>i greatly appreciate your help and patience.<br/>  Quote from: mazhive on September 09, 2010, 05:21:08 PM<blockquote>thanx i didn't know,... i am not that familiar with python scripting.<br/><br/>but now i got this,...<br/><br/>print "Moving " r , " to ", os.path.join(dirsortedpath,ext)<br/>                   ^<br/>SyntaxError: Invalid syntax<br/><br/>i greatly appreciate your help and patience.<br/> <br/></blockquote> <br/>sorry, it should be<br/> Code: <a>[Select]</a>print "Moving ", r , " to ", os.path.join(dirsortedpath,ext)<br/><br/>Now proceed to look at the <a href="https://www.python.org/doc/">Python</a> documentations. Take the tutorial if you are not familiarthanx ,... hmm i feel like stupid right now ,....<br/>now it doesn't move anything.<br/>and no error... Quote from: mazhive on September 09, 2010, 06:13:35 PM<blockquote>thanx ,... hmm i feel like stupid right now ,....<br/>now it doesn't move anything.<br/>and no error... <br/></blockquote> 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 <br/><br/> And what does your output <a href="https://interviewquestions.tuteehub.com/tag/show-236642" style="font-weight:bold;" target="_blank" title="Click to know more about SHOW">SHOW</a>? 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<br/><br/> Code: <a>[Select]</a>if ext in dirsorted:<br/>is not executed. Like i told you, if you are not sure, put in print statements to show the execution<br/><br/> Code: <a>[Select]</a>....<br/>for r,d,f in os.walk(dirunsortedpath):<br/>   for files in f:   <br/>      print "now doing file: " , os.path.join(r,files)<br/>      ext=files[-3:].lower() #get the extension<br/>      print "Extension is ", ext<br/> .....<br/>Just a question, will the folders only contain <strong><em>1</em></strong> file type?oke,..<br/><br/>well some <a href="https://interviewquestions.tuteehub.com/tag/times-344892" style="font-weight:bold;" target="_blank" title="Click to know more about TIMES">TIMES</a> it has 3 files but only 1 is important for me to decide <br/>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<br/> so these te1 or te2 are just for experimental environment so i can use it in my actual folder and files later.<br/><br/>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.<br/><br/>###BOF<br/><br/>import os<br/>import shutil<br/>root="C:\\"<br/>dirsortedpath=os.path.join(root,"TEMP","batch","root","sorted")  # put sorted path here<br/>dirunsortedpath=os.path.join(root,"TEMP","batch","root","unsorted")  # unsorted path<br/>os.chdir(dirsortedpath)<br/>dirsorted = [ d.lower() for d in os.listdir(".") if os.path.isdir(d) ]<br/>for r,d,f in os.walk(dirunsortedpath):<br/>   for files in f:   <br/>      print "now doing file: " , os.path.join(r,files)<br/>      ext=files[-3:].lower() #get the extension<br/>      print "Extension is ", ext<br/>for r,d,f in os.walk(dirunsortedpath):<br/>   for files in f:<br/>      ext=files[-3:].lower() #get the extension<br/>      if ext in dirsorted:<br/>          try:<br/>              print "Moving ", r , " to ", os.path.join(dirsortedpath,ext)<br/>              shutil.move(r, os.path.join(dirsortedpath,ext))<br/>          except Exception,e:<br/>              print e<br/>          else:<br/>              print "moved .."<br/><br/>###EOF</body></html>


Discussion

No Comment Found