Solved

Mac Automator / Applescript to move files

Posted on 2010-09-05
15
1,067 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Apple's Mac OS X has become an official member of the malware club. The Flashback Trojan has affected over half million Macs, worldwide. It is behavior that ultimately gets malware onto a person’s computer. Obsolete or out-of-date software helps…
A common question or need, when setting-up a new Mac for someone would be to make all of the applications, installed, available from the dock. Many people often do not realize an application is installed unless it is in the dock. Creating a custo…
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 …
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

726 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