1.

Solve : sorting folders according to content file extension!?

Answer»

Hello .. guys..

As i cannot find any batchfile wich can do my job,and
since i got a unsorted folder with more then 20000 files to be sorted, i am TIRED of doing it manualy
can or is it possible to batch this.
can some one tell me if this is possible in a msdos batch file ..
folders has more then one files but particular file extensions makes my decision where to put the folder.
this is the situation in unsorted
 
\---root
    +---sorted
    |   +---te1
    |   +---te2
    |   \---te3
    \---unsorted
        +---folder1
        |       pre.te3
        |       
        +---folder2
        |       pre.te1
        |       
        \---FOLDER3
                pre.te2

this is what i want to end up sorted all files in the corrsponding folders.

|   
\---root
    +---sorted
    |   +---te1
    |   |   \---folder2
    |   |           pre.te1
    |   |           
    |   +---te2
    |   |   \---folder3
    |   |           pre.te2
    |   |           
    |   \---te3
    |       \---folder1
    |               pre.te3
    |               
    \---unsorted


In short i wanne sort files  but keep the folder names in the new sorted map structure.

i have tried to use different aproaches like dir *.te1 /D/B/S > te1.log
and use this log file to sort, but got stuck on how to use this log in a "for" environement

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.
or copy  and use the unsorted as backup.

i do not need to view the output
as i said in my first post just moving/copying into the sorted folder

The folder structure shown above is only to clearify my problem. Since words are sometimes not enough and misunderstood.

Quote from: mazhive on September 06, 2010, 12:27:02 PM

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

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.

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.

but now i got this,...

print "Moving " r , " to ", os.path.join(dirsortedpath,ext)
                   ^
SyntaxError: Invalid syntax

i greatly appreciate your help and patience.
 

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 ,....
now it doesn't move anything.
and no error...
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


Discussion

No Comment Found