Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old November 8th, 2005, 09:05 AM
dt_aiying dt_aiying is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 93 dt_aiying User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 13 h 58 m 8 sec
Reputation Power: 4
File Path Validation Check

Hi everyone,

How do I check that a file path is valid and existing?
(So that the user will not have to see a run-time error generated, in case that the specific file path is invalid / non-existant.)

And if a file path is found to be invalid and non-existant, the program should allow the user to browse and select the specific file desired.
(Still trying to work this out with Common Dialog control)

Any ideas / suggestions on how I should go about doing this? I've been trying to think this out for almost a day now.

Many thanks in advance.

Reply With Quote
  #2  
Old November 9th, 2005, 08:51 AM
sevenhalo sevenhalo is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Nov 2005
Location: Canto 34
Posts: 35 sevenhalo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 41 m 47 sec
Reputation Power: 4
Quote:
Originally Posted by dt_aiying
Hi everyone,

How do I check that a file path is valid and existing?
(So that the user will not have to see a run-time error generated, in case that the specific file path is invalid / non-existant.)

And if a file path is found to be invalid and non-existant, the program should allow the user to browse and select the specific file desired.
(Still trying to work this out with Common Dialog control)

Any ideas / suggestions on how I should go about doing this? I've been trying to think this out for almost a day now.

Many thanks in advance.

This will only work for local files (ie. C:\whereever\whatever.txt). If the file isn't found, it will reconstruct the path and find the last valid directory and use that in the OpenFileDialog Box.
Code:
Dim InitialFilePath As String = "The initial file path to check"
        Dim fso As System.IO.File
        Dim direct As System.IO.Directory

        Try
            If fso.Exists(InitialFilePath) Then
                'File is valid and exists
            Else
                Dim arrHold() As String
                Dim strReconstructed As String

                arrHold = InitialFilePath.Split("\")
                If UBound(arrHold) > 0 Then
                    arrHold.Reverse(arrHold)
                    strReconstructed = arrHold(UBound(arrHold)) & "\"
                    If direct.Exists(strReconstructed) Then
                        While direct.Exists(strReconstructed & arrHold(UBound(arrHold) - 1) & "\")
                            strReconstructed += arrHold(UBound(arrHold) - 1) & "\"
                            ReDim Preserve arrHold(UBound(arrHold) - 1)
                        End While

                        OpenFileDialog1.InitialDirectory = strReconstructed
                        OpenFileDialog1.ShowDialog()
                    Else
                        'Root directory isn't valid
                    End If
                Else
                    'Blank Value passed (should have been validated previously)
                End If
            End If
        Catch ex As WhateverYourFavoriteExceptionsAre

        Finally
            fso = Nothing
            direct = Nothing
        End Try


(Example done in VS.Net 2003 EA)

Reply With Quote
  #3  
Old November 10th, 2005, 03:03 AM
dt_aiying dt_aiying is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 93 dt_aiying User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 13 h 58 m 8 sec
Reputation Power: 4
Dear sevenhalo,

Thanks very much for your posting.

However, I'm developing my program in VB6 and not in VB.NET.
Don't think I can compile your code (posted in VB.NET) in VB6.

Currently I'm trying to work something out with API for solving this problem.

Reply With Quote
  #4  
Old November 10th, 2005, 11:20 AM
Alias-Zero Alias-Zero is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2005
Location: localhost
Posts: 40 Alias-Zero User rank is Lance Corporal (50 - 100 Reputation Level)Alias-Zero User rank is Lance Corporal (50 - 100 Reputation Level)Alias-Zero User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 14 h 44 m 12 sec
Reputation Power: 4
Send a message via ICQ to Alias-Zero Send a message via AIM to Alias-Zero Send a message via MSN to Alias-Zero Send a message via Yahoo to Alias-Zero
You could try something simple like:

Code:
Private Sub Command1_Click()
On Error GoTo EHandler
Dim path As String
path = "C:\NonExistant.NoExt"
Shell path, vbNormalFocus

Exit Sub

EHandler:
If Err.Number = "53" Then
MsgBox "File Not Found!", vbOKOnly, "Error:"
CommonDialog1.ShowOpen
path = CommonDialog1.FileName
Shell path, vbNormalFocus
Else
MsgBox Err.Number & " || " & Err.Description, vbOKOnly, "Error:"
End If

End Sub

Reply With Quote
  #5  
Old November 15th, 2005, 02:08 AM
dt_aiying dt_aiying is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Oct 2005
Posts: 93 dt_aiying User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 13 h 58 m 8 sec
Reputation Power: 4
Thanks for the help, Alias-Zero. I've got some ideas on how to go about with this now.

Reply With Quote
  #6  
Old November 16th, 2005, 12:10 AM
hithere's Avatar
hithere hithere is offline
Learner
ASP Free Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Location: India
Posts: 450 hithere User rank is Sergeant (500 - 2000 Reputation Level)hithere User rank is Sergeant (500 - 2000 Reputation Level)hithere User rank is Sergeant (500 - 2000 Reputation Level)hithere User rank is Sergeant (500 - 2000 Reputation Level)hithere User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 5 Days 12 h 13 m 21 sec
Reputation Power: 18
Hey u could use FileSystemObject instead of this

Code:
set fs = CreateObject("Scripting.FileSystemObject")
if fs.fileexists(filename) then
 msgbox "file is existing"
else
 commondialog1.showopen
 filename = commondialog1.filename
end if


i hope u got some idea. if not please ask.

Reply With Quote
  #7  
Old November 18th, 2005, 09:22 PM
Alias-Zero Alias-Zero is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2005
Location: localhost
Posts: 40 Alias-Zero User rank is Lance Corporal (50 - 100 Reputation Level)Alias-Zero User rank is Lance Corporal (50 - 100 Reputation Level)Alias-Zero User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 14 h 44 m 12 sec
Reputation Power: 4
Send a message via ICQ to Alias-Zero Send a message via AIM to Alias-Zero Send a message via MSN to Alias-Zero Send a message via Yahoo to Alias-Zero
Quote:
Originally Posted by dt_aiying
Thanks for the help, Alias-Zero. I've got some ideas on how to go about with this now.


No problem. My pleasure.

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingVisual Basic Programming > File Path Validation Check


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT