Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

simple file copy program

Name: Anonymous 2010-01-18 23:35

need a bash script or python to copy files in batches of 500 and put them into subdirectories

there can be any number of files (I have 15k), and the filenames are irrelevant, and the names of the subdirectories that are created to house the files is irrelevant

here's what I was trying to get work in python but it
errors out at file 339, don't know why



def BreakUp(indir):
   
    biglist = glob.glob('*.mp3')
    dircount = 0
    filecount = 0

    while biglist:
        dircount += 1
        os.mkdir('subdir'+str(dircount))
        for i in range(500):   
            filecount += 1
            try:
                src = biglist[filecount].translate("(copy)")
                os.system ("mv"+ " " + src + " subdir"+ str(dircount))          
                biglist.remove(src) 
            except IndexError:
                raise IndexError, "could not return item " + str(filecount) + " from list " + str(biglist)
        continue
               
           
       

if __name__ == '__main__':
    BreakUp(".")

Name: Anonymous 2010-01-19 0:08

>>1
python
Found your problem, bro.  Try this:
perl -e'mkdir$_ for 0..(@f=<*.mp3>)/500;rename$_,int($n++/500)."/$_"for@f'

Name: Anonymous 2010-01-19 3:52

>>7
Fine…here's the same thing expanded a bit, with the loops in block form instead of using statement modifiers.

@f = <*.mp3>;          #  @f becomes an array of filenames matching the glob.
for (0 .. @f / 500) {  #  @f taken in scalar context = length of @f,
                       #    so 0 through floor(@f / 500) are the directories we'll need.
                       #    Loop across them setting $_ to each number.
    mkdir $_;          #  Make a directory each time through the loop.
}
for (@f) {  #  This loop sets $_ to each individual filename.
    #  $n is undefined at first, which is zero when taken in a numerical context.
    #  It gets incremented each time through the loop, so floor($n / 500) tells us
    #    what directory we should be moving things into.
    rename $_, (int($n++ / 500) . "/" . $_);  #  Rename "file" to "dir/file".
    #  There is no proper "move" operator (the File::Copy module handles that),
    #    but rename is equivalent as long as it stays on the same filesystem.
}

Name: Anonymous 2010-01-19 17:11

What's sad is that for a simple administrative task like renaming some files in a systematic way I'd go for Python, just like >>1-chan did.
However, today >>3-sama opened my eyes and I saw the light!

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List