Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Check whether 'filename' exists 2

Status
Not open for further replies.

PHILinUSA

Technical User
Oct 12, 2000
1
US
Could someone please help me to understand how to check whether a filename exists. For instance, I wish to save data to a file using the OPEN 'filename' for 'mode' AS #n.

I can do this but, I then want to be able to check whether, given user input, and using the Input mode of access whether a file exists and therefore I can go ahead and read data. Is there a 'return value' using the OPEN command that can be used?

Also, is there a way to check for the existance of a file straight from the 'windows shell'?

Thanks guys+gals,

Phil [sig][/sig]
 
If Dir("C:\test.txt") = "" Then
MsgBox "Selected file does not exist."
Else
Open "C:\test.txt" for Input As #1

'Process the file here
'Dir did not return an empty string, so it exists
End If

If the file test.txt exists in the root directory of the c drive the Dir Function will return "test.txt", otherwise it
will return an empty string "". You could also do something like

Dim RetVal As String

RetVal = Dir("c:\test.txt")

if it suits you better. This way the value of RetVal will be "" if the file given to the Dir function does not exist and the filename(minus path) if it does. You must give a full path name because otherwise it will just search the current directory.

[sig]<p>Ruairi<br><a href=mailto:ruairi@logsoftware.com>ruairi@logsoftware.com</a><br>Experienced with: <br>
VB6, SQL Server, QBASIC, C(unix), MS Office VBA solutions<br>
ALSO: Machine Control/Automation using GE and Omron PLC's and HMI(human machine interface) for industrial applications[/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top