Friday, February 24, 2012

Make Directory if it not exist

The macro below will make a folder named TestFolder on your desktop if it not exist.

Sub MakeFolderIfNotExist()
    Dim FolderString As String
    Dim ScriptToMakeDir As String

    'Make folder named TestFolder on the Desktop
    FolderString = MacScript("return (path to desktop folder) as string") & "TestFolder:"
   
    'Or enter the complete path
    'FolderString = "Macintosh HD:Users:Ron:Desktop:TestFolder:"

    ScriptToMakeDir = "tell application " & Chr(34) & _
                      "Finder" & Chr(34) & Chr(13)
    ScriptToMakeDir = ScriptToMakeDir & _
                      "do shell script ""mkdir -p "" & quoted form of posix path of " & _
                      Chr(34) & FolderString & Chr(34) & Chr(13)
    ScriptToMakeDir = ScriptToMakeDir & "end tell"

    On Error Resume Next
    MacScript (ScriptToMakeDir)
    On Error GoTo 0
End Sub

No comments:

Post a Comment