Create destination folder on network if it does not exist (7 replies and 2 comments)
Arline, copy the function into the Common Functions in Setup.
To create the folder(s), do the following in a Map transform.
Dim Dir
Dir = "\\Network Location\" & %CUSTOMERID & "\" & Year
CreateFolder(Dir)
Thanks for that.
So I am not seeing the folder being created successfully. I think it has to do with something happening in my map transform. Check out the preview, which shows the file path (kind of) but without the "\" elements (screenshot attached).
Here's what I have in the map transform that calls the function:
Dim Dir
Dir = "\\Network Location Name\Accounting\IMAN\OEINV020-InvoicePDF\" & %customer & "\" & %Year
CreateFolder(Dir)
(the %customer and %year elements are in the data already)
Sage 300 throws an error that the file path can't be found.
Just a note that I updated one small thing in the code on Line 10 to add a " = " to get around an error. So the final function right now is:
Function CreateFolder(sFolder)
Dim sPathSegments, sDirectory, sPath, oFs
Set oFs = CreateObject("Scripting.FileSystemObject")
sPathSegments = Split(sFolder, "\")
sPath = ""
For Each sDirectory In sPathSegments
If sPath = "" Then sPath = sPath & "\"
sPath = sPath & sDirectory
If oFs.FolderExists(sPath) = False Then oFs.CreateFolder (sPath)
Next
CreateFolder = sPath
End Function
I'm not sure why my images aren't coming through - here's a link to the image: https://www.dropbox.com/s/e011vmoqokp94dq/CreateFolder.jpg?dl=0
Any feedback on this?
Arline, it's working with the script we provided.
On closer inspection, however the following line is incorrect.
If sPath = "" Then sPath = sPath & "\"
It should be...
If sPath <> "" Then sPath = sPath & "\"
I don't think that's it. When I made that change I can't even refresh the transform without getting this error:
Common Function is currently:
Function CreateFolder(sFolder)
Dim sPathSegments, sDirectory, sPath, oFs
Set oFs = CreateObject("Scripting.FileSystemObject")
sPathSegments = Split(sFolder, "\")
sPath = ""
For Each sDirectory In sPathSegments
If sPath "" Then sPath = sPath & "\"
sPath = sPath & sDirectory
If oFs.FolderExists(sPath) = False Then oFs.CreateFolder(sPath)
Next
CreateFolder = sPath
End Function
I don't understand why when I pasted that function, the line showed what I have in my function, and, when I save it, the function looks different. There is something fishy going on with that line. It should be:
For Each sDirectory In sPathSegments
If sPath "" Then sPath = sPath & "\"
Here is a screen snap showing the common function as it's saved in IMAN right now.
https://www.dropbox.com/s/ymb931wir7vt16b/2020-03-08_8-10-21.jpg?dl=0
Another comment. I've done troubleshooting to confirm that the user running the IMAN service has rights to create folders in the directory I've specified, so I think this means the issue is script-related.
For others, the function didn't handle UNC paths. The below script is ammended script.
Function CreateFolder(sFolder) Dim sPathSegments, sSubDir, sPath, oFs Dim bUNC, l Set oFs = CreateObject("Scripting.FileSystemObject") bUNC = sFolder <> "" And Left(sFolder, 2) = "\\" If bUNC Then sFolder = Mid(sFolder, 3) End If sPathSegments = Split(sFolder, "\") If bUNC Then sPathSegments(0) = "\\" & sPathSegments(0) End If sPath = "" For l = 0 To UBound(sPathSegments) sSubDir = sPathSegments(l) If sPath <> "" Then sPath = sPath & "\" sPath = sPath & sSubDir If Not bUNC Or bUNC And l >= 2 Then If oFs.FolderExists(sPath) = False Then oFs.CreateFolder (sPath) End If Next CreateFolder = sPath End Function
Based on this closed topic: https://www.realisable.co.uk/feature-requests/create-destination-folder-if-it-does-not-exist-on-file-task/
I understand this will be included in an upcoming release- wanted to ask a follow-up question here.
We have an IMAN job that creates invoices against shipments and emails those to customers. It also creates a PDF of the invoice. Right now, it's just creating a bunch of invoices in a single network folder. But our requirement is that the network structure be dynamically created, with the following parameters:
\\Network Location\ %CUSTOMERID\CURRENT CALENDAR YEAR\
You gave me some advice (thank you) that I am trying to use- just need some help understanding how to use this tool. Couple of questions:
"
You can use this common function to create any missing folders.
Function CreateFolder(sFolder)
Dim sPathSegments, sDirectory, sPath, oFs
Set oFs = CreateObject("Scripting.FileSystemObject")
sPathSegments = Split(sFolder, "\")
sPath = ""
For Each sDirectory In sPathSegments
If sPath "" Then sPath = sPath & "\"
sPath = sPath & sDirectory
If oFs.FolderExists(sPath) = False Then oFs.CreateFolder (sPath)
Next
CreateFolder = sPath
End Function
"