Creating a new folder with MapBasic

38 views
Skip to first unread message

Reg Halstead

unread,
Dec 28, 2022, 7:26:37 AM12/28/22
to MapInfo-L
Hello,

I hope everyone on here is enjoying the Christmas period.

I’ve started using the functions on pages 22-23 of the MapBasic Reference PDF to edit files outside of MapInfo, such as simple deleting, renaming, moving and testing if files exist, and I’ve found them very useful. So now I’m wondering if there’s also a way to create a new folder. It would come in very useful for something I have in mind.

Is there an existing function that could replace the following yellow line, please? I’m not sure if one exists or if I’ve just been unable to find it in the PDF.

Dim text1 as string
Dim text2 as string
text1 = “C:\Users\myusername\Documents\Temp files\”
text2 = “C:\Users\myuser name\Documents\”
If not(FileExists(text1)) and FileExists(text2) then
Create Folder “C:\Users\myusername\Documents\Temp files\”
End if

Many thanks in advance,

Reggie



P.S.

If there isn’t a “create folder” function and someone is reading this at a later date looking for one, then here’s a solution I've written that works by making a BAT file.

Dim text1 as string
Dim text2 as string
Dim t1 as integer
text1 = "C:\Users\myusername\Documents\Temp files\"
text2 = "C:\Users\myusername\Documents\"
If not(FileExists(text1)) and FileExists(text2) then
Open File "C:\Users\myusername\Documents\temp_bat.txt" For Output Access Write As #1
text1 = "md " & CHR$(34) & "C:" & CHR$(34) & "\" & CHR$(34) & "Users" & CHR$(34) & "\" & CHR$(34) & "myusername" & CHR$(34) & "\" & CHR$(34) & "Documents" & CHR$(34) & "\" & CHR$(34) & "Temp files" & CHR$(34)
Print #1, text1
Close File #1
text1 = "C:\Users\myusername\Documents\temp_bat.bat"
text2 = "C:\Users\myusername\Documents\temp_bat.txt"
Rename File text2 As text1
text2 = "explorer.exe " & text1
Run Program text2
' The following 3 lines make the script wait at least 2 seconds to avoid deleting the bat file prematurely. Credit to DavidW who shared this method of stalling a script in this group back in Nov 2015.
t1 = timer()
Do
Loop while t1 > timer() - 2
Kill text1
End if

Uffe Kousgaard

unread,
Dec 28, 2022, 8:24:32 AM12/28/22
to mapi...@googlegroups.com
Hi,

There is an not existing function for that, afaik, but you can use this library to do it:
https://www.routeware.dk/download/rw_utility.7z

You can also call into the winapi, using the mblibrary:
https://github.com/PeterHorsbollMoller/mbLibrary
File winapi.def

Regards
Uffe Kousgaard
--
--
You received this message because you are subscribed to the
Google Groups "MapInfo-L" group.To post a message to this group, send
email to mapi...@googlegroups.com
To unsubscribe from this group, go to:
http://groups.google.com/group/mapinfo-l/subscribe?hl=en
For more options, information and links to MapInfo resources (searching
archives, feature requests, to visit our Wiki, visit the Welcome page at
http://groups.google.com/group/mapinfo-l?hl=en

---
You received this message because you are subscribed to the Google Groups "MapInfo-L" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mapinfo-l+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mapinfo-l/aa914191-8d0e-4b57-b22b-de85e780942en%40googlegroups.com.

Reg Halstead

unread,
Dec 28, 2022, 2:39:19 PM12/28/22
to mapi...@googlegroups.com
Thanks a lot Uffe. I’ll check those out.

Matthew Hirsch

unread,
Dec 29, 2022, 9:58:01 AM12/29/22
to MapInfo-L
I found this works in mapbasic code.   I found it somewhere via a google search.   
I have not tried it in an MBX, but it works being pasted into the mapbasic window. ( I use long scripts with code changes each time I run them so I cant use a compiled MBX for them.)

Run Program "cmd.exe /c cd /D ""L:\BITI Crrt's"" && mkdir ""Crrt_boxes_Nov22"""

                 L:\BITI Crrt's  is the drive and folder we want the new folder in.

                 Crrt_boxes_Nov22 is the name of the new folder.

Regards, 
Matthew

Reg Halstead

unread,
Dec 29, 2022, 11:08:22 AM12/29/22
to mapi...@googlegroups.com
Fantastic! Thanks a lot.

--
--
You received this message because you are subscribed to the
Google Groups "MapInfo-L" group.To post a message to this group, send
email to mapi...@googlegroups.com
To unsubscribe from this group, go to:
http://groups.google.com/group/mapinfo-l/subscribe?hl=en
For more options, information and links to MapInfo resources (searching
archives, feature requests, to visit our Wiki, visit the Welcome page at
http://groups.google.com/group/mapinfo-l?hl=en

---
You received this message because you are subscribed to the Google Groups "MapInfo-L" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mapinfo-l+...@googlegroups.com.

Peter Horsbøll Møller

unread,
Jan 2, 2023, 4:52:22 AM1/2/23
to mapi...@googlegroups.com

Happy New Year!

 

And FILELib from the mbLibraries (referred to by Uffe below) also comes with support for various file operations, such as FILEDelete, FILECopyFile, FILECreateFolder, FILEDeleteFolder, FILEFindFilesInFolder(s),and  FILEFindFoldersInFolder.

 

You can find these in the file FILELib.def and most of these require that you have the file FILELib.dll in the same folder as your MapBasic application. You can also find the C# source code for building the DLL if you want to look at how that has been done. Or if you want to add additional functionality

 

Thanks

Peter

 

Peter Horsbøll Møller
Principal Sales Engineer - Distinguished Engineer

 

From: mapi...@googlegroups.com <mapi...@googlegroups.com> On Behalf Of Uffe Kousgaard
Sent: Wednesday, 28 December 2022 14.24
To: mapi...@googlegroups.com
Subject: Re: [MI-L] Creating a new folder with MapBasic

 

This message originated Externally. Use proper judgement and caution with attachments, links, or responses.

 

Reply all
Reply to author
Forward
0 new messages