Create Directory with mapbasic program

892 views
Skip to first unread message

888Peter777

unread,
Jan 31, 2010, 8:54:25 AM1/31/10
to MapInfo-L
Dear All
Is it possible to create a directory from within a mapbasic program
Regards
Peter

Peter Horsbøll Møller

unread,
Jan 31, 2010, 2:11:02 PM1/31/10
to mapi...@googlegroups.com
Pretty easy if you are using Mi Pro 9.5 or later and want to use some .NET integration:
 
C#
namespace FILELib
{
   public class MIController
   {
      //<summary>
      /// </summary>
      /// <param name="filePath">Folder to create</param>
      /// <returns>nthing</returns>
      public static void CreateFolder(string folderPath)
      {
         System.IO.Directory.CreateDirectory(folderPath);
      }
   }
}
 
MapBasic
Declare Method CreateFolder
 Class "FILELib.MIController"  Lib "FILELib.dll"
 (ByVal sFolderPath As String)
 
Let me know if you want a compiled dll in stead of creating it yourself

Peter Horsbøll Møller
Pitney Bowes Business Insight - MapInfo


2010/1/31 888Peter777 <pe...@ellesmeregeological.com>

--
You received this message because you are subscribed to the Google Groups "MapInfo-L" group.
To post to this group, send email to
mapi...@googlegroups.com.
To unsubscribe from this group, send email to
mapinfo-l+...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/mapinfo-l?hl=en.

888Peter777

unread,
Jan 31, 2010, 7:17:49 PM1/31/10
to MapInfo-L
Yes Peter
If you could supply the complied DLL as I don't know how to do it.
Further I am using Vista 64; will this work with this?
Regards,
Peter

On Feb 1, 3:11 am, Peter Horsbøll Møller <mapinf...@horsboll-


moller.dk> wrote:
> Pretty easy if you are using Mi Pro 9.5 or later and want to use some .NET
> integration:
>

> *C#*


> namespace FILELib
> {
>    public class MIController
>    {
>       //<summary>
>       /// </summary>
>       /// <param name="filePath">Folder to create</param>
>       /// <returns>nthing</returns>
>       public static void CreateFolder(string folderPath)
>       {
>          System.IO.Directory.CreateDirectory(folderPath);
>        }
>    }
>
> }
>

> *MapBasic*


> Declare Method CreateFolder
>  Class "FILELib.MIController"  Lib "FILELib.dll"
>  (ByVal sFolderPath As String)
>
> Let me know if you want a compiled dll in stead of creating it yourself
>
> Peter Horsbøll Møller
> Pitney Bowes Business Insight - MapInfo
>
> 2010/1/31 888Peter777 <pe...@ellesmeregeological.com>
>
> > Dear All
> > Is it possible to create a directory from within a mapbasic program
> > Regards
> > Peter
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "MapInfo-L" group.
> > To post to this group, send email to mapi...@googlegroups.com.
> > To unsubscribe from this group, send email to

> > mapinfo-l+...@googlegroups.com<mapinfo-l%2Bunsu...@googlegroups.com>

Ti

unread,
Feb 1, 2010, 4:42:12 AM2/1/10
to MapInfo-L
As an alternative to this, you might want to try this function call to
the Win API in MapBasic:

' declaration:
Declare Function CreateDirectory Lib "kernel32" Alias
"CreateDirectoryA" (ByVal s_pathname as String, ByVal
i_SecurityAttributes as Integer) as Integer

' vars
Dim s_path as String
Dim i_success as Integer

s_path = "C:\example\directory\path"

' create directory
i_success = CreateDirectory(s_path, 0)
if i_success = 0 Then
Note "Problem creating directory, please check that you have the
necessary rights to perform this operation or that the directory does
not already exist"
End Program
End If

AFAIK this should work fine under Vista64.

On Jan 31, 7:11 pm, Peter Horsbøll Møller <mapinf...@horsboll-


moller.dk> wrote:
> Pretty easy if you are using Mi Pro 9.5 or later and want to use some .NET
> integration:
>

> *C#*


