Jump to content

Welcome to eMastercam

Register now to participate in the forums, access the download area, buy Mastercam training materials, post processors and more. This message will be removed once you have signed in.

Use your display name or email address to sign in:

browseforfolder and browseinfo function


Recommended Posts

Hi!

my first try to do a vbscript and I'm almost to where I'd like to.

Here is the project, I'd like to browse for a folder but I don't want to start from the desktop everytime. I'd like to start from a predefine folder but I would alos like to keep the possibility to go up some level. As I have been able so far to get the the starting folder I wanted but I can't go higher in the root...

Also if I could get a window format with the favorites (like the Win7 browser window for example) it would be great! (I've read there is something about the browseinfo function or command but I don't understand how to use it exactly)

 

So basically I'd like to either one of these 2 solutions:

- Start to a predefined folder but with the possibility to go up the root or even change completely the location.

or

- use a browser window like the mastercam one or the WinXP or Win7 browser with the favorites or special folder on the left side for quick selection.

 

Any help is appreciated

 

thanks in advance

 

PS Note that I'm calling this script from a post so that is why I write the selected folder into a text file.

 

André

 

here is the code I'm using right now.

 

'///////////////// My Constants /////////////////







'///////////////// My Global Variables //////////







' -- Start Script

Call Main()





' ////////////////////

' Sub Declaration

' ////////////////////

Sub Main()



' ***************************************************

' * This function will open the NC file for the nest

' ***************************************************



Dim strOutputFolder

Dim FSO, fsoMCX, fsoFiles, fsoNCFile



Set FSO = CreateObject("Scripting.FileSystemObject")



' -- Get MCX folder 

strOutputFolder = BrowseForFolder("Select folder containing MCX files")

If FSO.FolderExists(strOutputFolder) Then 



               Set fsoMCX = FSO.GetFolder(strOutputFolder) 

               'strOutputFolder = BrowseForFolder("Select folder for NC Output files")

               'If FSO.FolderExists(strOutputFolder) Then 

               '               Set fsoFiles = fsoMCX.Files

                               ' -- Iterate all files per folder 

                '              For Each fsoMCX In fsoFiles   

               ' -- Only looking for MCX files

    '          If Right(ucase(fsoMCX.Name), 3) =  "MCX" Then

               ' -- Open it

     '         Call NewMC(false)

      '        Call OpenMC(fsoMCX)

                                               ' -- Post it

                               'Call RunPostAll(strOutputFolder, false)

               'End If

               'Next

   'End If

   FSO = WriteFileText(strOutputFolder)

End If



' -- Clean up

Set fsoNCFile = Nothing

Set fsoFiles = Nothing

Set FSO = Nothing



End Sub







' //////////////////////////////////////////

' Function Declaration

' Returns vbNullString if no folder selected

' //////////////////////////////////////////



Function BrowseForFolder(strPrompt)



   On Error Resume Next



   Dim objShell, objFolder, intColonPos, objWshShell

   Dim FSO, varTemp

   Const sFilestart = "C:\Users\Andre Lapointe\Documents\my mcamx5\Mill\NC"



   ' -- Create our all important objects

   Set objWshShell = CreateObject("WScript.Shell")

   Set objShell = CreateObject("Shell.Application")

   Set FSO = CreateObject("Scripting.FileSystemObject")



   Set objFolder = objShell.BrowseForFolder(&H0&, strPrompt, &H1&, sFilestart)



   ' -- Initialize to failure

   BrowseForFolder = vbNullString



   ' -- Did the user Cancel?

   If Not objFolder Is Nothing Then



       Select Case objFolder.Title



              ' -- Did the user select Desktop?

              Case "Desktop"

                  BrowseForFolder = objWshShell.SpecialFolders("Desktop")



              Case Else

                   ' -- Get the folder selected

                   varTemp = objFolder.ParentFolder.ParseName(objFolder.Title).Path



                   ' -- If the user does not select a folder or cancels out an error is raised

                   If Err Then



                      Select Case Err.Number

                             ' -- Probably root drive selected e.g. (C:)

                             Case 91, 424

                                   ' -- Check for Root

                                   intColonPos = InStr(objFolder.Title, ":")



                                   If intColonPos > 0 Then

                                      BrowseForFolder = Mid(objFolder.Title, intColonPos - 1, 2) & "\"

                                   End If



                              Case Else



                       End Select



                       Err.Clear



                   Else

                       ' -- If we get this far make sure the folder exists

                       ' -- selecting a network folder may result in an invalid folder

                       If FSO.FolderExists(varTemp) Then BrowseForFolder = varTemp

                   End If



        End Select

   End If



   ' -- Check for an error of any kind

   If Err Then

      BrowseForFolder = vbNullString

      ShowString "Error selecting folder: " & Err.Description, vbExclamation, "BrowseForFolder"

      Err.Clear

      Exit Function

    End If



