CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: Saving Problem

  1. #1
    Join Date
    Oct 2001
    Location
    Tennessee
    Posts
    33

    Saving Problem

    When I run the following code, it tries to write to files which aren't folders. I only want it to save the file in the folders under that directory. Help would be appreciated.
    _____________________

    option Explicit
    private Sub Command1_Click()
    Dim strFolder as string
    Dim strtPoint as string
    strtPoint = "C:\My Documents"
    strFolder = Dir(strtPoint & "\*.*", vbDirectory)
    Do
    If strFolder <> "." And strFolder <> ".." then ' bypass system folders
    If GetAttr(strtPoint & "\" & strFolder) And vbDirectory = vbDirectory then ' This a Directory
    Open strtPoint & "\" & strFolder & "\title.txt" for Output as #1
    print #1, "Message"
    Close #1
    End If
    End If
    strFolder = Dir ' get next in chain
    Loop Until strFolder = "" ' do it again
    End Sub





  2. #2
    Join Date
    Nov 2000
    Location
    Chicago
    Posts
    51

    Re: Saving Problem

    Here is the problem

    If (GetAttr(strtPoint & "\" & strFolder) And vbDirectory) = vbDirectory then ' This a Directory


    Hope this helps

    TB_Guy_2000

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

    Re: Saving Problem

    What I see is that any folder that is a subFolder of "My Documents" will end up with a one line file called "Title.txt"

    Where do you want "Title.txt" to end up at and how many copies do you want?
    On my System the "My DOcuments" has three subfolders called "My Picturees", "My eBooks" and "My Music". All tree ended up with that file in it.

    John G

  4. #4
    Join Date
    Oct 2001
    Location
    Tennessee
    Posts
    33

    Re: Saving Problem

    That's exactly what I wanted except now I have a new problem. I need to be able to have more than one directory (ex. My Documents and Misc Folder). Here's my code again.
    __________

    option Explicit
    private Sub Command1_Click()
    Dim strFolder as string
    Dim strtPoint as string
    strtPoint = "C:\Directory"
    strFolder = Dir(strtPoint & "\*.*", vbDirectory)
    Do
    If strFolder <> "." And strFolder <> ".." then ' bypass system folders
    If (GetAttr(strtPoint & "\" & strFolder) And vbDirectory) = vbDirectory then ' This a Directory
    Open strtPoint & "\" & strFolder & "\file.txt" for Output as #1
    print #1, "Text"
    Close #1
    End If
    End If
    strFolder = Dir ' get next in chain
    Loop Until strFolder = "" ' do it again
    End Sub





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

    Re: Saving Problem

    Simply put the code into a subroutine then call the subroutine from Command1_Click passing the name of the starting folder like so.

    option Explicit
    '
    private Sub Command1_Click()
    SaveToFolder "C:\My Documents"
    SaveToFolder "C:\Misc Folder"
    End Sub
    '
    public Sub SaveToFolder(strtPoint as string)
    '
    Dim strFolder as string
    strFolder = Dir(strtPoint & "\*.*", vbDirectory)
    Do
    If strFolder <> "." And strFolder <> ".." then ' bypass system folders
    If GetAttr(strtPoint & "\" & strFolder) And vbDirectory = vbDirectory then ' This a Directory
    Open strtPoint & "\" & strFolder & "\title.txt" for Output as #1
    print #1, "Message"
    Close #1
    End If
    End If
    strFolder = Dir ' get next in chain
    Loop Until strFolder = "" ' do it again
    End Sub




    John G

  6. #6
    Join Date
    Sep 2001
    Location
    Montreal Canada
    Posts
    1,080

    Re: Saving Problem


    If GetAttr(strtPoint & "\" & strFolder) And
    vbDirectory = vbDirectory then ' This a Directory




    The second part of this condition seems useless.
    VbDirectory always equals vbDirectory

    Nicolas Bohemier

  7. #7
    Join Date
    Oct 2001
    Location
    Tennessee
    Posts
    33

    Re: Saving Problem

    How can I make it so that the program will write to system folders?


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

    Re: Saving Problem

    Slight error in my sample. Relplace similiar line with this one. See positioning of right parenthesis

    If GetAttr(strtPoint & "\" & strFolder And vbDirectory) = vbDirectory then ' This a Directory




    John G

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

    Re: Saving Problem

    Misplaced Right Parenthesis. It should read

    If GetAttr(strtPoint & "\" & strFolder) And vbDirectory = vbDirectory then ' This a Directory



    THanks for pointing that out

    John G

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

    Re: Saving Problem

    A correction to my correction.

    If GetAttr(strtPoint & "\" & strFolder And vbDirectory) = vbDirectory then ' This a Directory




    John G

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

    Re: Saving Problem

    There seems to be some confusion regarding positioning of parenthesis in my original post so here is is again with some clarification (Additional left - right parenthesis). The original post is correct. It takes the output of the GetAttr function "ands" the result with vbDirectory then compares the results with vbDirectory.
    Disregard my corrections and corrections to the corrections.

    option Explicit
    '
    private Sub Command1_Click()
    SaveToFolder "C:\My Documents"
    End Sub
    '
    public Sub SaveToFolder(strtPoint as string)
    '
    Dim strFolder as string
    strFolder = Dir(strtPoint & "\*.*", vbDirectory)
    Do
    If strFolder <> "." And strFolder <> ".." then ' bypass system folders
    If (GetAttr(strtPoint & "\" & strFolder) And vbDirectory) = vbDirectory then ' This a Directory
    Open strtPoint & "\" & strFolder & "\title.txt" for Output as #1
    print #1, "Message"
    Close #1
    End If
    End If
    strFolder = Dir ' get next in chain
    Loop Until strFolder = "" ' do it again
    End Sub





    John G

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

    Re: Saving Problem

    I know of no reason why you can't read to a system folder unless they are password protected or are made readonly for some reason.
    I am not familiar with Windows NT or Windows 2000 so if you are using those I can not help.

    John G

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