Help, trying to make directorys

homer2002

Registered User.
Local time
Today, 12:28
Joined
Aug 27, 2002
Messages
152
Hi. I am having a few problems making a directory.

I have a string (the name of a directory to be created

say for example this string is

C:\TEMP2000\This is a Directory\

I would like to be able to create this directory from scratch.

i.e "TEMP2000" & "This is a Directory"

do not exist already.

HELP
 
Thats how i have been trying to do this.


The problem I'm getting is that when I try to make
a bunch of directorys in one go, if these directory names have spaces, everything goes wrong.


I tried this, but it didnt work.
___________________________________
strShell = "C:\WINNT\SYSTEM32\COMMAND.COM /C MD " & chr(34)C:\\TEMP2000\THIS IS A NEW FOLDER" & chr(34)

shell strShell

___________________________________

(the chr(34)'s are quotes around the filename)
I tried this with and without quotes
 
Hey Homer,

when dealing with spaces (but you should avoid naming files and folders with spaces when possible!), you need to surround the name in quotations.

strShell = "C:\WINNT\SYSTEM32\COMMAND.COM /C MD " & chr(34)C:\\TEMP2000\""THIS IS A NEW FOLDER""" & chr(34)

Hope that helps!

-Sean
 
Why the double backslash after the drive letter?

Not that I've ever tried this, but your syntax looks a bit off. I am assuming you've already tried:
strShell = "C:\WINNT\SYSTEM32\COMMAND.COM /C MD C:\TEMP2000\THIS IS A NEW FOLDER"
and that it didn't work, presumably because of the spaces in the directory name.

Can you try:
strShell = "C:\WINNT\SYSTEM32\COMMAND.COM /C MD 'C:\TEMP2000\THIS IS A NEW FOLDER'"
 
Ok, I have tried both of the suggestions above, thanks.

I cannot put Double quotes around any directorys with spaces. All I have is a string with the directory path, so i'd have to filter these out.... which is exactly what I am trying to avoid :-)

If i use a string of

"C:\TEMP2000\A NEW DIRECTORY" <-----with the double quotes

I get a shell error message saying
_____________________________________
Parameter format not correct - "C:\TEMP2000\A
_____________________________________


but if I use single quotes instead i get this error message
________________________________
Too Many Parameters - NEW DIRECTORY'
________________________________


Reckon its about time to buy a shotgun
 
Ok I managed to write myself a little function

it's not perfect by any means but it works for most things
(not UCC paths)

It just cycles through the string passed in and creates each directory one by one.
_________________________________________
Private Sub CreateDirectory(DirectoryPath As String)
On Error GoTo Handle
Dim PathPart As String
Dim i As Integer

If Right(DirectoryPath, 1) <> "\" Then DirectoryPath = DirectoryPath & "\"

For i = 1 To Len(DirectoryPath)
If Mid(DirectoryPath, i, 1) = "\" Then
PathPart = Left(DirectoryPath, i - 1)
MkDir PathPart
End If
Next i
Handle:
Resume Next
'This handle just knocks out the first error message you would get
'When trying to execute
'MkDir C:
End Sub
_________________________________________
 
How about this:
"C:\WINNT\SYSTEM32\COMMAND.COM /C MD " & Chr(34) & "C:\TEMP2000\THIS IS A NEW FOLDER" & Chr(34)
 
Homer, you need 2 sets of double quotes (to make one in a string).

old: "C:\TEMP2000\A NEW DIRECTORY"
new: "C:\TEMP2000\""A NEW DIRECTORY"""

If you put two double-quotes then the compiler will treat it as text (note that at the end I've put two sets of double quotes followed by another double quote to end your string).

Without the quotes, it thinks that anything after the space is a command or manipulator.

-Sean
 
homer2002 said:
it's not perfect by any means but it works for most things (not UCC paths)
Thanks homer2002 for your code works and yes it does work [for me] for UNC paths to create a path that does not exist. I just made a small change and thanks again for posting your solution!

Code:
Private Sub Test()
    CreateDirectory ("\\Server\Partition\Directory\Sub1\Sub2")
End Sub
    
Public Sub CreateDirectory(sPath As String)
On Error GoTo DirectoryError
    
    Dim sPathPart As String
    Dim i As Integer
    
    If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
    
    For i = 1 To Len(sPath)
        If Mid(sPath, i, 1) = "\" Then
            sPathPart = Left(sPath, i - 1)
            MkDir sPathPart
        End If
    Next i
    
DirectoryError:
    Resume Next
    
End Sub
 

Users who are viewing this thread

Back
Top Bottom