Back to forum list… Back to How Do I?...

Create destination folder on network if it does not exist (7 replies and 2 comments)

Arline Welty
5 years ago
Arline Welty 5 years ago

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:

  • How would I  modify this to include a sub-folder for calendar year?
  • How do I deploy this function? Do I put it in a Map transform? Or is this a Script task?

"

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

 

"

 

Support
5 years ago
Support 5 years ago

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)

Arline Welty
5 years ago
Arline Welty 5 years ago

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

 

Arline Welty
5 years ago
Arline Welty 5 years ago

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

Arline Welty
5 years ago
Arline Welty 5 years ago

Any feedback on this?

Support
5 years ago
Support 5 years ago

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 & "\"
Arline Welty
5 years ago
Arline Welty 5 years ago

I don't think that's it.  When I made that change I can't even refresh the transform without getting this error:

Error whilst evaluating field [CreateFolderFunction] on transaction type [Record]. Resolved Function - CreateFolderFunction Error - Description: Invalid procedure call or argument . ^ Line=12 Pos=44

 

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

Arline Welty
5 years ago

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

Arline Welty
5 years ago

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.

Support
5 years ago
Support 5 years ago

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

Contact

Realisable Software Ltd provides code-free, cost-effective applications integration solutions for SMEs. Our core IMan product is designed to integrate almost any application with a number of Sage solutions and online payment processors.

Looking to purchase IMan, please see our resellers here.

Realisable Software
Ph: +44 (0) 208 123 1017

Copyright © Realisable. All rights reserved.
Realisable is a registered trademark

Close

Request Demo

Realisable Software Ltd provides code-free, cost-effective applications integration solutions for SMEs. Our core IMan product is designed to integrate almost any application with a number of Sage solutions and online payment processors.

Looking to purchase IMan, please see our resellers here.

Realisable Software
Ph: +44 (0) 208 123 1017

Copyright © Realisable. All rights reserved.
Realisable is a registered trademark

Close

Access Downloads

Realisable Software Ltd provides code-free, cost-effective applications integration solutions for SMEs. Our core IMan product is designed to integrate almost any application with a number of Sage solutions and online payment processors.

Looking to purchase IMan, please see our resellers here.

Realisable Software
Ph: +44 (0) 208 123 1017

Copyright © Realisable. All rights reserved.
Realisable is a registered trademark

Close