将文件列表复制到新文件夹

时间:2016-07-27 23:32:37

标签: csv applescript

我有一个图像文件名列表(csv文件),我想检查这些图像是否存在于图像文件夹中。如果图像存在,我想将其复制到新文件夹。

我构建了一个没有错误的示例代码,并将第一个匹配的文件正确复制到目标文件夹。然而,它返回的其余文件虽然它们明显存在于源文件夹中,但它找不到匹配项。出了什么问题?

--Step 1.  Get file path and read file.
set csvFile to (choose file with prompt "Select CSV file...") as text
set csvRecords to read file csvFile using delimiter {return}
set sourceFolder to (path to documents folder as text) & "AllProducts:"
set destinationFolder to (path to documents folder as text) & "SelectedProducts:"

--Step 2.  Process each record (data row) by first extracting the record data as text items.
repeat with thisRecord in csvRecords
    set recordItems to my getItems(thisRecord)

    -- Enter processing code here
    set filePath to sourceFolder & thisRecord
    log filePath

    tell application "Finder"
        if (exists file filePath) then
            duplicate file filePath to folder destinationFolder
        else
            log "File doesn't exist" & thisRecord
        end if
    end tell


end repeat

--Subroutine that returns the text items from each record.
on getItems(theRecord)
    copy the text item delimiters to origDelims
    set the text item delimiters to ","
    set recordItems to {}
    set recordItems to every text item of theRecord
    set the text item delimiters to origDelims
    return recordItems
end getItems

0 个答案:

没有答案