I have the following VB6 code

Dim objdir As Scripting.FileSystemObject
cfile="c:\workingdir\france\include"
If DirExists(cfile) = False Then
        objdir.CreateFolder(cfile)
End If

Public Function DirExists(OrigFile As String) As Boolean
Dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
DirExists = fs.FolderExists(OrigFile)
End Function

The folder C:\workingdir\france already exists and there should not be any permissions problems.
I step through the code to make sure the createfolder method is called. No error is raised, but the directory is not created.
Have exactly the same problem in vb.net if I try

my.Computer.FileSystem.CreateDirectory(cfile)

I always use this when in VB .. it's not as complicated, and if your permissions are ok it works every time..

If Dir("c:\newfolder") = "" Then MkDir ("c:\newfolder")

Have to say I was a bit sceptical, but that actually worked. Still cannot understand why orig code doesn't. Many thanks.

Sometimes the simple way works best ..

You can also use 'rmdir' (rd) to delete a file or folder, and 'chdir' (cd) to change the current directory. Yes, these are DOS commands (dos is old! OMG!) but they are still supported in all versions of windows and Visual Basic. These same commands are built in to Linux and other operating systems and work very well.


look here for more - http://en.wikipedia.org/wiki/List_of_DOS_commands


You should mark this thread as "Solved" and pass me the credit. Thanks.

It does not look like you have instantiated your object. Use one of the following 2 methods:

Dim fso as FileSystemObject
set fso = new FileSystemObject

' Or
Dim fso as New FileSystemObject

The first method is early binding and is quicker. The second method is late binding and is somewhat slower.

Or maybe I'm just not used to your methodology of instantiating an object. I don't usually do it that way. Doesn't mean that your way is not correct.

In most cases the directory will be there, so no need to instantiate an object which might never be used. Also just on local machine so performance not an issue.

I tried it your way. Made a directory. and used your method to delete it.
It was as I suspected: the object was not created: error 91, Object variable or block object variable not set.

A directory is not an object. An object has properties, methods, and or events.

Objects are encapsulated — that is, they contain both their code and their data, making them more easier to maintain than traditional ways of writing code.

Visual Basic objects have properties, methods, and events. Properties are data that describe an object. Methods are things you can tell the object to do. Events are things the object does; you can write code to be executed when events occur.(MSDN: Programming with objects)

Set objdir = New Scripting.FileSystemObject

Sorry, left

Set objdir = New Scripting.FileSystemObject

out of my post but have it in the program (it would not have run otherwise).

The key point here is that, as I said in my original post, VB will execute

objdir.CreateFolder(cfile)

without generating an error but will not create the directory.

In my last reply,

no need to instantiate an object

I was referring to the objdir object.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.