> namespace FILELib
> {
>    public class MIController
>    {
>       //<summary>
>       /// </summary>
>       /// <param name="filePath">Folder to create</param>
>       /// <returns>nthing</returns>
>       public static void CreateFolder(string folderPath)
>       {
>          System.IO.Directory.CreateDirectory(folderPath);
>        }
>    }
>
> }
>

> *MapBasic*


> Declare Method CreateFolder
>  Class "FILELib.MIController"  Lib "FILELib.dll"
>  (ByVal sFolderPath As String)
>
> Let me know if you want a compiled dll in stead of creating it yourself
>
> Peter Horsbøll Møller
> Pitney Bowes Business Insight - MapInfo
>
> 2010/1/31 888Peter777 <pe...@ellesmeregeological.com>
>
> > Dear All
> > Is it possible to create a directory from within a mapbasic program
> > Regards
> > Peter
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "MapInfo-L" group.
> > To post to this group, send email to mapi...@googlegroups.com.
> > To unsubscribe from this group, send email to

> > mapinfo-l+...@googlegroups.com<mapinfo-l%2Bunsu...@googlegroups.com>

888Peter777

unread,
Feb 1, 2010, 7:15:38 AM2/1/10
to MapInfo-L
Peter
Here is the code I tested your code with:

'********************************************************************************************************
Include "c:/Tenman/testapp/Includes/Mapbasic.def"


' declaration:
Declare Function CreateDirectory Lib "kernel32" Alias
"CreateDirectoryA" (ByVal s_pathname as String, ByVal
i_SecurityAttributes as Integer) as Integer

' vars
Dim s_path as String
Dim i_success as Integer

Declare Sub CreateDir
Declare Sub Exitapp
Declare Sub Main

sub main
create menu "Directory Test" as
"Create a directory" calling CreateDir,
"Exit" calling ExitApp
alter menu bar add "Directory Test"
end sub

sub ExitApp
'end program
terminate application "dir_test.mbx"
end sub

Sub CreateDir


s_path = "C:\example\directory\path"

' create directory
i_success = CreateDirectory(s_path, 0)
if i_success = 0 Then
Note "Problem creating directory, please check that you have
the necessary rights to perform this operation or that the directory
does not already exist"
End Program
End If

End Sub
'*************************************************************************************************
The directory did create and did not previously exist. What could the
problem be?

Regards
Peter

Ti

unread,
Feb 1, 2010, 9:27:48 AM2/1/10
to MapInfo-L
You can only create a directory within one that already exists.

Try changing:


s_path = "C:\example\directory\path"

to:
s_path = "C:\exampledirectory"

If you need to create an entirely new directory path, you'd have to
step over the folder path to find the lowest existing directory. Then
loop over the rest of the path, creating folders in turn. This isn't
necessary a good thing to do.

Uffe Kousgaard

unread,
Feb 1, 2010, 9:45:08 AM2/1/10
to mapi...@googlegroups.com
You can do it this way:

Declare Function SHCreateDirectoryEx Lib "shell32.dll" Alias
"SHCreateDirectoryExA" (ByVal hwnd as integer, Byval LPCWSTR as string,
ByVal
SecurityAttributes as Integer) As integer

dim err as integer
err = SHCreateDirectoryEx(0,"c:\test\test1\test2",0)

This will create the whole directory structure. No looping.

See also:
http://msdn.microsoft.com/en-us/library/bb762131(VS.85).aspx

Regards
Uffe Kousgaard

888Peter777

unread,
Feb 1, 2010, 10:29:10 AM2/1/10
to MapInfo-L
Thanks Worked well

Hans Milling

unread,
Jul 19, 2018, 7:39:37 AM7/19/18
to MapInfo-L
For future reference, the most easy way to do this, is to connect to the .Net function directly:

Declare Method CreateDirectory Class "System.IO.Directory" Lib "mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" alias CreateDirectory(byVal path As String)

Then you can do:

CreateDirectory("c:\temp\my\directory\path")

This will create the entire path of subdirectories no matter how many or few exists already. No need for a DLL or lots of code lines, very simple. If you are using an older MapInfo 11/12 you might want to use .Net 3.5 or 2.0 instead.

Best regards
  Hans Milling...
Reply all
Reply to author
Forward
0 new messages