I have a choice do I allow the user to choose where to load the program and
if yes ensure that the folders are created on the correct drive.
Block all use until the folders exists.
Create the folders each time the program runs, also delete same at end.
Or simple error trap the program and let the user sort out the problem.
I would be very grateful from you experienced programmers as to how to
approach the decision.
Thanks in anticipation.
Ron
"LondonLad" <Lond...@discussions.microsoft.com> wrote in message
news:4B1AB430-6BC3-4E5B...@microsoft.com...
Refactor the program to not require folders created off the root
If creating and deleting folders at end is also an option for you,
means you're doing some temporary working in those 3 folders. Why
don't you create those three folders inside the Windows' Temporary
folder ? Users dont usually go over there, and user has the option to
clean up the temporary files using Disk Cleanup in Windows.
"LondonLad" <Lond...@discussions.microsoft.com> wrote in message
news:4B1AB430-6BC3-4E5B...@microsoft.com...
> Hi
> I have written a program that requires 3 folders to be created in the root
> dir.
Why in the root? This is a BAD design. For one, I *think* the root folder
(at least of the drive Windows is installed on) is off limits for creating
and deleting folders to standard users. I could be wrong about that. But
regardless, it's still not a good idea when there are more appropriate
folders to use as the "root" for your folders.
> I have a choice do I allow the user to choose where to load the program
> and
> if yes ensure that the folders are created on the correct drive.
No. Except that if by allowing the user to choose where to load the program
you mean allowing the choose where to INSTALL your program, then yes for
that part only.
> Block all use until the folders exists.
No.
> Create the folders each time the program runs, also delete same at end.
No.
> Or simple error trap the program and let the user sort out the problem.
No.
> I would be very grateful from you experienced programmers as to how to
> approach the decision.
I'm curious why you need to create folders at all. You said one of your
options was to delete the folders. So this must mean whatever you're doing
with these folders must only be temporary. Assuming you're creating files in
these folders, I agree with Faraz. You should create the files in the
user's Temp folder. If you really need to create the folders too in Temp, I
guess go ahead. But I really can't see the need for that either.
Perhaps you can explain the purpose of creating these folders. That might
help us give you better answers. But based on what you've said, I don't
think ANY of the options you’ve listed are good ones, nor is it a good idea
to be creating these folders in the root (as Bob basically said).
--
Mike
Ron
"MikeD" wrote:
> .
>
If they are temporary, use the temporary folder.
Simples.
--
Dee Earley (dee.e...@icode.co.uk)
i-Catcher Development Team
iCode Systems
' Form1 code:
Option Explicit
Private Sub Form_Load()
sTempDir = GetTempDir()
Debug.Print sTempDir
End Sub
' Module1 code:
Option Explicit
Public Const MAX_PATH As Long = 260
Public Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" ( _
ByVal nSize As Long, ByVal lpBuffer As String) As Long
Public sTempDir As String
' Returns the Temp folder. Always ends with "\". Created if necessary.
Public Function GetTempDir() As String
Dim tmp As String
Dim TempPathLen As Long
Dim ResumeNext As Boolean
Dim ErrNumber As Long
On Error GoTo GetTempDir_Error
tmp = String(MAX_PATH, 0)
TempPathLen = GetTempPath(Len(tmp), tmp)
tmp = Left(tmp, TempPathLen)
If Not IsPathExist(tmp) Then
' Sometimes the folder does not exist
ResumeNext = True
MkDir tmp
ResumeNext = False
End If
If Right(tmp, 1) <> "\" Then
tmp = tmp & "\"
End If
GetTempDir = tmp
ExitSub:
Exit Function
GetTempDir_Error:
ErrNumber = Err.Number
If ResumeNext Then
Resume Next
Else
MsgBox "GetTempDir: Error " & ErrNumber & ": " & Err.Description
Resume ExitSub
End If
End Function
Public Function IsPathExist(Path As String) As Boolean
Dim ret As Long
Dim ResumeNext As Boolean
Dim ErrNumber As Long
On Error GoTo IsPathExist_Error
ResumeNext = True
ret = GetAttr(Path) And vbDirectory
ResumeNext = False
If ErrNumber = 0 Then
IsPathExist = True
End If
ExitSub:
Exit Function
IsPathExist_Error:
ErrNumber = Err.Number
If ResumeNext Then
Resume Next
Else
MsgBox "GetTempDir: Error " & ErrNumber & ": " & Err.Description
Resume ExitSub
End If
End Function
Ron
"Nobody" wrote:
> .
>
FWIW: I get the same thing from:
Debug.Print Environ("TEMP")
LFS
FWIW: I get the same thing from:
Debug.Print Environ("TEMP")
===========
Indeed, but I've learned never to assume that variable is always set, or set
properly. Occasionally and end user turns up who thinks he knows what he's
doing when he fiddles with his system when it just isn't so. <g>
I would use the TEMP folder AND provide a setting
to change that. On systems with multiple partitions it
can be preferable to assign a spot on another drive.
I've found this myself with CD/DVD writing. If writer
software puts its cache in TEMP and provides no other
option then C drive needs to be several GB bigger just
to accomodate that occasional cache.
Ron
Yes, create the folders under the Temp folder. Example:
sTempDir = GetTempDir()
Debug.Print sTempDir & "SubFolder1"
' Create all folders
MkDir sTempDir & "SubFolder1"
MkDir sTempDir & "SubFolder2"
MkDir sTempDir & "SubFolder3"
' Do what you want here
' Delete everything
Kill sTempDir & "SubFolder1\*.*"
RmDir sTempDir & "SubFolder1"
Kill sTempDir & "SubFolder2\*.*"
RmDir sTempDir & "SubFolder2"
Kill sTempDir & "SubFolder3\*.*"
RmDir sTempDir & "SubFolder3"
<Quoting fixed>
GetTempPath() looks for TMP first, then TEMP. In either case, you have to
create the folder if doesn't already exist.
GetTempPath Function
http://msdn.microsoft.com/en-us/library/aa364992(VS.85).aspx
Thanks for your input on this post, and thank you to Nobody for the code, I
will need to change my code now to reflect the new approach.
Good lesson learned.
Thanks again.
Ron
"Nobody" wrote:
> .
>
Jeez, I don't think I ever a) noticed that or b) ran into a system
where it didn't exist. Ouch.
That said, wouldn't you want a recursive mkdir for that? Looks like
yours assumes the tempdir's parent exists as well?
--
[.NET: It's About Trust!]
You are right. SHCreateDirectoryEx(ME/2000+) can create a directory tree. In
VB, you have to use InStrRev() or equivalent and test if the path exists.
Yeah, this is how I worked that one out some time ago:
Private Declare Function SHCreateDirectoryExW Lib "shell32" (ByVal
hWnd As Long, ByVal lpszPath As Long, ByVal lpSA As Long) As Long
Public Function PathMakeDirs(ByVal Path As String) As Boolean
Dim nRet As Long
' Potential error codes.
Const ERROR_SUCCESS As Long = 0&
Const ERROR_FILENAME_EXCED_RANGE As Long = 206&
Const ERROR_BAD_PATHNAME As Long = 161&
Const ERROR_PATH_NOT_FOUND As Long = 3&
Const ERROR_FILE_EXISTS As Long = 80&
Const ERROR_ALREADY_EXISTS As Long = 183&
Const ERROR_CANCELLED As Long = 1223&
' Requires v5.0 of Shell32.dll...
nRet = SHCreateDirectoryExW(0&, StrPtr(Path), 0&)
' Treat "already exists" as success...
PathMakeDirs = _
(nRet = ERROR_SUCCESS) Or _
(nRet = ERROR_FILE_EXISTS) Or _
(nRet = ERROR_ALREADY_EXISTS)
End Function
I think it'll work on 98 natively, though, and 95 if IE5+ is installed.
It requires Shell v5.00+.
"Karl E. Peterson" <ka...@exmvps.org> wrote in message
news:eM7k$f5dKH...@TK2MSFTNGP06.phx.gbl...
If your talking about the "Temp" folder, Windows won't boot without one. You
can redirect it, but there has to be one.
Damn tough to delete it too. It always seems to have a file in use.
-ralph
But what if the user changes the e-var to point at a non-existant
location?
No. IE5 doesn't update Shell32.dll. See "5.0 Shell32.dll" row here and Note
3:
Shell and Common Controls Versions
http://msdn.microsoft.com/en-us/library/bb776779(VS.85).aspx
The notes talk about several DLL's, which makes it confusing to read. In
this case, only look for what it says about Shell32.dll, which is where that
function is implemented. Anyway, one could easily make a For loop scanning
backward and look for "\" and use GetAttr() and MkDir to make a pure VB
solution.
Hmmmm, you're right. I was thinking shlwapi. <grumble>
> Anyway, one could easily make a For loop scanning
> backward and look for "\" and use GetAttr() and MkDir to make a pure VB
> solution.
Okay, gonna toss this one out to see who can shoot it down quickest...
Public Function MkDirs(ByVal Folder As String) As Boolean
Dim f() As String
Dim attr As Long
Dim i As Long
' Split incoming folder into subfolders.
f = Split(Folder, "\")
For i = 1 To UBound(f)
f(i) = f(i - 1) & "\" & f(i)
Next i
On Error Resume Next
For i = 0 To UBound(f)
' Check if this level already exists.
attr = GetAttr(f(i))
If Err.Number Then
' Folder likely doesn't exist,
' clear error and create.
Err.Clear
MkDir f(i)
If Err.Number Then Exit For
End If
Next i
' Return success?
MkDirs = CBool(GetAttr(Folder) And vbDirectory)
End Function
I've sure seen a lot of these over the years, but I guess never saw one
I liked enough to bother hanging onto. Anyone see a scenario this one
wouldn't handle?
maybe...
MkDirs \\server\share\subfolder
Saw that too. Slight modification...
Public Function MkDirs(ByVal Folder As String) As Boolean
Dim f() As String
Dim attr As Long
Dim first As Long
Dim i As Long
' Split incoming folder into subfolders.
f = Split(Folder, "\")
For i = 1 To UBound(f)
f(i) = f(i - 1) & "\" & f(i)
Next i
' If the input path is UNC, the first element
' will be empty and the second "\", so we need
' to adjust where we start creating folders.
If f(0) = "" And UBound(f) > 0 Then
If f(1) = "\" Then
first = 4 'fourth element is first path.
End If
End If
' Use errors to signal need to take action.
On Error Resume Next
For i = first To UBound(f)
' Check if this level already exists.
attr = GetAttr(f(i))
If Err.Number Then
' Folder likely doesn't exist,
' clear error and create.
Err.Clear
MkDir f(i)
If Err.Number Then Exit For
End If
Next i
' Return success?
MkDirs = CBool(GetAttr(Folder) And vbDirectory)
End Function
--
I prefer to use a for loop instead of Split. Example air code:
Public Function MkDirs(ByVal Folder As String) As Boolean
Dim attr As Long
Dim i As Long
Dim iStart As Long
iStart =1 ' Start from the first character
If Left(Folder, 2) = "\\" Then
' The folder is a share, skip to the third character
iStart = 3
End If
On Error Resume Next
For i = iStart To Len(Folder)
If Mid(Folder, i, 1) = "\" Then
attr = (GetAttr(Left(Folder, i)) And vbDirectory)
If Err.Number <> 0 Then
' Folder likely doesn't exist,
' clear error and create.
Err.Clear
MkDir Left(Folder, i)
If Err.Number Then Exit For
End If
End If
Next
Yeah, that's another approach. I think you'd need to be a bit more
rigorous with the UNC stuff, though. Probably skipping ahead to the
end of the sharename before proceeding to mkdirs.
My version of the code doesn't check or create the last folder if the last
character in the path isn't "\", so change this line:
If Mid(Folder, i, 1) = "\" Then
To:
If Mid(Folder, i, 1) = "\" Or i = Len(Folder) Then