|
-
Jan 29th, 2008, 02:01 PM
#1
Thread Starter
Addicted Member
[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?
-
Jan 29th, 2008, 02:03 PM
#2
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
-
Jan 29th, 2008, 02:20 PM
#3
Re: MkDir Path/File Access Error
Your variable (strUserPath) must contain full path.
-
Jan 29th, 2008, 02:27 PM
#4
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
-
Jan 29th, 2008, 02:40 PM
#5
Re: MkDir Path/File Access Error
with out using FSO
If Dir("c:\test", vbDirectory + vbHidden) = "" Then MkDir "c:\test"
-
Jan 29th, 2008, 02:43 PM
#6
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
-
Jan 29th, 2008, 03:20 PM
#7
Thread Starter
Addicted Member
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.
-
Jan 29th, 2008, 05:07 PM
#8
Lively Member
Re: MkDir Path/File Access Error
Hei people how can this work, when Fso.FileExists is called with folder as an argument?
-
Jan 29th, 2008, 05:20 PM
#9
Hyperactive Member
Re: MkDir Path/File Access Error
You can simply use the API function as shown below:
vb Code:
Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" _ (ByVal lpPath As String) As Long Sub CreateDirectories() 'This will create the directory "c:\this\is\a\test\directory\", if it doesn't exist already MakeSureDirectoryPathExists "c:\this\is\a\test\directory\" End Sub
-
Jan 30th, 2008, 07:53 AM
#10
Re: MkDir Path/File Access Error
 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?
-
Jan 30th, 2008, 08:30 AM
#11
Lively Member
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.
-
Jan 30th, 2008, 01:06 PM
#12
Thread Starter
Addicted Member
Re: MkDir Path/File Access Error
 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!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|