如果文件系统在保存文件时不存在,则如何在文件系统上创建文件夹

时间:2017-07-17 19:12:02

标签: xquery marklogic

鉴于以下XQuery代码基于MarkLogic数据库中的数据生成路径并保存文件,如果没有异常,如何创建文件系统文件夹?

for $doc in collection("http://example.com/stuff")
let $folderName := name($doc/Envelope/*[1]) 
let $folderPath := concat("c:\temp\", $folderName, "\")
let $fileName := concat($doc/Envelope/*[1]/*:Code/text(), ".xml")
let $fullPath := concat($folderPath, $fileName)  

(: Create the folder at $folderPath if it does not exist :)

return xdmp:save($fullPath,$doc)

1 个答案:

答案 0 :(得分:4)

AFAIK没有“文件夹存在”功能,令人困惑filesystem-file-exists实际上也适用于文件夹,所以答案是:

for $doc in collection("http://example.com/stuff")
let $folderName := name($doc/Envelope/*[1]) 
let $folderPath := concat("c:\temp\", $folderName, "\")
let $fileName := concat($doc/Envelope/*[1]/*:Code/text(), ".xml")
let $fullPath := concat($folderPath, $fileName)  

let $_ := if(xdmp:filesystem-file-exists($folderPath)) then () else xdmp:filesystem-directory-create($folder)

return xdmp:save($fullPath,$doc)