Solved

Mac Automator / Applescript to move files

Posted on 2010-09-05
15
997 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
 

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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MAC Laptop - No Boot Loader 5 94
Auto-rotate google map 1 58
Content Filtering by Search Term with a Smoothwall Firewall 1 87
mac maintenance 6 32
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:…
Create a default user profile for Mac OS X 10.7/10.8 Create a user account on OS X that will be a template for every other user of that computer. I usually call it “profile” and make it an administrator account for the time being. 1. Install a…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

914 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now