Mkdir on network location causes a debug error

spydey

Active Member
Joined
Sep 19, 2017
Messages
314
Office Version
  1. 2013
Platform
  1. Windows
I have two snippets of code that work just fine locally but seem to have issues when run over the network.

When I run the code from within my LAN, it captures a directory location (Path), checks to see if that location exists, and if it doesn't, then make the correct directory tree. The location is on a network drive.
I have simplified the code below.

Code:
Sub start()

Dim Path As String


Path = "R:\Location\Of\The\Folder\Where\The\File\Needs\To\Be\Saved\"
Call MakeDirectory(Path)


End Sub

Sub MakeDirectory(FolderPath As String)

Dim x, i As Integer, strPath As String
x = Split(FolderPath, "\")


For i = 0 To UBound(x) - 1
    strPath = strPath & x(i) & "\"
    If Not FolderExists(strPath) Then MkDir strPath
Next i


End Sub


Function FolderExists(FolderPath As String) As Boolean
On Error Resume Next


ChDir FolderPath
If Err Then FolderExists = False Else FolderExists = True


End Function


However, if I remote into my network via Citrix, the "R:" no longer exists under that drive letter. It now follows the Universal Naming convention for networked drives.

So I changed the code to make it work for when I remote in. The only code that changed is the Path name.

Code:
Sub start()

Dim Path As String


Path = "\\Network\Drive\Name\Location\Of\The\Folder\Where\The\File\Needs\To\Be\Saved\"
Call MakeDirectory(Path)


End Sub

Sub MakeDirectory(FolderPath As String)

Dim x, i As Integer, strPath As String
x = Split(FolderPath, "\")


For i = 0 To UBound(x) - 1
    strPath = strPath & x(i) & "\"
    If Not FolderExists(strPath) Then MkDir strPath
Next i


End Sub


Function FolderExists(FolderPath As String) As Boolean
On Error Resume Next


ChDir FolderPath
If Err Then FolderExists = False Else FolderExists = True


End Function

If the folder path already exists, no issues.

The problem comes specifically from the "MkDir strPath" code when using the Universal Naming convention. It fails and debugs. It can't make the directory using the \\Network\Drive\Name\ ....." for some reason.

I can't determine if it is a permissions thing when accessing the drive remotely, if I coded it incorrectly, etc.

Any ideas on what needs to change to make it work? Are there any alternatives or different ways to have a directory created other than MkDir??

Your guidance is very much appreciated!!

-Spydey
 
Last edited:

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Doing a bit more research, it looks like MkDir cannot handle UNC.

I think that my solution most likely lies with calling
MakeMultiStepDirectory ......

Found some great info on Chip's page (RIP).

http://www.cpearson.com/excel/MakeDirMulti.aspx

Pros and cons of using this to solve my issue?

-Spydey
 
Last edited:
Upvote 0
So I got it working!!!

I used Chip's example from this page, adjusted accordingly, updated some other lines of code, and got it working!! Works great!

Now, whether I be local or remote, I don't have to worry about.

-Spydey
 
Upvote 0
Doing a bit more research, it looks like MkDir cannot handle UNC.

I think that my solution most likely lies with calling
MakeMultiStepDirectory ......

Found some great info on Chip's page (RIP).

Create Multiple Directories

Pros and cons of using this to solve my issue?

-Spydey
Saw Chip's page.. (don't know why you call it Chip's page tbh)
Was going through same problem as yours.. Will come back here if i get what i am looking for.
 
Upvote 0

Forum statistics

Threads
1,213,510
Messages
6,114,040
Members
448,543
Latest member
MartinLarkin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top