Make directory path: Difference between revisions
Content added Content deleted
(→{{header|AppleScript}}: Added an AppleScript version using NSFileManager) |
|||
Line 12: | Line 12: | ||
It's likely that your language implements such a function as part of its standard library. If so, please also show how such a function would be implemented. |
It's likely that your language implements such a function as part of its standard library. If so, please also show how such a function would be implemented. |
||
<br><br> |
<br><br> |
||
=={{header|AppleScript}}== |
|||
AppleScript is not a cross-platform language so this is a macOS-only solution. |
|||
In post-Yosemite AppleScript we can draw on the macOS Foundation classes, |
|||
which include the NSFileManager method: |
|||
`createDirectoryAtPath:withIntermediateDirectories:attributes:error:` |
|||
<lang AppleScript>use framework "Foundation" |
|||
use scripting additions |
|||
-- createOrFindDirectoryMay :: Bool -> FilePath -> IO () |
|||
on createOrFindDirectoryMay(fp) |
|||
createDirectoryIfMissingMay(true, fp) |
|||
end createOrFindDirectoryMay |
|||
-- createDirectoryIfMissingMay :: Bool -> FilePath -> Maybe IO () |
|||
on createDirectoryIfMissingMay(blnParents, fp) |
|||
if doesPathExist(fp) then |
|||
nothing("Directory already exists: " & fp) |
|||
else |
|||
set e to reference |
|||
set ca to current application |
|||
set oPath to (ca's NSString's stringWithString:(fp))'s ¬ |
|||
stringByStandardizingPath |
|||
set {bool, nse} to ca's NSFileManager's ¬ |
|||
defaultManager's createDirectoryAtPath:(oPath) ¬ |
|||
withIntermediateDirectories:(blnParents) ¬ |
|||
attributes:(missing value) |error|:(e) |
|||
if bool then |
|||
just(fp) |
|||
else |
|||
nothing((localizedDescription of nse) as string) |
|||
end if |
|||
end if |
|||
end createDirectoryIfMissingMay |
|||
-- TEST ---------------------------------------------------------------------- |
|||
on run |
|||
createOrFindDirectoryMay("~/Desktop/Notes/today") |
|||
end run |
|||
-- GENERIC FUNCTIONS --------------------------------------------------------- |
|||
-- doesPathExist :: FilePath -> IO Bool |
|||
on doesPathExist(strPath) |
|||
set ca to current application |
|||
ca's NSFileManager's defaultManager's ¬ |
|||
fileExistsAtPath:((ca's NSString's ¬ |
|||
stringWithString:strPath)'s ¬ |
|||
stringByStandardizingPath) |
|||
end doesPathExist |
|||
-- just :: a -> Just a |
|||
on just(x) |
|||
{nothing:false, just:x} |
|||
end just |
|||
-- nothing :: () -> Nothing |
|||
on nothing(msg) |
|||
{nothing:true, msg:msg} |
|||
end nothing</lang> |
|||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
Line 38: | Line 103: | ||
'\TEMP\A\B C' created |
'\TEMP\A\B C' created |
||
</pre> |
</pre> |
||
=={{header|C}}== |
=={{header|C}}== |
||
<lang c>#include <stdio.h> |
<lang c>#include <stdio.h> |