Link to home
Start Free TrialLog in
Avatar of forumware
forumware

asked on

Mac Automator / Applescript to move files

I want to move all the pdf files into a folder and move the original folder into trash.

So i have folders like

A1
B1
C1

which contain

A1.pdf
B1.pdf
C1.pdf

i want to move the pdfs to a folder on the desktop and delete folders A1, B1, and C1.

Thanks,
Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

So?
Launch Terminal. Let's think that all three folders are on your desktop, so:

$ cd Desktop
$ mkdir B1
$ mv -f A1/*.pdf B1

cd Desktop - go to Desktop folder.
mkdir B1 - create new folder B1. You don't need this line, if the folder exists.
mv- f A1/*.pdf B1 - move all pdf-files from A1 to B1

If you need the same in Automator, simply launch it, select "Workflow" or "Application" template and press "Record". Now it will record you actions: launch Finder, find the folder A1, select all files you need, copy them to the target folder, move to the trash in the original folder.

Terminal---bash---bash---ttys000.jpg
Untitled.jpg
Avatar of forumware
forumware

ASKER

Hi thanks I'll try you suggestion but one more detail I forgot to add was the folder names are not static they change so The solution cannot specifically target a1 b1 etc
Avatar of Simon
Are the folders A1, B1, C1 etc all within a common root folder?

Is this something you want to trigger after selecting a folder, e.g recursively scan folders inside a common root folder, or a folder action that you want to run each time items are added to a particular folder?
Yes they have a common root folder called books\a1, books\b1 etc so I want to select the root "books" and have it scan recursively to get the PDFs and move them to the desktop and Then delete the folders where the PDF was stored
SOLUTION
Avatar of nxnw
nxnw
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks but I'm afraid all the solutions so far only address finding the PDF and moving them to a common folder but this is easy and ihave the Automator script to do this alredy BUT I want the folders containing the PDF to be deleted after the move which I have no solution for thus far
>>Automator script
I think you mean AppleScript.

>>BUT I want the folders containing the PDF to be deleted after the move
So the folder will be empty? What to do with the files that left there.

Add the delete operation to the script.

Perhaps you can explain whether there will be anything other than empty folders in /books/.
(*

This script below will:
1. Ask for a folder name to move the PDF files TO
2. Ask for the folder where the subfolders (A1, B1,...) with the pdf files are
3. Move the files (A1.pdf, B1.pdf,...) from each folder to the new folder informed in step 1
4. Delete the original pdf folders (A1, B1,...) after the pdf files have been moved

*)

set toFolder to choose folder with prompt "Choose folder to move PDF files TO:"
tell application "Finder"
      set fromFolder to choose folder with prompt "Choose folder to search for PDF files:"
      set pdfFolders to folders in the folder fromFolder
      repeat with pdfFolder in pdfFolders
            set pdfFolderAlias to pdfFolder as alias
            set pdfFiles to (every file in pdfFolderAlias whose name ends with "pdf") as alias list
            repeat with i from 1 to number of items in pdfFiles
                  set pdfFile to (item i of pdfFiles)
                  move pdfFile to folder toFolder
            end repeat
            delete folder pdfFolderAlias
      end repeat
end tell
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi i tried the above script Alexrs, it successfully asked me for the source and destination but did not move anything or delete any of the folders. But the script in theory is what i am looking for.

Thanks,
or you can use the one line command I suggested above.

The command can run automatically, by the way, by setting it up in a user daemon with lingon.

Also, I don't want to put you out, but if you don't mind answering whether there is anything in the "books" folder, besides pdfs and other file folders, it will help someone suggest a good solution to the delete part.
I just did this test, it may help you to find out what I'm dong differently:

1. Open terminal and run these lines:

mkdir ~/Desktop/PDF ~/Documents/PDF
mkdir ~/Documents/PDF/A1 ~/Documents/PDF/B1 ~/Documents/PDF/C1
touch ~/Documents/PDF/A1/A1.pdf
touch ~/Documents/PDF/B1/B1.pdf
touch ~/Documents/PDF/C1/C1.pdf

Run the script, select the first folder (move TO:) as Desktop/PDF, select the second folder (move FROM:) as Documents/PDF

I'm using Mac OS X 10.6.4 and AppleScript 2.1.2

Good luck

BTW, nxnw sugegstion also works to move the files, just teste it. However it doesn't remove the folders as you requested. If you want to take his/her approach, which I personally find more elegant, you can test it following these steps:

1. Create the "test environment"

mkdir ~/Desktop/PDF ~/Documents/PDF
mkdir ~/Documents/PDF/A1 ~/Documents/PDF/B1 ~/Documents/PDF/C1
touch ~/Documents/PDF/A1/A1.pdf
touch ~/Documents/PDF/B1/B1.pdf
touch ~/Documents/PDF/C1/C1.pdf

2. Move the PDF files

find ~/Documents/PDF/ -type f -iname "*pdf" | xargs -I {} mv {} ~/Desktop/PDF/

3. Remove the PDF origin folders (assuming they now are empty), using the find command:

find ~/Documents/PDF  -type d -empty -exec rmdir {} \;

*** Remember, be careful when using UNIX shell commands, especially those with deleting functions like rmdir in this case ***


Thanks Alexrs and NXNW. i tried both the solutions again and they worked!!! Thanks much.