Results 1 to 5 of 5

Thread: Opening Folder if file exists in folder

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2011
    Posts
    1

    Exclamation Opening Folder if file exists in folder

    Having difficulty with the module below failing on line 21. It is telling me:
    The system cannot find the file specified. (Exception from HRESULT: 0x80070002)
    Basically what it is designed to do is look for a directory, if that exists then look for a file in that directory. If both = True then it should open the folder to browse the files. I believe the issue is with the "Program Files" being 2 words since the script runs using a root path, but I'm not sure how to format the path correctly to make it work?! Ideas?

    Code:
            
    Imports System.IO
    Module Startup
    
        Sub Main()
            Dim RPath As String
            Dim RedunOff As String
            RPath = "C:\Program Files\Directory\Data\Configuration"
            RedunOff = "C:\Program Files\Directory\Data\Configuration\Random.xml"
            If Directory.Exists(RPath) = True Then
                If File.Exists(RedunOff) = True Then
                    Dim objShell = CreateObject("Wscript.Shell")
                    objShell.run(RPath)
                End If
            End If
    
        End Sub
    
    End Module

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Opening Folder if file exists in folder

    First up, if you want to get the path to the Program Files folder, or any other standard folder for that matter, then you should be using Environment.GetFolderPath or My.Computer.FileSystem.SpecialDirectories. You can then use IO.Path.Combine or My.Computer.FileSystem.CombinePath to append a relative folder or file path. If you then want to open a file or folder, you should be calling Process.Start. If you pass a folder path then it will open that folder in Windows Explorer.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Opening Folder if file exists in folder

    try this:

    vb Code:
    1. Dim RPath As String = "C:\Program Files\Directory\Data\Configuration"
    2. Dim RedunOff As String = "C:\Program Files\Directory\Data\Configuration\Random.xml"
    3. If IO.Directory.Exists(RPath) AndAlso IO.File.Exists(RedunOff) Then
    4.     Process.Start(RPath)
    5. End If

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Opening Folder if file exists in folder

    By the way, there's not much point checking for the existence of the folder and the file. If the file doesn't exist then it doesn't matter whether the folder exists because you're not using it anyway and if the file exists then you know for a fact that the folder exists too.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Opening Folder if file exists in folder

    Quote Originally Posted by jmcilhinney View Post
    By the way, there's not much point checking for the existence of the folder and the file. If the file doesn't exist then it doesn't matter whether the folder exists because you're not using it anyway and if the file exists then you know for a fact that the folder exists too.
    oh yeah... didn't think of that...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width