Solved

Mac Automator / Applescript to move files

Posted on 2010-09-05
15
986 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
Comment Utility
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
Comment Utility
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:SimonAdept
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
>>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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 12

Expert Comment

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

Expert Comment

by:alexrs
Comment Utility
(*

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
Comment Utility
(*

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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks Alexrs and NXNW. i tried both the solutions again and they worked!!! Thanks much.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Syslogd is a utility that traps and logs messages sent by running processes. It is configured with the syslog.conf file, which consists of lines containing a pair of fields: "the selector field which specifies the types of messages and priorities to…
CocoaPods is the best way to manage library dependencies in iOS and OS X projects. By using cocoa pods there is no need of downloading the code from github and copying to your project. There are plenty of open source libraries now available with C…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

18 Experts available now in Live!

Get 1:1 Help Now