2017-08-17 17 views
0

我已經完成了這個腳本,根據它們的前6個字符串將文件組織到子文件夾中。例如,將位於名爲"bjaaahd"的子文件夾中。 此腳本已在虛擬文件上進行了測試,以檢查其可行性。但現在,試圖測試真正的任務。它不按預期工作。 這些文件以這種形式出現"_londondire_123455-901-34"此代碼的分隔符添加/修改

我希望「_londondire」部分僅作爲文件夾並按該名稱進行組織。換句話說,只有沒有數字的名字。 我讀過關於分隔符的內容,但對於上帝的愛,我不明白。

--script begins 

on run 
    set theFolder to "" 
    set fileList to {} 
    set theTarget to "" 
    set theString to "" 

    tell application "Finder" 
     set theFolder to (choose folder with prompt "Choose containing folder:" without invisibles and multiple selections allowed) 
     set fileList to every file of theFolder 

     repeat with i in fileList 
      name of i as text 
      set theString to text 1 thru 6 of result 
      try 
       set theTarget to ((theFolder as text) & theString) as alias 
       move i to theTarget 
      on error 
       make new folder at theFolder with properties {name:theString} 
       move i to result 
      end try 
     end repeat 
    end tell 
end run 

--script ends 

我該如何做到這一點?

回答

0

text item delimiters是做到這一點的最佳方式。

有了這個代碼

set theText to "_londondire_123455-901-34" 
set {TID, text item delimiters} to {text item delimiters, "_"} 
set textItems to text items of theText 
set text item delimiters to TID 

結果的變量textItems是一個列表{"", "londondire", "123455-901-34"},因此所需的值是第二個文本項目

set theResult to item 2 of textItems -- "londondire" 

如果需要下劃線領導寫

set theResult to "_" & item 2 of textItems -- "_londondire" 
+0

萬一有多個相同樣式的文件,例如e,_londondire,_Parisdire等? –

+0

該代碼通過下劃線字符分割任何字符串。下劃線的數量很重要,而不是整個文本文本 – vadian

+0

太棒了非常感謝你!乾杯! –