CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2003
    Location
    San Diego, California
    Posts
    49

    Dir() with spaces??

    Hello,

    I am using the following code:

    Code:
    If fso.FileExists(Dir(DataPath & "\Data_*.txt")) then
    'file exists...execute code
    End If
    This works great if my DataPath has no spaces in it. For example: C:\Test. But if my DataPath variable has spaces like C:\Documents and Settings...then it does not work.

    How can I make this code work even if the variable DataPath has spaces? Thanks so much..

    Steph!

  2. #2
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,900

    Re: Dir() with spaces??

    Try this - seems to work fine


    Dim DataPath As String
    DataPath = Text1.Text
    If Dir(DataPath & "\*.txt") = "" Then
    MsgBox "NO FILE"
    Else
    MsgBox "FILE EXISTS"
    End If

  3. #3
    Join Date
    Nov 2003
    Location
    San Diego, California
    Posts
    49

    Re: Dir() with spaces??

    Thanks for your response. I am still having trouble though.

    My scenario is I could have multiple data_*.txt files. These files get date stamped. For example, I could have data_05132005.txt and data_05142005.txt.

    I need to check for the presence of a data_*.txt file and if it is there copy it to another directory and delete the original. My code below has always worked but I never had spaces in my paths. Now I have a case where the path does have spaces.

    Code:
    If fso.FileExists(Dir(DataPath & "\Data_File_*.txt")) Then
    
    fso.CopyFile Dir(DataPath & "\Data_File_*.txt), "C:\Data"
    fso.DeleteFile Dir(DataPath & "\Data_File_*.txt)
    
    End If
    Again, this has worked for me as long as DataPath had no spaces. There has to be some way I can handle DataPath correctly when it has spaces? I've searched around and I can not find anything that is relevant.

    Thanks again!

    Steph

  4. #4
    Join Date
    Aug 2003
    Location
    Sydney, Australia
    Posts
    1,900

    Re: Dir() with spaces??

    The following works


    Private Sub Command1_Click()
    DataPath = "C:\My Data Directory"
    If Dir(DataPath & "\Data_File_*.txt") <> "" Then

    FileIn = Dir(DataPath & "\Data_File_*.txt")

    PathIn = DataPath & "\" & Dir(DataPath & "\Data_File_*.txt")

    FileCopy PathIn, ("C:\Data\" & FileIn)
    Kill PathIn

    End If

    End Sub



    Your problem was not the spaces in the Directory Name - it was


    fso.CopyFile Dir(DataPath & "\Data_File_*.txt), "C:\Data"

    should have been


    fso.CopyFile Dir(DataPath & "\Data_File_*.txt), "C:\Data\" & filename


    You need a destination filename in a copy function - not just a destination directory name

  5. #5
    Join Date
    Nov 2003
    Location
    San Diego, California
    Posts
    49

    Re: Dir() with spaces??

    Thanks for the help!

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