get directory

Super Suarez

Registered User.
Local time
Yesterday, 21:06
Joined
Jul 10, 2013
Messages
36
Hi

I'm trying to check a directory for a folder name which has a certain string in it (RecNum)

However the fso object filesys.FolderExists does not take an argument when looking for part of a folder name on a particular path.

What I basically want to do is look in a directory for a folder with the RecNum variable as part of the folder name. if it exists open it, if not create it.

This is how far i've got:-

targetdir = targetpath & RecNum & ChannelPartner & Enduser

Set filesys = CreateObject("Scripting.FileSystemObject")
If filesys.FolderExists(targetdir) Then GoTo function_end
MkDir targetdir
If filesys.FolderExists(copypath) Then
filesys.CopyFolder copypath, targetdir
End If
MsgBox ("folder created")
function_end:

Shell "Explorer.exe /n,/e," & targetdir, vbNormalFocus
 
Try this..
Code:
targetdir = targetpath & RecNum & ChannelPartner & Enduser 
 MsgBox targetdir
[COLOR=SeaGreen]'Set filesys = CreateObject("Scripting.FileSystemObject")
'If filesys.FolderExists(targetdir) Then GoTo function_end
'MkDir targetdir
'If filesys.FolderExists(copypath) Then
'filesys.CopyFolder copypath, targetdir
'End If
'MsgBox ("folder created")
'function_end:

'Shell "Explorer.exe /n,/e," & targetdir, vbNormalFocus[/COLOR]
See what you get in the MsgBox..
 
You can loop through all of the subdirectories of target path with this:
SubDirExists(targetpath, recnum)

Function SubDirExists(ByVal sDir, strMyCriteria as string) As Boolean

Dim oFS As Object
Dim oSub As Object
Set oFS = CreateObject("Scripting.filesystemobject")
Dim oDir
Set oDir = oFS.GetFolder(sDir)
For Each oSub In oDir.SubFolders
If InStr(1, oSub, strMyCriteria) > 0 Then
SubDirExists = True
Exit Function
End If
Next oSub
SubDirExists = False
End Function
 
Putting it all together:
Code:
sub test()
targetdir = targetpath & RecNum & ChannelPartner & Enduser 
Set filesys = CreateObject("Scripting.FileSystemObject")
If filesys.FolderExists(targetdir) Then GoTo function_end
if SubDirExists(targetpath, recnum) = True then GoTo function_end
MkDir targetdir
If filesys.FolderExists(copypath) Then
filesys.CopyFolder copypath, targetdir
End If
MsgBox ("folder created")
function_end:
Shell "Explorer.exe /n,/e," & targetdir, vbNormalFocus 
End Sub
 
Function SubDirExists(ByVal sDir, strMyCriteria) As Boolean
 
Dim oFS As Object
Dim oSub As Object
Set oFS = CreateObject("Scripting.filesystemobject")
Dim oDir
Set oDir = oFS.GetFolder(sDir)
For Each oSub In oDir.SubFolders
If InStr(1, oSub, strMyCriteria) > 0 Then
    SubDirExists = True
    Exit Function
End If
Next oSub
SubDirExists = False
End Function
 
Thanks for this.

What returns the path of the folder when TRUE? as it's a true/false situation
 
The True\False is checking to see if the string value of RecNum is anywhere in the the subdirectory name of the parent targetpath

For instance,
Code:
 SubDirExists("C:\", "program")
finds the "Program FIles" folder in C:\. Kinda like a wild card and returns true. Be careful beciase you are seeing if a string is in the directory name, so it may have inconsistant results if you are looking for "Program Docs" and you use
Code:
 SubDirExists("C:\", "program")
it would return true becuase of the existance of program FILES, exen though the "Promgram DOCS" folder does not exist.
 
Sorry: I read it again and see that I didin't answer the question.
Code:
targetdir = targetpath & RecNum & ChannelPartner & Enduser
still holds the Full path.
 

Users who are viewing this thread

Back
Top Bottom