【发布时间】:2019-01-01 06:07:13
【问题描述】:
假定的重复问题解释了如何删除文件,但我需要创建一个(或更多)不存在的目录完全不同的任务!
作为我之前的(已解决的)问题Can Applescript be used to tell whether or not a directory (path) exists?的跟进
我现在需要知道如何在路径中创建任何不存在的目录?
【问题讨论】:
标签: applescript
假定的重复问题解释了如何删除文件,但我需要创建一个(或更多)不存在的目录完全不同的任务!
作为我之前的(已解决的)问题Can Applescript be used to tell whether or not a directory (path) exists?的跟进
我现在需要知道如何在路径中创建任何不存在的目录?
【问题讨论】:
标签: applescript
最简单的方法是使用shell,mkdir -p 仅在文件夹不存在时创建。
do shell script "mkdir -p ~/Desktop/TestFolder"
但是有一个警告:如果路径中有空格字符,则需要用两个反斜杠替换每个空格,因为通常的 quoted of 不会扩展波浪号。
do shell script "mkdir -p ~/Desktop/Test\\ Folder"
或者
set thePath to "~/Desktop/Test Folder ABC"
if thePath starts with "~" then
set quotedPath to text 1 thru 2 of thePath & quoted form of (text 3 thru -1 of thePath)
else
set quotedPath to quoted form of thePath
end if
do shell script "mkdir -p " & quotedPath
【讨论】:
在获取文件对象的路径前添加POSIX file:
tell application "Finder"
set f to POSIX file "/Users/username/Documents/new.mp3"
if exists f then delete f
end tell
system attribute "HOME" 替换为/Users/username:
set f to POSIX file ((system attribute "HOME") & "/Documents/new.mp3")
tell application "Finder" to if exists f then delete f
或者使用 pre-OS X 路径格式:
tell application "Finder"
set f to "Macintosh HD:Users:username:Documents:new.mp3"
-- set f to (path to documents folder as text) & "new.mp3"
if exists f then delete f
end tell
【讨论】:
如果您的问题仍然存在:
“创建一个(或更多)不存在的目录是一项完全不同的任务?”
为了管理我的文件夹,我在相关案例中使用了这些行:
创建从“a”到“e”的所有文件夹。如果文件夹“a”已经存在,则从“b”到“e”。 等等……
set mkdirFolder to "mkdir -p " & desktopPath & "a/b/c/d/e/"
do shell script mkdirFolder
如果不存在则创建一个文件夹“a”,并在其顶层创建一个文件夹“b to e”
set mkdirFolder to "mkdir -p " & desktopPath & "a/{b,c,d,e}/"
do shell script mkdirFolder
使用部分名称创建文件夹
-- (Note the single quotes round the space to mark it as part of the name.)
set mkdirFolder to "mkdir -p " & desktopPath & "a/Chapter' '{1,2,3,4}/"
do shell script mkdirFolder
result--> Folders "Chapter 1", "Chapter 2", "Chapter 3", and "Chapter 4" are created in folder "a"
您可以在此处找到更多信息 (Learn more about creating folder with "mkdir")
【讨论】: