NAORC
asked on
FileSystemObject - Creating Folders
Hi peoples,
Any chance of a sample code?
I have a string variable which contains either "C:\program\file.txt", "C:\program\subfolder\file .txt", "C:\program\subfolder\subf older2\fil e.txt" and so on...
The folder C:\program\ already exists, but right now it is empty.
I need code that will look at the string variable, take of the "file.txt" from the end of the string, check to see if the folder structure exists, and if it doesnt, create it.
I also need the fully intact string at the end so i can use it in order to copy a file from a location to that one.
HELP!!!
Any chance of a sample code?
I have a string variable which contains either "C:\program\file.txt", "C:\program\subfolder\file
The folder C:\program\ already exists, but right now it is empty.
I need code that will look at the string variable, take of the "file.txt" from the end of the string, check to see if the folder structure exists, and if it doesnt, create it.
I also need the fully intact string at the end so i can use it in order to copy a file from a location to that one.
HELP!!!
ASKER
VB6.
does this affect the code?
does this affect the code?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
what needs to be done to modify the code to work for me?
The above is tested, working VB6 code....
Try it out. =)
Try it out. =)
ASKER
Have done... it is very reliant on accurate data being fed into it, but i managed to tweak it into working :)
thanks
thanks
ASKER
Just for reference of others, this code is an optimised and much more efficient version of the code above :)
Private Function createPath(ByVal path As String)
Dim j&, currentParentFolder$
j& = 0
Do
'Find the next slash, after the last one found.
j& = InStr(j& + 1, path, "\")
If j& > 0 Then 'found another slash...
currentParentFolder$ = Left$(path, j&)
If (m_fso.FolderExists(curren tParentFol der$) = False) Then
m_fso.CreateFolder currentParentFolder$
End If
End If
Loop Until j& = 0
End Function
Private Function createPath(ByVal path As String)
Dim j&, currentParentFolder$
j& = 0
Do
'Find the next slash, after the last one found.
j& = InStr(j& + 1, path, "\")
If j& > 0 Then 'found another slash...
currentParentFolder$ = Left$(path, j&)
If (m_fso.FolderExists(curren
m_fso.CreateFolder currentParentFolder$
End If
End If
Loop Until j& = 0
End Function
Dim fso
Set fso = CreateObject("Scripting.Fi
Dim fullPath
fullPath = "C:\program\subfolder\subf
createPath (Left(fullPath, InStrRev(fullPath, "\")))
function createPath(path)
folders = Split (path, "\")
for i = lbound(folders) to ubound(folders)
if folders(i) <> "" then
p = p & folders(i) & "\"
if (not fso.FolderExists (p)) then
fso.CreateFolder (p)
end if
end if
next
end function