Last Updated: February 25, 2016
·
770
· jjkilpatrick

Shell script to read text file, find each item in text file and move items a new folder

Needed this when trying to find missing images on a website. I used an xml export to list all the images used on the site, put this into a text file, searched the media folder and moved what I needed to a new folder. Very helpful as there were over 1000 images. Bit of a strange thing to post but I'd hate to loose this code or rewrite it. Much more easier to write this than try and build a automator process.

set newFolder to POSIX path of (path to desktop as text) & "Found Photos"
do shell script "mkdir -p " & quoted form of newFolder
set filePaths to paragraphs of (read (choose file with prompt "Select file list") as «class utf8»)
set fileFolder to POSIX path of (choose folder with prompt "Select folder containing files")

set foundFiles to {}
repeat with fileName in filePaths
    set fileName to (contents of fileName)
    set xxx to do shell script "find " & quoted form of fileFolder & " -name " & quoted form of fileName
    if xxx ≠ "" then
        tell application "System Events" to move file xxx to newFolder
        set end of foundFiles to fileName & return
    end if
end repeat

set foundFiles to (foundFiles as text)
do shell script "echo " & quoted form of foundFiles & " > " & quoted form of POSIX path of ((path to desktop as text) & "FoundFiles.txt")