Solved

Mac Automator / Applescript to move files

Posted on 2010-09-05
15
1,018 Views
Last Modified: 2012-05-10
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,
0
Comment
Question by:forumware
  • 5
  • 4
  • 3
  • +2
15 Comments
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33607549
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
0
 

Author Comment

by:forumware
ID: 33607926
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
0
 
LVL 18

Expert Comment

by:Simon
ID: 33607981
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?
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

Author Comment

by:forumware
ID: 33607989
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
0
 
LVL 12

Assisted Solution

by:nxnw
nxnw earned 200 total points
ID: 33608656
I can't believe I know this, but the following command will move every file ending with the letters "pdf" from anywhere in the directory /Documents/books/ to a directory called "newbooks" on the desktop.

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

note: I am only guessing that the "books" folder is in the user's documents folder. the path must be corrected, if not.

If the only files in ~/Documents/books/ are the pdfs, then all of the folders will be empty and easily deleted.
0
 

Author Comment

by:forumware
ID: 33608702
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
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33608768
>>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.

0
 
LVL 12

Expert Comment

by:nxnw
ID: 33608774
Perhaps you can explain whether there will be anything other than empty folders in /books/.
0
 

Expert Comment

by:alexrs
ID: 33621894
(*

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
0
 

Accepted Solution

by:
alexrs earned 300 total points
ID: 33631784
(*

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
0
 

Author Comment

by:forumware
ID: 33632873
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,
0
 
LVL 12

Expert Comment

by:nxnw
ID: 33633236
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.
0
 

Expert Comment

by:alexrs
ID: 33636994
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

0
 

Expert Comment

by:alexrs
ID: 33637227
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 ***


0
 

Author Comment

by:forumware
ID: 33665245
Thanks Alexrs and NXNW. i tried both the solutions again and they worked!!! Thanks much.
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you other experts are anything like me you are always looking into and testing out new features. While I was doing some research one day I ran across an app that I installed on my Mac and used as a security system. Mac OS X: SecureHome uses your …
This article describes in detail how to set up the iPad in the Enterprise using iPCU aka iPhone Configuration Utility.  This could also be used for the iPhone although I have not detailed out any differences. Preparation as an iPad Administrator:…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question