Tag Archives: folder

Open a Folder/Directory and Select/Highlight a Specific File

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
 

Have you ever seen a program that can open a files destination/containing folder and also highlight/select the filename as well in the opened folder window? This will show you a simple way to do the same thing. Basically your using shell to access explorer.exe while passing certain command line arguments. All versions of Visual Basic and Visual Basic.NET can easily interface with with the explorer.exe process. VB.NET, VB 2008, Visual Basic 2010, and higher will use the same code. VB 6.0 will need an additional piece of code to open the process. The target .exe and argument text passed on are used for all VB’s however.

VB 6.0 will need to add the API shell code below to the Declarations section…

Private Declare Function ShellExecute Lib “shell32.dll” Alias “ShellExecuteA” ( ByVal hwnd As Long , ByVal lpOperation As String , ByVal lpFile As String , ByVal lpParameters As String , ByVal lpDirectory As String , ByVal nShowCmd As Long ) As Long

 

Then call the ShellExecute function and pass the proper parameter arguments.  Here is the code to open the folder and select a filename using VB 6.0…

ShellExecute  Me.hwnd, vbNullString, “explorer” , “/select,” & “c:\myFileToOpen.zip” , vbNullString, 1

 

Visual Basic .NET has built-in support for running shell based code by using the Process class. Using this class is very simple. Just add the proper arguments and parameters like below…

Process.Start( “explorer” , “/select,” & “c:\myFileToOpen.txt”)

Thats all there is to it! As you can see its a very simple code to perform this functionality. Hope this little snippet and article proves helpful to you!

Jason

Revised: 2014

Check if a File or Folder/Directory Exists in Visual Basic 6.0

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 3.40 out of 5)
 

There could be a time when you need to check a file or folder exists or not. There are actually a different ways to accomplish this task. You could use the Dir Function located in the FileSystem Class, the Windows API, or the FileSystemObject located in the Windows Scripting Host Object Model. I will show how to use the Dir method and the FileSystem Scripting Object to check if a folder/file exits or not.


Using the DIR Function…

This feature is accessible from the VB 6.0 Runtime, so you don’t need to add any references or components. In my experience this method works just fine, but there could be a scenario that it may not work properly for you.

You simply call the Dir function with the path of the file or directory in the parameter. If the functions returns nothing then its not seeing the path you specified and thus the file or directory does not exist.

    If Dir("c:\myFile.txt") <> "" Then

        MsgBox "It Exists!"

    Else

        MsgBox "No Go!"

    End If

That’s all there is to it. As I said, I do not know how this method would work in every scenario, but I’ve seen no problems yet.


Using the Scripting FileSystemObject

This method is not part of the VB 6.0 runtime. So you will first want to go to the Project menu and click on the References item. Once all of the objects are displayed scroll down till you see – “Windows Script Host Object Model”, checkmark it then click OK.

Next you need to create an instance of the FileSystemObject you just referenced.

    Dim f As FileSystemObject

    Set f = New FileSystemObject

Now you just need to call the available FileExists and FolderExists Functions while passing the path for the file and the path for the directory you want to check.

    MsgBox f.FileExists("c:\myFile.txt")

    MsgBox f.FolderExists("c:\")

Depending on whether the file or folder path you specified exits or not the messagebox should have thrown a True or False message.


That’s all there is to it for checking whether a file exists or if a folder/directory exists. The FileSystemObject method is more elegant and maybe more reliable than using the Dir Function but at the cost of having to add a Reference to the Windows Scripting Object which is not apart of the Visual Basic 6.0 Runtime Library. The Dir method still seems to work ok for me. Both ways appear to be case in-sensitive so you won’t have to worry about the letter casing being exact. IF you know of some other ways please feel free to leave a message with the way you do it. Anyways, Have Fun!

         Jason

Revisited: 2015

Get or Extract/Trim the extension from a filename and path in VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 5.00 out of 5)
 

This will remove the extension from the filepath and return the extension only. There are two methods of doing it below. If you don’t care that the . (period) is included with the extension then use the second way. This works for all versions of .NET including Visual Basic 2010.

Here is One way to do it…

'
'This will extract and return the extension without the dot from a specified filename/path.
'
Dim filePath As String = "c:\MyDirectory\MY.File.jpeg"
Dim dotPosition As Integer = filePath.LastIndexOf(".")
Dim extOnly As String = filePath.Substring(dotPosition + 1)

MsgBox(extOnly)


Another easy way from Jim. But it will include the “.” period with the string.

'
'Thanks to Jim for this other method to get the file extension. Just remember that the "."
'(dot or period) will be included with the extension. So you will need to add codes to remove
'it if you don't want the extension seperator character included.

Dim fullPath As String = "c:\MyDirectory\MYFile.txt"
Dim fileName As String = IO.Path.GetExtension(fullPath)

MsgBox(fileName)

 

Have fun 🙂

Check if a Specified File or Directory Exists in VB 2008/2010 and Newer

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 4.60 out of 5)
 

Shows how to use Visual Basic.NET 2005/2008/2010 and Higher to check if a file and if a folder or directory exists or not.

MsgBox(My.Computer.FileSystem.FileExists("c:\theFile.txt"))
MsgBox(My.Computer.FileSystem.DirectoryExists("c:\"))

Copy, Create, Delete, and Rename a Directory/Folder using the MY Interface

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (4 votes, average: 3.75 out of 5)
 

This source code works for VB 2005, VB 2008, Visual Basic 2010, and higher.


'
'Copy, Create, Delete, and Renaming a Directory.
Dim dir AsString = "c:\Directory"'

'
'Create a directory with the path and name specified in the dir string variable.
My.Computer.FileSystem.CreateDirectory(dir)

'
'New directory with the contents of the dir path copied to it.
My.Computer.FileSystem.CopyDirectory(dir, "c:\Directory Copied")

'
'Rename the original directory that is specified in the dir variable to the new directory name.
My.Computer.FileSystem.RenameDirectory(dir, "Directory Renamed") 

'
'Delete both the Copy Directory and the Renamed Directory from the harddrive.
My.Computer.FileSystem.DeleteDirectory("c:\Directory Copied", FileIO.DeleteDirectoryOption.DeleteAllContents) 

My.Computer.FileSystem.DeleteDirectory("Directory Renamed", FileIO.UIOption.AllDialogs, FileIO.RecycleOption.SendToRecycleBin, FileIO.UICancelOption.DoNothing)

How to Check if a File and Directory or Folder Exists using VB.NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 3.00 out of 5)
 

This shows how to check if a File or a Directory and Folder Exists using VB.NET and Higher

This will check if a File exists or not


        MessageBox.Show(IO.File.Exists("d:\someFileName.txt"))

This will check if a Folder or Directory exists or not


        MessageBox.Show(IO.Directory.Exists("c:\myFolder"))

 

Add ‘all’ Special-Folder names to a Listbox/Combobox using VB.NET or higher

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
 

This will add the names of all the available special folders like MyDocuments, MyPictures, Startup, Programs, ProgramFiles, and so on

        '
        'Should add all of the available special folder names or directorys to a listbox or combobox.
        '
        Dim sFolder As Environment.SpecialFolder

        For sFolder = 0 To 43

            If Not Char.IsNumber(sFolder.ToString) Then

                Listbox1.Items.Add(sFolder.ToString)

            End If

        Next