Click to See Complete Forum and Search --> : Len function doesn't work correctly!


Jeong
August 6th, 2001, 12:24 PM
I used Len function in my program to know the folder exists or not but doesn't work corectly.
Source is like following.

Private Sub Command1_Click()
Dim strSrcDir As String
Dim SrcDirLen As Integer
Dim fs, s

Set fs = CreateObject("Scripting.FileSystemObject")
Set s = fs.GetDrive(fs.GetDriveName(fs.GetAbsolutePathName(Drive1.Drive)))

strSrcDir = "C:\Windows\System\"
SrcDirLen = Len(Dir(strSrcDir))
MsgBox "source path = " + strSrcDir + vbCrLf + "Length = " + CStr(SrcDirLen), vbOK

End Sub

If run the program, SrcDirLen becomes 10.
Is it right that SrcDirLen becomes 18?
Len function returns unpredictable value even if input other folder name as the input parameter of the Len function.
Anyone who explain let me know the reason?
Thank you.

I'm a senior programmer.
Working at CamSight, Dental imaging solutions industry.

John G Duffy
August 6th, 2001, 01:06 PM
MSDN Help says the LEN function returns the number of bytes required to store a variable. The way you are using the LEN function tells VB to issue a DIR command then produce the length of the returned filename. Here is a small alteration of your program that shows this.

private Sub Command1_Click()
Dim strSrcDir as string
Dim SrcDirLen as Integer
Dim fs, s

set fs = CreateObject("Scripting.FileSystemObject")
set s = fs.GetDrive(fs.GetDriveName(fs.GetAbsolutePathName(Drive1.Drive)))

strSrcDir = "C:\Windows\System\"
s = Dir(strSrcDir)
MsgBox s
SrcDirLen = len(s)
MsgBox "source path = " + strSrcDir + vbCrLf + "Length = " + CStr(SrcDirLen), vbOK

End Sub




When I run my sample, the DIR command returns "OEMINFO.INI" It may be different on your computer.
The LEN(S) function then reports the length of the "OEMINFO.INI"

John G

Ghost308
August 6th, 2001, 01:08 PM
Hi there,
I'm really not sure why you are getting those results with that algorithm, but maybe try this one to see if the folder exists...


Dim filesys
set filesys = createobject("scripting.filesystemobject")

If filesys.FolderExists("c:\myfolder") then
Msgbox "That folder exists!"
End If

Spectre
August 7th, 2001, 06:11 AM
Use the API PathIsDirectory to determine the existance of a folder. The Dir command returns the name of the first file it finds in a directory, so do you know what you get when you point it to an empty folder? An empty string (len = 0). PathIsDirectory returns 0 if the path doesn't exist or a positive number if it does.

Hope this helps.
Spectre