' -- Clean up

Set objWshShell = Nothing

Set objShell = Nothing

Set objFolder = Nothing

Set FSO = Nothing







End Function





'-------------------------------------------------------------------------------

' WriteFileText - Used to write an item to a file in a folder.

' Parameters:

'   sFile - The file to read

'

' Returns:

'   A string containing the content of the file.

'-------------------------------------------------------------------------------

Function WriteFileText(sText)    'sFilepath --> path du fichier texte   sText---> path que je veux

   Dim objFSO 'As FileSystemObject

   Dim objTextFile 'As Object



   Const ForReading = 1

   Const ForWriting = 2

   Const ForAppending = 8

   Const sFilePath = "C:\test.txt"



   Set objFSO = CreateObject("Scripting.FileSystemObject")

   Set objTextFile = objFSO.CreateTextFile(sFilePath, True)



   ' Write a line.

   objTextFile.Write (sText)



   objTextFile.Close

   'objTextFile.Close



End Function


Link to comment
Share on other sites
- Start to a predefined folder but with the possibility to go up the root or even change completely the location.

I don't believe this is possible with VB Script.

The folder path you supply as the final parameter is the "root" folder,

so you cannot navigate above that - *using VBScript.

Ref: Shell.BrowseForFolder method

 

or

- use a browser window like the mastercam one or the WinXP or Win7 browser with the favorites or special folder on the left side for quick selection.

Where in Mastercam do you see the dialog your describe used when selecting a folder?

It sound like you are describing the File Select/Open dialog.

 

Why not use a NETHook? (Either C# or VB)

You can find MUCH more useful and consistent help info for these than the myriad ways of VBScript. Even Microsoft has basically abandoned VBScript "as-is" for PowerShell. (Which is very powerful and very confusing for getting started or for something you use every once in awhile).

You can use the free Visual Studio "Express" versions for creating NETHooks.

 

Here is a VB.NET function that does the std FolderBrowserDialog, but unlike the VBScript version, the SelectedPath is preset with the desired initial folder and the user can browse above this folder (or actually anywhere).

*I prefer C# and I think you'll find more how-to help out on the Web for it vs. VB.

Either way it is simple to convert between the two. Which is exactly what I did here from for this code C# -> VB

''' <summary>Display the 'Browse for Folder' Dialog.</summary>
''' <param name="initialFolder">The default starting folder path.</param>
''' <returns>The selected Folder or String.Empty if user aborted.</returns>
Public Shared Function SelectFolder(initialFolder As String) As String
Dim folder As String = String.Empty

Dim fbd As New FolderBrowserDialog()
If Directory.Exists(initialFolder) Then
	fbd.SelectedPath = initialFolder
End If
fbd.ShowNewFolderButton = True
fbd.Description = "Select output subfolder..."
If fbd.ShowDialog() = DialogResult.OK Then
	folder = fbd.SelectedPath
End If

Return folder
End Function

Link to comment
Share on other sites

Thanks Roger,

The main reason why I wanted the VBS instead of the Nethook is because I didn't want to support the Nethook at every new release of Mastercam.

The VBS allow me to just copy and pste to the new folder, I put it in the post folder so I don't even have to change the path in the post for it to work in a more recent version.

 

As for the window I was refering to in Mastercam, yes it is the file select/open dialog box.

 

Thanks for looking at it I really appreciate it!

 

I guess I'll discuss with the customer to see what he prefers, starting on the desktop or in the sub folder with no possibility to go higher in the root.

Thanks again.

Link to comment
Share on other sites
The main reason why I wanted the VBS instead of the Nethook is because I didn't want to support the Nethook at every new release of Mastercam.

 

I understand, but if you're going to give them the VBScript source (not that you have a choice)

you can certainly give then the .NET project. Then they can rebuild it (if needed) using a free Express version of Visual Studio.

Link to comment
Share on other sites

I'd like to browse for a folder but I don't want to start from the desktop everytime.

I'd like to start from a predefine folder but I would alos like to keep the possibility to go up some level.

 

Well, how about SEEDING the BrowseForFolder dialog with a folder one or more folders UP from the folder

you (probably) want the user to select?

 

Function ParentFolder(strBaseFolder)
Dim filesys
Set filesys = CreateObject("Scripting.FileSystemObject")
ParentFolder= filesys.GetParentFolderName(strBaseFolder)
End Function

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.

Join us!

eMastercam - your online source for all things Mastercam.

Together, we are the strongest Mastercam community on the web with over 56,000 members, and our online store offers a wide selection of training materials for all applications and skill levels.

Follow us

×
×
  • Create New...