CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2001
    Location
    Los Angeles, CA, United States
    Posts
    47

    Len function doesn't work correctly!

    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.

  2. #2
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Len function doesn't work correctly!

    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

  3. #3
    Join Date
    Jun 2001
    Location
    Memphis, TN
    Posts
    146

    Re: Len function doesn't work correctly!

    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





  4. #4
    Join Date
    Feb 2000
    Posts
    137

    Re: Len function doesn't work correctly!

    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


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured