I posted a script that did this using ADO/Exoledb some time ago here someone asked this week about doing the same thing using WebDAV. In WebDAV to create a folder you use the MKCOL verb eg but if you try and issue this against a URL that already exist then it will throw a 405 error. While dealing with such an error is not that difficult an alternative to doing this is to first do a search against the parent folder to see if that folder exists and then take the appropriate action. A script to do this looks like the following I've posted a downloadable copy here. The script itself looks for a folder called Spamfolder within the inbox if it doesn't find it then it creates it using MKCOL.
server = "Servername"
mailbox = "Mailbox"
NewFLd = "Spamfolder"
strURL = "http://" & server & "/exchange/" & mailbox & "/inbox/"
strQuery = ""
strQuery = strQuery & "SELECT ""http://schemas.microsoft.com/mapi/proptag/x3001001E"""
strQuery = strQuery & " FROM scope('shallow traversal of """
strQuery = strQuery & strURL & """') Where ""DAV:ishidden"" = False AND ""DAV:isfolder"" = True AND "
strQuery = strQuery & """http://schemas.microsoft.com/mapi/proptag/x3001001E"" = '" & NewFLd & "' "
set req = createobject("microsoft.xmlhttp")
req.open "SEARCH", strURL, false
req.setrequestheader "Content-Type", "text/xml"
req.setRequestHeader "Translate","f"
req.send strQuery
If req.status >= 500 Then
ElseIf req.status = 207 Then
set oResponseDoc = req.responseXML
set oNodeList = oResponseDoc.getElementsByTagName("d:x3001001E")
wscript.echo oNodeList.length
if oNodeList.length <> 0 then
wscript.echo "Folder Already Exists"
else
call Createfolder(NewFld,strURL)
end if
Else
End If
Sub Createfolder(fldname,parentfolder)
nfolderURL = parentfolder & "/" & fldname & "/"
req.open "MKCOL", nfolderURL, false
req.setrequestheader "Content-Type", "text/xml"
req.setRequestHeader "Translate","f"
req.send
if req.status = 201 then
Wscript.echo "Folder created sucessfully"
else
wscript.echo req.status
wscript.echo req.statustext
end if
end sub
server = "Servername"
mailbox = "Mailbox"
NewFLd = "Spamfolder"
strURL = "http://" & server & "/exchange/" & mailbox & "/inbox/"
strQuery = "
strQuery = strQuery & "
strQuery = strQuery & " FROM scope('shallow traversal of """
strQuery = strQuery & strURL & """') Where ""DAV:ishidden"" = False AND ""DAV:isfolder"" = True AND "
strQuery = strQuery & """http://schemas.microsoft.com/mapi/proptag/x3001001E"" = '" & NewFLd & "'
set req = createobject("microsoft.xmlhttp")
req.open "SEARCH", strURL, false
req.setrequestheader "Content-Type", "text/xml"
req.setRequestHeader "Translate","f"
req.send strQuery
If req.status >= 500 Then
ElseIf req.status = 207 Then
set oResponseDoc = req.responseXML
set oNodeList = oResponseDoc.getElementsByTagName("d:x3001001E")
wscript.echo oNodeList.length
if oNodeList.length <> 0 then
wscript.echo "Folder Already Exists"
else
call Createfolder(NewFld,strURL)
end if
Else
End If
Sub Createfolder(fldname,parentfolder)
nfolderURL = parentfolder & "/" & fldname & "/"
req.open "MKCOL", nfolderURL, false
req.setrequestheader "Content-Type", "text/xml"
req.setRequestHeader "Translate","f"
req.send
if req.status = 201 then
Wscript.echo "Folder created sucessfully"
else
wscript.echo req.status
wscript.echo req.statustext
end if
end sub