advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Partners & Affiliates
advertisement
advertisement
Tip of the Day
Average Rating: 5/5 | Rate this item | 1 user has rated this item.
Expertise: Intermediate
Language: Visual Basic
February 24, 1999
Quirks of the Dir$ Function
If you use the intrinsic VB Dir$ function to check for the existence of a certain file, then subsequently try to remove the directory where the file is found using the VB RMDir statement, you get the error 75, "Path/File access error." This error occurs even if you kill the file prior to removing the directory.

You can see the problem if you manually create a directory and file with the names c:\dummy\bla-bla.txt. Then try to go step-by-step through this sample code to see what's going on:

 
Private Sub Command1_Click()
	If Dir$("C:\dummy\bla-bla.txt") = "" Then
		'do nothing, file is not found
	Else
		'kill the file and remove the directory
		Kill "C:\dummy\bla-bla.txt"
		RmDir "C:\dummy"
	End If 
End Sub 

The statement RmDir "C:\dummy" causes error 75 because this directory is locked and cannot be removed.

To work around this problem, check for the existence of a file by trying to open it for sequential/random/binary (anything should work) access and close it immediately afterwards. If this file exists, your routine will proceed with its code, where you can kill the file and remove the directory. A trappable error 53, "File not found", indicates the file does not exist. After you trap the error, you can re-direct the execution of your code as required. This code is a good example to start with:

 
Private Sub Command1_Click()
	Dim FHandle As Long
	Dim FileNAme As String
	FileNAme = "C:\dummy\bla-bla.txt"
	FH = FreeFile
	On Error Goto ErrHadler
	Open FileNAme For Input As FHandle
	Close FHandle
	Kill FileNAme
	RmDir "C:\dummy" 

CleanUp:
Exit Sub 

ErrHadler: 
	Select case Err
	Case 53 'File not found
		Resume CleanUp
	Case Else
		'display error info
	End Select 
End Sub 

You can't use Sequential Access for Output or Append. If the file does not exist, it is created automatically when the Open statement executes, and all your code loses sense.

Also note that the RmDir statement causes error 75, "Path/File access error", if you try to remove a directory that's not empty. Kill all the files one by one prior to removing a directory, or opt to use an API to do the job.

Of course, you would never want to go to this extreme unless you find that there's no alternative. This is an incredibly bizarre behavior, and most apps would never be affected by it.

Brian Hunter
If you have a hot tip and we publish it, we'll pay you. However, due to accounting overhead we no longer pay $10 for a single tip submission. You must accumulate 10 acceptable tips to receive payment. Be sure to include a clear explanation of what the technique does and why it's useful. If it includes code, limit it to 20 lines if possible. Submit your tip here.
Please rate this item (5=best)
 1  2  3  4  5
advertisement
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs