Become a MacRumors Supporter for $25/year with no ads, private forums, and more!

Mac Moving PDFs to Corresponding Folders

TBast

macrumors newbie
Original poster
Jul 23, 2019
5
1
United Kingdom
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
 

superscape

macrumors 6502a
Feb 12, 2008
930
218
East Riding of Yorkshire, UK
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?
 
Comment

TBast

macrumors newbie
Original poster
Jul 23, 2019
5
1
United Kingdom
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.
 
Comment

superscape

macrumors 6502a
Feb 12, 2008
930
218
East Riding of Yorkshire, UK
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.


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!
 
Comment

superscape

macrumors 6502a
Feb 12, 2008
930
218
East Riding of Yorkshire, UK
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.
 
Comment
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.