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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Weird Dir$() behaviour... 1

Status
Not open for further replies.

lbaker78

Technical User
Jul 20, 2005
17
GB
Hi All,

Am trying to check if a file exists using Dir$(filenamehere) and this works fine when I pass a filename to it by typign it in manually or even via a variable. However, if I read the filename from a text file using Textstream.Readline, it ALWAYS says the file exists, even when it doesnt.

I have tried to do a bit of debugging and have found that when I use the code below....

Code:
Private Sub Form_Load()
Dim Trackname As String

 Trackname = "c:\test.txt"

If Dir$(Trackname) <> "" Then

Text1.Text = "File Exists"

Else
Text1.Text = "File DOESNT Exist"
End If

End Sub

... and ask it to tell me what Dir$(Trackname) actually returns (with an INVALID FILE NAME) it shows nothing - As it's supposed to. (With a vlaid file name it returns the filename)

BUT if I read Trackname from a text file, when I ask it to return the value of Dir$(Trackname) I actually get the line of text from the file returned to me! Irrespective of whether it exists as a file or is even a valid file name! I've been trying to solve this all day and now my brain hurts... I'm guessing theres some sort of control character in the string that's returned from the text file, but I have no idea how to actually remove or even view such characters! That could be totally wrong of course... Any ideas? :)

TIA!

 
Given that you are suggesting that it might be the reading of the text from your text file it might be a good idea to show us that code, rather than the code that works ...
 
lol, yes I suppose that would be useful...Here it is:

Code:
Dim Trackname As String

    Dim fso, TextStream As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set TextStream = fso.OpenTextFile("c:\nowplaying.m3u")

 Trackname = TextStream.readline


If Dir$(Trackname) <> "" Then
Text1.Text = "File Exists" & Trackname

Else
Text1.Text = "File DOESNT Exist"
End If

End Sub

:$


So, as mentioned above, if the text file has an invalid file path in it, instead of returning an empty string I get the actual line of text returned (when you look at what Dir$(filename) actually is)....
 
OK... so I got it working by comparing the Dir$() result to the original text line (if its valid it will return the file name without the full path, so its different to the line of text but if it is a non existant file it returns the string again so the comparison is true and jumps to the else statement).

Great, but I'd still love to know why it doesnt like lines from a text file???? :)
 
I never did like the Dir function. Since you are already using the FileSystemObject, I recommend you also use it to check for the file existence.

Code:
If FSO.FileExists(Trackname) Then
  Text1.Text = "File Exists" & Trackname
Else
  Text1.Text = "File DOESNT Exist"
End If


-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
I agree. For your purposes, DIR is not ideal. Other examples of when dir$ will give you a result that you don't like could be filenames in your file that are directories. e.g. ".", ".."

To answer your question though, I have never seen an example of DIR returning a non empty string when the path specified does not exist.

I can emulate your behaviour if I use On Error Resume Next.

If you truly believe this is happening, could you reply with the result of the dir$(trackname) when it comes out wrong? I think in making the small changes to your code to find this value, you will see that DIR is behaving properly afterall.

Cheers
 
Aha! Thanks GMMastros, I have only just started using the FSO, so had no idea you could do that! :)

But PCLewis - I am now REALLY confused as to what Dir$() is doing! If I replace the incorrect file name with some other random text (ie - blahblah.mp3) it works correctly even tho its basically the same as what was written in there orginally.

The original line of text that gave me so much trouble was:

Massive Attack - Protection.mp3

It should have read
D:\music\Massive Attack - Protection.mp3

Now when I use the following code:

Code:
Private Sub Form_Load()

Dim trackname As String
Dim dirreturn As String

    Dim fso, TextStream As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set TextStream = fso.OpenTextFile("c:\nowplaying.m3u")

trackname = TextStream.readline

dirreturn = "Return Value of Dir$ : " & Dir$(trackname)

Text1.Text = dirreturn
Text2.Text = "Line of text : " & trackname

End Sub

With the incorrect line of text written above being retrieved BOTH text fields show the full text line! However, if I delete that line in the text file and replace it with something similar (ie Guns N Roses - November Rain.MP3) it works again!!! Of course, that file name is not correct either, it doesnt have a full path on it.

But what I dont understand is - what is VBs problem with Massive Attack?? :) They're a great band and never did anything to VB! lol

I guess it's no big deal now, as I'll use the FSO.FileExists, but it is weird isnt it? Or is just me... that is a possibilty!
 
Dir when called will look for the file or path provided in the first parameter. So if you provide a parameter of "Massive Attack - Protection.mp3
" and if DIR returns with "Massive Attack - Protection.mp3
" then you can be sure that the file exists in the directory specified. Since no directory is specified, Dir knows to look in the current directory (this would generally be there your project file is). Can you be sure that no such file exists there?

And VB6 (if wrong) is probably just noticing the name of that file as a potential virus. I mean, who would name a file MASSIVE ATTACK and suffix it with NEGATIVE PROTECTION. Sounds like a trojan horse to me.. :p
 
LOL!! :D

And nope, the file doesnt exist in the working directory! :eek:

That's gonna make me chuckle all day now.... :)

 
Code:
'A reference to the Scripting runtime (SCRRUN.DLL) is necessary
Public Function DriveExists(Drive As String) As Boolean

Dim oDir As New Scripting.FileSystemObject
    
    DriveExists = oDir.DriveExists(Drive)
    
End Function

Public Function DirectoryExists(Dir As String) As Boolean

Dim oDir As New Scripting.FileSystemObject
    
    DirectoryExists = oDir.FolderExists(Dir)

End Function

Public Function FileExists(File As String) As Boolean

Dim oDir As New Scripting.FileSystemObject
    
    FileExists = oDir.FileExists(File)

End Function

Everybody body is somebodys Nutter.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top