sort files by number and move to created folder

Discussion in 'Mac Programming' started by govmac, Feb 8, 2016.

  1. govmac macrumors newbie

    Joined:
    Jun 21, 2007
    #1
    I have a bunch of folders that contain upwards of 50,000 files and which are becoming unweildy to deal with. The files in each folder are sequentially named with a time and datestamp. I would like to use automator or applescript to do the following sorting action. Can someone help me with either some code or an example automator workflow?

    I would like to have a script that goes to a "parent folder" it then selects the first 1000 files based on sequential naming and then creates a folder in the parent folder named something like folder1 and then moves those first 1000 files into that folder, then the script selects the next 1000 files, creates a folder named folder2 in the parent folder and moves those 1000 files into that folder etc. etc. until all the files have been moved into new sequentially named folders.

    any help with this would be greatly appreciated!
     
  2. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #2
    Hi,

    Something like this?

    Code:
    set theMaxFilesPerFolder to 5 --change this number to however many files you want in a folder
    
    tell application "Finder"
        set theFolder to choose folder
      
        set theFiles to files of theFolder sort by name
        set theNoOfTotalFiles to count of theFiles
      
        set theFileNo to 1
        repeat with theFile in theFiles
            set theFolderNumber to round (theFileNo / theMaxFilesPerFolder) rounding up
          
            if folder ("folder_" & theFolderNumber) of theFolder exists then
                set theDestFolder to folder ("folder_" & theFolderNumber) of theFolder
            else
                set theDestFolder to make new folder in folder theFolder with properties {name:"folder_" & theFolderNumber}
            end if
          
            move theFile to theDestFolder
            set theFileNo to theFileNo + 1
        end repeat
      
      
      
    end tell
    
    
    
    NB. I've not tested this thoroughly. Use at your own risk, after making sure you have a backup! usual sensible precautions...

    Yes, I am available for children's parties, barmitzvahs, private functions etc.
     
  3. govmac thread starter macrumors newbie

    Joined:
    Jun 21, 2007
    #3
    Superscape - first, THANK YOU! this code works pretty much perfect with one exception. I have done a bunch of trial and error testing today. When the "Parent Folder" or in this case "theFolder" contains more than about 1300 files I get a Finder Error- see below- it seems that there is some kind of a timeout limit that I am running into. I get the error regardless of how many files I set "theMaxFilesPerFolder" to. It happens when the code is sorting the files by name. I wonder if there is a "timeout" command that could be entered? If I have less than about 1200 files in the parent folder the code works flawlessly.

    The parent folders I want to use this code with to sort files contain upwards of 25,000 files each so the timeout must be set to some value long enough to allow the code to sort all the files by name. Maybe there is some machine speed limitations here as well? I am running OSX Lion on a mac mini for this work. the error is below.

    tell application "Finder"
    choose folder
    sort every file of alias "Macintosh HD:Users:Frank:Desktop:camsorttestfolder3:" by name
    Result:
    error "Finder got an error: AppleEvent timed out." number -1712
     
  4. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #4

Share This Page