Quirks of the Dir$ Function

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:dummyla-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:dummyla-bla.txt") = "" Then		'do nothing, file is not found	Else		'kill the file and remove the directory		Kill "C:dummyla-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:dummyla-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.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes