Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreVisual 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 Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old September 5th, 2003, 10:34 AM
marcel_911's Avatar
marcel_911 marcel_911 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: Midlands, UK
Posts: 37 marcel_911 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 18 m 23 sec
Reputation Power: 6
Send a message via Yahoo to marcel_911
Question An easy way to check to see if a file exists in VB6?

Hi, Is there an easy way in VB6 to check to see if a file exists?

It seems daft to have to try and open it and then trap and error.

All I want to do is check to see it a file exists and if it does then I want to copy it to another folder.

I can't get it to work.

All the ON ERROR GOTO label stuff really makes a mess of the structure of my program.

Is there a way like in C :- handle = fopen(.... etc etc) and then handle is NULL if the file doesn't exist? That seems a more elegant way of doing it.

Thanks

Reply With Quote
  #2  
Old September 5th, 2003, 11:21 AM
zak2zak zak2zak is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: Singapore
Posts: 34 zak2zak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Send a message via Yahoo to zak2zak
Lightbulb File Exist In VB?

May be this helps....
Code:
'Assume That The File Is At "C:\What\File.txt"
If Dir("C\What\File.txt") <>"" Then
FileCopy "C:\What\File.txt", "C:\Where\File.txt"
Else
MsgBox "File Does Not Exist!"
End If
__________________
I May Have Misinterpret U'r Post
Correct Me If I Am Wrong......//
Enjoy Coding..........................///

zak2zak

Reply With Quote
  #3  
Old September 5th, 2003, 11:53 AM
marcel_911's Avatar
marcel_911 marcel_911 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: Midlands, UK
Posts: 37 marcel_911 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 18 m 23 sec
Reputation Power: 6
Send a message via Yahoo to marcel_911
Re: File Exist In VB?

Quote:
Originally posted by zak2zak
May be this helps....


Yes. Thank you VERY much. That has done the trick

Reply With Quote
  #4  
Old September 10th, 2003, 12:00 AM
cleverpig cleverpig is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jul 2003
Posts: 1,152 cleverpig User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via MSN to cleverpig
Talking

In anther way:
if Len(dir(C\What\File.txt") >0 Then
FileCopy "C:\What\File.txt", "C:\Where\File.txt"
Else
MsgBox "File Does Not Exist!"
End If

or Use Ms srcipt object...

Reply With Quote
  #5  
Old October 31st, 2003, 11:56 AM
BillBrosius BillBrosius is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 2 BillBrosius User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
What is the syntax if I want to check for file in same directory as executable?

For example, I have an executable that relys on an INI file for info. The INI file should be located in the same location as the EXE file. So I need to check to see if INI file is there.

This doesn't seem to work:

If Dir("File.ini") <>"" Then
MsgBox "File Exists!"
Else
MsgBox "File Does Not Exist!"
End If

Reply With Quote
  #6  
Old November 2nd, 2003, 03:56 AM
zak2zak zak2zak is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: Singapore
Posts: 34 zak2zak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Send a message via Yahoo to zak2zak
Lightbulb Check INI In Same Dir

May Be This Helps..
Code:
If Dir(App.Path & "\File.ini") <>"" Then
MsgBox "File Exists!" 
'Statement here
Else
MsgBox "File Does Not Exist!"
End If

Reply With Quote
  #7  
Old November 2nd, 2003, 01:31 PM
BanksySan's Avatar
BanksySan BanksySan is offline
A mule with a spinning wheel.
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Wales
Posts: 113 BanksySan User rank is Corporal (100 - 500 Reputation Level)BanksySan User rank is Corporal (100 - 500 Reputation Level)BanksySan User rank is Corporal (100 - 500 Reputation Level)BanksySan User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 20 h 48 m 39 sec
Reputation Power: 7
MySpace
FileSystemObject Solution

If you are using the FileSystemObject then

FileSystemObject.FileExists(filePath)

Returns True or False. filePath can by absolute or relative.

Reply With Quote
  #8  
Old November 3rd, 2003, 02:48 PM
BillBrosius BillBrosius is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 2 BillBrosius User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Re: Check INI In Same Dir

Quote:
Originally posted by zak2zak
May Be This Helps..
Code:
If Dir(App.Path & "\File.ini") <>"" Then
MsgBox "File Exists!" 
'Statement here
Else
MsgBox "File Does Not Exist!"
End If



This did the trick. Thanks a bunch.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > An easy way to check to see if a file exists in VB6?


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 5 hosted by Hostway