Results 1 to 8 of 8

Thread: How do I create a directory and file if it doesn't exist?

  1. #1
    Guest
    I am writing a program that will save data to a sequential file. Everything works just fine if the user has the directory and the file in that directory on their computer already, but how do I create the directory and file if it doesn't exist? I want to check on the form load event to see if there is one there, and if not, create the folder, and file(blank). If it does exist, then I want to do nothing. I would like to do everything from the same form, no modules, and no api calls. Any help? I can't seem to find this. It is probably some real simple commands....

  2. #2
    Lively Member
    Join Date
    Jan 1999
    Posts
    81

    Here try this

    if dir(c:\file\file.txt) = "" then 'Looks for file
    a = shell("mkdir file")

    Open "c:\file\file.txt" For Output As #12
    Print #12, " "
    close #12
    end if


    what it does is looks for the file if its not there creates the dir then makes a blank file.



    Hope it helps
    Brooke Hostmeyer
    -------------------------
    There is never only one right answer. That is the magic of programming.
    -------------------------

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221

    mkdir statement is available in vb

    Code:
    MkDir path
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4
    Addicted Member jcouture100's Avatar
    Join Date
    Aug 1999
    Posts
    141

    Talking

    You might want to look into using the Microsoft Scripting Runtime to access the FileSystemObject. It really provides
    a lot of easy to use methods for this sort of thing.

    You can add a reference to this by first going to the Project Menu then References menu and adding a reference to Microsoft Scripting Runtime.

    Here's an example of doing something like this...
    Oops... I had some variables mistyped...
    this should work better.

    Hope it helps.

    Code:
    Private Sub CheckFolderAndFile()
        Dim fso As FileSystemObject
        Dim strFileName As String
        Dim blnFolder As Boolean
        Dim blnFile As Boolean
        Dim strFolder As String
        Dim strFile As String
        
        strFolder = "C:\111Temp\"
        strFile = "Junk.txt"
        
        'create a file scripting object
        Set fso = New FileSystemObject
        
        'Check if Folder exists
        'If folder doesn't exist then we know that the file
        'doesn't exist.
        blnFolder = fso.FolderExists(strFolder)
        If blnFolder = False Then
            'Create Folder
            fso.CreateFolder (strFolder)
            'Create Text File
            fso.CreateTextFile (strFolder & strFile)
        Else
            'Folder exists now check for file
            blnFile = fso.FileExists(strFolder & strFile)
            If blnFile = False Then
                'Create Text File
                fso.CreateTextFile (strFolder & strFile)
            End If
        End If
    End Sub
    [Edited by jcouture100 on 05-05-2000 at 03:38 PM]
    JC

  5. #5
    Guest
    jcourure100, no good. It didn't recognize FileSystemObject. Am I doing something wrong? I cut and pasted the code you gave (and added the correct folder and file names).


    BHostmeyer,
    I am getting some errors with yours too. I get the runtime error when it processes

    a = shell("mkdir file")

    Should I have changed anything in there, or is this what it was supposed to be?

    Thanks for your help, I do appreciate it, even if I can't make it work. lol

  6. #6
    Lively Member
    Join Date
    Aug 1999
    Posts
    89
    I think you haven't put the reference to the dll yet. Instead, may be you want to try the createobject("FileSystemObject.Scripting")...I think.

  7. #7
    Addicted Member
    Join Date
    Aug 1999
    Location
    Ottawa,ON,Canada
    Posts
    217

    Wink

    Wow, jcouture100, you really like the MS Scripting Runtime
    library, I've seen you suggest it on a few posts now. Sorry
    to tell you, but I don't see any reason to include a 149KB
    DLL that only wraps some of the functionality
    already built into VB's file system functions and the core
    system libraries (kernel32.dll to be exact). It's good for
    beginners learning how to use the file system, but you
    should move on as soon as you feel comfortable. I mean it
    doesn't allow Binary file processing or Random Access!
    Sorry to say it, but it's time to move on.

    Just my 2¢
    Dan PM
    Analyst Programmer

    VB6 SP3 (also VB4 16-bit sometimes )

  8. #8
    Addicted Member jcouture100's Avatar
    Join Date
    Aug 1999
    Posts
    141
    You're right about that, Dan. To be honest with you, I personally don't use the FSO and pretty much stick with the route you suggested. I'm rather addicted to API calls and try to use them whenever possible.

    But considering that HellsWraith is a beginner I thought it would be nice to not totally overwhelm him, but still give him something new to learn. Using the FSO has got to be better than Shelling out and running the MKDIR Command.

    HellsWraith, lychew is correct. It sounds like the reference to the FileSystemObject contained in the scrrun.dll. To set this reference, you need to do the following:

    Click on the PROJECT menu, then Click on the REFERENCES submenu. This will open up a window that lets you pick the various DLLs that you want referenced in your project. Scroll down until you find "Microsoft Scripting Runtime" and make sure the check box is checked. Click OK. Now the code I gave you earlier should work.

    If you're interested in the calls mentioned by Dan (SonGouki) I'll be glad to send them to you. They are actually very easy to implement and use.


    JC

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