Moving PDFs to Corresponding Folders

Discussion in 'Mac Programming' started by TBast, Jul 23, 2019.

  1. TBast macrumors newbie

    Joined:
    Jul 23, 2019
    Location:
    United Kingdom
    #1
    Hi all

    Just working on a project at the moment which involves creating around 1500 individual PDFs, these then need putting into 1500 corresponding folders
    The PDFs are simply named with incremental numbers, so:

    1.PDF
    2.PDF
    ...
    ...
    1500.PDF

    The Folders have been created through a dirlist script in terminal, they include an incremental number as well as a unique identifier, so for example

    1 - 113596
    2 - 115374
    ...
    ...
    1500 - 11877

    Does anyone know of a way, through terminal, or even with automator, to move the PDFs to the corresponding incremental number of the folders...so '1.PDF' into folder '1 - 113596'
    Just to save me dragging and dropping 1500 times :D

    Any Help would be much appreciated
     
  2. superscape macrumors 6502a

    superscape

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

    I'm slightly confused by the logic for deciding which folder the PDF goes into.

    Am I right in thinking that the second number in the folder name is irrelevant, but we're taking the PDF file name and putting it in the folder which starts with the same number?

    So, 2.PDF would go into folder '2 - 115374', and 7.PDF would go into folder '7 - 000000'.

    Is that right?
     
  3. TBast thread starter macrumors newbie

    Joined:
    Jul 23, 2019
    Location:
    United Kingdom
    #3
    That is exactly right.

    Apologies, I think mentioning the second number of the folder name just made it slightly more confusing. Just wanted to make it clear that the full folder name isn't exactly the same name as the PDF.
     
  4. superscape macrumors 6502a

    superscape

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

    Cool. OK, you could use something like this shell script, I guess:


    Code:
    
    #!/bin/sh
    
    theSourceFolder=$1
    theDestFolder=$2
    
    find "$theSourceFolder" -type f -maxdepth 1  -name "*.pdf" | while read theFile; do
    
        theDigit=$(basename "$theFile" | cut -f1 -d ".")
    
        find "$theDestFolder" -type d  -name "$theDigit - *" | while read theFolder; do
            cp "$theFile" "$theFolder"
        done
    
    done
    

    I guess there are more elegant ways, but this should work.

    Hope that's of some help!

    r.


    ps. As always - test carefully and have a backup!
     
  5. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #5
    I perhaps should have added that you'd run it in Terminal like this:

    sh ~/Desktop/myshellscript.sh ~/Desktop/SOURCE ~/Desktop/DEST

    ...assuming your shell script, source and dest folder are on your desktop.
     
  6. TBast thread starter macrumors newbie

    Joined:
    Jul 23, 2019
    Location:
    United Kingdom
    #6
    Apologies for the slow reply. But the above worked perfectly, thank you so much for your help :)
     
  7. superscape macrumors 6502a

    superscape

    Joined:
    Feb 12, 2008
    Location:
    East Riding of Yorkshire, UK
    #7
    You're welcome. Glad it helped.
     

Share This Page

6 July 23, 2019