raizedinrus

Q: Script to move and rename files with sequential number based on filename

Pls help. I need a script to move and rename files based on filename. New name should have sequential index number.

I have three different folders "PPM", "MM6", "MM9" and they have their own different index numbers.

i.e :

ppm-01.jpg  to /PPM folder and rename to PPM4876.jpg (4876 is next sequential number in PPM folder)

mm9-01.jpg to /MM9 folder and rename to MM9396.jpg (396 is next sequential number in MM9 folder)

mm6-01.jpg to /MM6 folder and renamed to MM6325.jpg (325 is next sequential number in MM6 folder)

 

I have a script that I run as app. It works only for one "PPM" files and I can't figure out how to make it work

for the other two groups ("MM6", "MM9")

 

property theName : "PPM" -- the baseName

property n : 4876 -- the starting number

property theExt : ".jpg" -- name extension

global watchFolder, destinationFolder

on run

    set watchFolder to "Media HD:DBASE:Files:CONTAINERS:DROP:UNFILTERED:" as text

    set destinationFolder to "Media HD:DBASE:Files:CONTAINERS:DROP:PPM:" as text

end run

on idle

    repeat

        tell application "System Events"

            repeat with i in (get files of folder watchFolder whose name ends with theExt)

                move i to destinationFolder

                set name of the result to theName & n & theExt

                set n to n + 1

            end repeat

        end tell

        delay 1

    end repeat

end idle

 

Please help.

iMac (27-inch, Late 2012), iOS 9.3.3, null

Posted on Aug 3, 2016 1:35 PM

Close

Q: Script to move and rename files with sequential number based on filename

  • All replies
  • Helpful answers

  • by Jacques Rioux,

    Jacques Rioux Jacques Rioux Aug 4, 2016 6:34 AM in response to raizedinrus
    Level 4 (3,408 points)
    Mac OS X
    Aug 4, 2016 6:34 AM in response to raizedinrus

    Hi,


    If these folders ("PPM", "MM9", "MM6") are in the "Media HD:DBASE:Files:CONTAINERS:DROP:" folder:

    You can use this script


    property nameList : {"PPM", "MM9", "MM6"} -- list of the baseName
    property nList : {4876, 396, 325} -- list of the starting number
    property theExt : ".jpg" -- name extension
    global watchFolder, parentFolder
    on run
        set watchFolder to "Media HD:DBASE:Files:CONTAINERS:DROP:UNFILTERED:" as text
        set parentFolder to "Media HD:DBASE:Files:CONTAINERS:DROP:" as text
    end run
    
    on idle
        set tCount to count nameList
        tell application "System Events"
            repeat with x from 1 to tCount
                set theName to item x of nameList
                set destinationFolder to folder (parentFolder & theName)
                repeat with i in (get files of folder watchFolder whose name starts with theName and name ends with theExt)
                    move i to destinationFolder
                    set name of the result to theName & (item x of nList) & theExt
                    set item x of nList to (item x of nList) + 1
                end repeat
            end repeat
        end tell
        return 5 -- wait five seconds before running the next idle
    end idle