benjamin-mills

Q: Use Automator to search through folders and find ones with specified missing subfolder.

I'm trying to figure out how to use automator to look through a block of folders (and subfolders) and then list all folders that do not contain a specifically named folder.

 

Folders are structured as below:

 

delivery001/20150504/2157x1536

delivery001/20150505

delivery001/20150506

delivery001/20150507/2157x1536

delivery001/20150508

 

So I'd like to be able to formulate a way to only find the folders that DO NOT contain the '2157x1536' subfolder and create a list of these results if possible?

 

I've tried multiple ways, but can only get the list to show folders containing that folder and not vice versa! Any ideas?

MacBook Pro, OS X Mountain Lion (10.8.2), 2.2 Ghz i7 Quad Core/8Gb RAM/750GB

Posted on May 13, 2015 3:17 AM

Close

Q: Use Automator to search through folders and find ones with specified missing subfolder.

  • All replies
  • Helpful answers

  • by Tony T1,Helpful

    Tony T1 Tony T1 May 13, 2015 4:25 AM in response to benjamin-mills
    Level 6 (9,249 points)
    Mac OS X
    May 13, 2015 4:25 AM in response to benjamin-mills

    You can do this in Terminal with find and test (using the \! not operator):

     

        find $HOME/Desktop/delivery001 -type d -depth 1 -exec test \! -d "{}/2157x1536" ';' -print

     

    [Note: change $HOME/Desktop/delivery001 to match your directory]

     

    You can put this in Automator:

     

    Screen Shot 2015-05-13 at 7.16.38 AM.png

     

    The Run Shell Script Action is:

     

         find $1 -type d -depth 1 -exec test \! -d "{}/2157x1536" ';' -print > $HOME/Desktop/missing.txt

     

    and a list of the directories missing 2157x1536 will be written to a file named "missing.txt" on the Desktop.

  • by benjamin-mills,

    benjamin-mills benjamin-mills May 13, 2015 5:05 AM in response to Tony T1
    Level 1 (71 points)
    May 13, 2015 5:05 AM in response to Tony T1

    Is it possible to just remove the print to text part of the shell script and add a save to text document workflow block after the shell script? Will it contain the correct data??

     

    Is it also possible to amend the depth of subfolders it looks to? For example say in one of the directories contained an extra of level of subfolder, would it be possible to also get it to show those folders and that they're missing a 2157x1536 folder?

     

    Also to advance on what you've sent me so far, using the results created, is there a way to then add in a 2157x1536 folder to all those directories that do not currently contain one?

  • by benjamin-mills,

    benjamin-mills benjamin-mills May 13, 2015 5:05 AM in response to benjamin-mills
    Level 1 (71 points)
    May 13, 2015 5:05 AM in response to benjamin-mills

    Thanks for your help, tricky one :S

  • by Tony T1,Helpful

    Tony T1 Tony T1 May 13, 2015 5:17 AM in response to benjamin-mills
    Level 6 (9,249 points)
    Mac OS X
    May 13, 2015 5:17 AM in response to benjamin-mills

    benjamin-mills wrote:

     

    Is it possible to just remove the print to text part of the shell script and add a save to text document workflow block after the shell script? Will it contain the correct data??

     

    Just use:

         find $1 -type d -depth 1 -exec test \! -d "{}/2157x1536" \; -print

    and the results will be passed to the next action

  • by Tony T1,

    Tony T1 Tony T1 May 13, 2015 5:18 AM in response to benjamin-mills
    Level 6 (9,249 points)
    Mac OS X
    May 13, 2015 5:18 AM in response to benjamin-mills

    benjamin-mills wrote:

     

    Also to advance on that, using the results created, is there a way to then add in a 2157x1536 folder to all those directories that do not currently contain one?

     

    Yes.  Just need to add execdir.

    For the Run Shell Script Action:

        find $1 -type d -depth 1 -exec test \! -d "{}/2157x1536" \; -execdir mkdir "{}/2157x1536" \;

  • by benjamin-mills,

    benjamin-mills benjamin-mills May 13, 2015 5:16 AM in response to Tony T1
    Level 1 (71 points)
    May 13, 2015 5:16 AM in response to Tony T1

    Thanks so much Tony!

     

    So last but not least.... Say the folders that were missing the 2157x1536 directories contained a bunch of files, would there be a way to move those files into the newly created 2157x1536 folder?

  • by Tony T1,Solvedanswer

    Tony T1 Tony T1 May 14, 2015 5:35 AM in response to benjamin-mills
    Level 6 (9,249 points)
    Mac OS X
    May 14, 2015 5:35 AM in response to benjamin-mills

    benjamin-mills wrote:

     

    Thanks so much Tony!

     

    So last but not least.... Say the folders that were missing the 2157x1536 directories contained a bunch of files, would there be a way to move those files into the newly created 2157x1536 folder?

     

    This can be done by adding another Do Shell Script Action with find and -execdir

    We also need to use the Set Value of Variable and Get Value of Variable Actions.

    Note: This will move all files in the Folders below the selected directory (20150504, 20150505, etc), except .DS_Store to the 2157x1536 Folder:

     

    Screen Shot 2015-05-14 at 8.31.30 AM.pngScreen Shot 2015-05-14 at 8.31.50 AM.png

     

     

    The new Run Shell Script Action is:

     

         find $1  -type f -depth 2 \! -iname '.DS_Store' -execdir mv "{}" "2157x1536" \;

  • by Tony T1,

    Tony T1 Tony T1 May 14, 2015 6:06 AM in response to Tony T1
    Level 6 (9,249 points)
    Mac OS X
    May 14, 2015 6:06 AM in response to Tony T1

    May be better to exclude all hidden files with: \! -iname '.*' (instead of \! -iname '.DS_Store')