PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] MkDir Path/File Access Error-VBForums
Results 1 to 12 of 12

Thread: [RESOLVED] MkDir Path/File Access Error

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Location
    Chicago
    Posts
    136

    Resolved [RESOLVED] MkDir Path/File Access Error

    I'm using the following code to make a directory if not already created:

    Code:
            If fso.FileExists(strUserPath & "\AppData\") Then
            Else
                MkDir (strUserPath & "\AppData\")
            End If
    strUserPath = %UserProfile%

    The problem I'm running into is that when the directory exists (%UserProfile%\AppData\) the code is still proceeding to attempt to MkDir and generates an error (Run-time error 75; Path\File Access error) . What am I missing here?
    CodeBank: Launch IE

  2. #2
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    Re: MkDir Path/File Access Error

    Check to see if it exists first before you run the code
    Code:
    If Dir$("c:\hack", vbDirectory) = vbNullString Then
         'directory does not exist so make it
    End If
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  3. #3

  4. #4
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    Re: MkDir Path/File Access Error

    And you don't need the Else if there is no condition for which it applies. If you variable contains the full path, as RhinoBull indicated, then you can do
    Code:
    If Not fso.FileExists(strUserPath & "\AppData\") Then        
       MkDir (strUserPath & "\AppData\")
    End If
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  5. #5
    PowerPoster isnoend07's Avatar
    Join Date
    Feb 2007
    Posts
    3,237

    Re: MkDir Path/File Access Error

    with out using FSO
    If Dir("c:\test", vbDirectory + vbHidden) = "" Then MkDir "c:\test"

  6. #6
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,135

    Re: MkDir Path/File Access Error

    I think he's trying to use enviroment variable - if so then try this instead:
    Code:
    Dim strUserPath As String
    
    strUserPath = Environ("USERPROFILE")
    
    If Not fso.FileExists(strUserPath & "\AppData\") Then        
       MkDir (strUserPath & "\AppData\")
    End If
    Also, if you need to get list of all environment variables then run this simple loop.
    NOTE that list will vary from system to system:
    Code:
    Private Sub Command1_Click()
    Dim i%
    
        Do
            i = i + 1
            Debug.Print Environ(i)
        Loop Until Environ(i) = ""
    
    End Sub

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Location
    Chicago
    Posts
    136

    Re: MkDir Path/File Access Error

    Yes I am using environ. Sorry, should have posted my full code...

    Code:
        Dim strUserPath As String
        Dim strSource As String
        Dim strDest As String
        Dim fso As Object
        Dim strDestDir As String
            
        'Copy WebICA to UserProfile
        strUserPath = Environ("UserProfile")
        strSource = App.Path & "\webica.ini"
        strDest = strUserPath & "\AppData\Roaming\ICAClient\webica.ini"
        strDestDir = strUserPath & "\AppData\Roaming\ICAClient\"
            
        Set fso = CreateObject("scripting.filesystemobject")
        
        If fso.FileExists(strDestDir) Then
            fso.copyfile strSource, strDest
        Else
            If fso.FileExists(strUserPath & "\AppData\") Then
            Else
                MkDir (strUserPath & "\AppData\")
            End If
            
            If fso.FileExists(strUserPath & "\AppData\Roaming\") Then
            Else
                MkDir (strUserPath & "\AppData\Roaming\")
            End If
            
            If fso.FileExists(strUserPath & "\AppData\Roaming\ICAClient\") Then
            Else
                MkDir (strUserPath & "\AppData\Roaming\ICAClient\")
            End If
            
            fso.copyfile strSource, strDest
            
        End If
    On my particular PC strUserPath = "d:\documents and settings\user". Although I'm still unsure of why it is failing to recognize already existing directories.
    CodeBank: Launch IE

  8. #8
    Lively Member
    Join Date
    Jul 2007
    Posts
    98

    Re: MkDir Path/File Access Error

    Hei people how can this work, when Fso.FileExists is called with folder as an argument?

  9. #9
    Hyperactive Member csKanna's Avatar
    Join Date
    Dec 2005
    Location
    Tech-Tips-Now.com
    Posts
    339

    Re: MkDir Path/File Access Error

    You can simply use the API function as shown below:
    vb Code:
    1. Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" _
    2.     (ByVal lpPath As String) As Long
    3.  
    4. Sub CreateDirectories()
    5.     'This will create the directory "c:\this\is\a\test\directory\", if it doesn't exist already
    6.     MakeSureDirectoryPathExists "c:\this\is\a\test\directory\"
    7. End Sub
    Kanna

  10. #10
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,335

    Re: MkDir Path/File Access Error

    Quote Originally Posted by tatkosmurff
    Hei people how can this work, when Fso.FileExists is called with folder as an argument?
    Be a bit more specific. What are you trying to do?
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  11. #11
    Lively Member
    Join Date
    Jul 2007
    Posts
    98

    Re: MkDir Path/File Access Error

    Code:
    Private Sub Command1_Click()
        Dim l_objFso As FileSystemObject
        Dim l_strFolderPath As String
        
        l_strFolderPath = "Enter folder path here"
        
        Set l_objFso = CreateObject("Scripting.FileSystemObject")
        
        If Not l_objFso.FolderExists(l_strFolderPath) Then
            Call l_objFso.CreateFolder(l_strFolderPath)
        End If
        
        Set l_objFso = Nothing
    End Sub
    The folder path can be specified with end "\"(C:\Folder1\Folder2\) or without
    "\"(C:\Folder1\Folder2) it will work both ways

    The danger with this code lies in the folder path. For example
    l_strFolderPath = C:\Folder1\Folder2
    if Folder1 exists and Folder2 does not exist Folder2 will be created.
    if Folder2 does not exist an error will be raised.
    A sample solution of this problem follows

    Code:
    Public Sub CheckFolders(s_strDestination As String, s_strDestPath As String)
        Dim i As Integer, k As Integer, j As Integer
        Dim l_strData As String
        Dim l_strWork As String
        Dim l_strFolder() As String
        
        k = 0
        l_strData = s_strDestination
        If Not m_objFso.FolderExists(s_strDestPath) Then
            Call m_objFso.CreateFolder(s_strDestPath)
        End If
        
        Do While Len(l_strData) <> 0
            i = InStr(1, l_strData, "\")
            If i <> 0 And i <> 1 Then
                ReDim Preserve l_strFolder(k)
                l_strFolder(k) = Mid(l_strData, 1, i - 1)
                l_strData = Mid(l_strData, i + 1)
                
                l_strWork = s_strDestPath & "\"
                For j = 0 To k
                     l_strWork = l_strWork & l_strFolder(j) & "\"
                Next j
                
                l_strWork = Mid(l_strWork, 1, Len(l_strWork) - 1)
                If Not m_objFso.FolderExists(l_strWork) Then
                    Call m_objFso.CreateFolder(l_strWork)
                End If
                
                k = k + 1
            ElseIf i = 1 Then
                l_strData = Mid(l_strData, 2)
            End If
        Loop
    End Sub
    In the code above:
    s_strDestination As String: a main folder where the process is about to be executed. This path must contain no subfolders (Example-> C:\Test\)
    s_strDestPath As String : a sub folders path. This is the exact location where the file must be copied(\Data\Test1\Test2.....). The exact location is a function of the main folder. The output of the sub will be:
    1. create the main folder if not created
    2. create the first subfolder in the main folder
    3. creare the second subfolder in the first subfolder
    4. ......
    5. ......
    6. Copy file in the last subfolder.

    I use this code in a self- made installation manager, and it works fine

    I am really wondering why this problem needs so complicated solutions as shown above from the colleges . Doing it the difficult way is interesting, but the simpler it is the better it is- no bugs/ less coding etc.
    Last edited by tatkosmurff; Jan 30th, 2008 at 08:42 AM.

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Mar 2005
    Location
    Chicago
    Posts
    136

    Re: MkDir Path/File Access Error

    Quote Originally Posted by tatkosmurff
    Hei people how can this work, when Fso.FileExists is called with folder as an argument?


    Oops...

    I had reused .FileExists from when I was originally just trying to copy the file. Then I realized the directory structure might not exist as well. Switching to .FolderExists solved it. Thanks all!
    CodeBank: Launch IE

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width