How-To... - Check if a file exists using the Dir Function

DodgeSoft Productions - How To -- Dir FunctionYou already know how to copy files through Visual Basic, but what if the file already exists. You don't want to overwrite important files. To check if a file already exists, you can use the Dir Function. Anybody that has used DOS will already be familiar with this function. The syntax is :-

Dir[(pathname[, attributes])]

For example, Dir("C:\Windows\System.ini", 0) will search for that file. If it does exist "System.ini" is returned, if it doesn't exist then nothing is returned. The 0(that's zero) means Normal. For more constants, consult the VB Online Help, or the source code for this How-To. That's how easy it is to check for a file already existing.

You can download the source code by clicking here


This site is Copyright © 1999 of David Cowan. VB How-To, Dodgesoft and DodgeSoft Productions is Copyright © 1999 of David Cowan. Source-code and programs are copyright of their respective owners. For a messageboard, click here