Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Automator flatten folder structure

Hello,


I have been working on some automation service using automator in order to clean some folders I have on my mac.

So far it works perfectly here is the service created. But I have an issue. When files have the same name ( folder 1 > logo.png and folder 2 > logo.png )

once flatten It only keeps one of the two files.


Is there a way to keep both files, and just rename them ? ( logo_01.png and logo_02.png )


Here is the workflowfile


Hope someone will be able to help.


User uploaded file

MacBook Pro with Retina display, OS X El Capitan (10.11)

Posted on Nov 17, 2015 4:33 AM

Reply
Question marked as Best reply

Posted on Nov 17, 2015 8:41 AM

Hello,



It's possible with a script, here a solution with a bash script:


The workflow:

User uploaded file


Set the popups of the "Run Shell Script" action to "/bin/bash" and "as arguments"

Copy/paste this script:

dest="${@: -1}" ### the destination folder is the last argument in the list
set -- "${@:1:$(($#-1))}" ## remove the last argument in the list
for f in "$@" ### iterate a list of files
do
    name=${f##*/};
    if [ "$f" == "$dest/$name" ]; then continue; fi; ### this file is already in the destination folder (no need to move it, so next file)
    if [ -e "$dest/$name" ]; then ## this name already exists in the destination folder
        i=1
        if [[ \"$name\" != *.* ]]; then ext=""; base="$name"; else base=${name%.*}; ext=.${name##*.}; fi;
        while [ -e "$dest/$base$i$ext" ];do
            ((i++))
        done
        name="$base$i$ext" ### the new name (add a number before the name extension)
    fi
    mv "$f" "$dest/$name" ### move this file (rename if necessary)
done
echo "$dest" ### put the hostFolder to the output (for the next action)
2 replies
Question marked as Best reply

Nov 17, 2015 8:41 AM in response to edwardfromparis

Hello,



It's possible with a script, here a solution with a bash script:


The workflow:

User uploaded file


Set the popups of the "Run Shell Script" action to "/bin/bash" and "as arguments"

Copy/paste this script:

dest="${@: -1}" ### the destination folder is the last argument in the list
set -- "${@:1:$(($#-1))}" ## remove the last argument in the list
for f in "$@" ### iterate a list of files
do
    name=${f##*/};
    if [ "$f" == "$dest/$name" ]; then continue; fi; ### this file is already in the destination folder (no need to move it, so next file)
    if [ -e "$dest/$name" ]; then ## this name already exists in the destination folder
        i=1
        if [[ \"$name\" != *.* ]]; then ext=""; base="$name"; else base=${name%.*}; ext=.${name##*.}; fi;
        while [ -e "$dest/$base$i$ext" ];do
            ((i++))
        done
        name="$base$i$ext" ### the new name (add a number before the name extension)
    fi
    mv "$f" "$dest/$name" ### move this file (rename if necessary)
done
echo "$dest" ### put the hostFolder to the output (for the next action)

Automator flatten folder structure

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.