문제

I have a folder which contains the following files:

Elephant.19864.archive.other.pdf
Elephant.17334.other.something.pdf
Turnip.19864.something.knight.pdf
Camera.22378.nothing.elf.pdf

I want these files moved to the following structure

Archive
    Elephant
        Elephant.19864.pdf
        Elephant.17334.pdf
    Turnip
        Turnip.19864.pdf
    Camera.HighRes
        Camera.HighRes.22378.pdf

The generated files consist of a word or multiple words, followed by a sequence of number, followed by other words and then the extension. I want to move these into a folder named the word or words before the numbers, and remove all of the words between the numbers and the extension (.pdf in this case).

If the folder does not exist, then I have to create it.

I thought this would be quite simple using Automator or an AppleScript, but I don't seem to be able to get my head around it.

Is this easy using Automator/AppleScript if so, what should I be looking at

도움이 되었습니까?

해결책

It's easy, it's just not obvious at first. Some things to get you started.

To parse the file names to get your folder names, you need to separate the name into a list...

set AppleScript's text item delimiters to {"."}
set fileNameComponents to (every text item in fileName) as list
set AppleScript's text item delimiters to oldDelims
--> returns: {"Elephant", "19864", "archive", "other", "pdf"}

The list has a 1-based index, so item 1 is "Elephant" and item 5 is "pdf". To mash the file name together, then all you need is this

set theFileName to (item 1 of fileNameComponents & item 2 of fileNameComponents & item 5 of fileNameComponents) as string

To create a folder, just use the following...

tell application "Finder"
    set theNewFolder to make new folder at (theTargetFolder as alias) with properties {name:newFolderName, owner privileges:read write, group privileges:read write, everyones privileges:read write}
end tell

To move a file, all you need is this...

tell application "Finder"
    set fileMoved to move theTargetFile to theTargetFolder
end tell

To rename a file, use something like the following...

set theFileToRename to theTargetFilePath as alias -- alias is important here
set name of theFileToRename to theFileName

I suggest first creating a list of all of the target files, then for each file in the list create the folders based on its name, move the file, finally renaming it once it is in its final location.

Add salt to taste.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top