Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Asking Advice

4 views
Skip to first unread message

LondonLad

unread,
Dec 3, 2009, 12:46:02 PM12/3/09
to

Hi
I have written a program that requires 3 folders to be created in the root
dir.

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

Bob Butler

unread,
Dec 3, 2009, 2:09:53 PM12/3/09
to

"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

Faraz Azhar

unread,
Dec 3, 2009, 2:14:11 PM12/3/09
to
On Dec 3, 10:46 pm, LondonLad <London...@discussions.microsoft.com>
wrote:

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.

MikeD

unread,
Dec 3, 2009, 8:20:26 PM12/3/09
to

"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


LondonLad

unread,
Dec 4, 2009, 5:19:01 AM12/4/09
to
Hi to All
First let me explain why i felt I needed the folders. I wrote the program
because when I download all my photo's from my camera I get 2 types JPG's and
Raw, these are mixed in folders by subject and location of the shoot, I want
to get the Raw Photo's split into batches by the same criteria (subject and
location) and back them up.
After I download from the camera, I run my program this moves the download
to a temp Folder(1) splits the JPG's/Raw into Folders(2 & 3) and backs up the
Raw. I then manually move Folders(2 & 3) to where I want them. all folders
now empty.

OK no to the root dir, I will have to decide which drive to use, ensure it
exists. Say I choose C:\ and it does not exists whats the best way to cover
that possibility? I have 2 friends who are showing interest so I want to get
the best solution possible.

Ron

"MikeD" wrote:

> .
>

Dee Earley

unread,
Dec 4, 2009, 5:26:00 AM12/4/09
to
On 04/12/2009 10:19, LondonLad wrote:
> Hi to All
> First let me explain why i felt I needed the folders. I wrote the program
> because when I download all my photo's from my camera I get 2 types JPG's and
> Raw, these are mixed in folders by subject and location of the shoot, I want
> to get the Raw Photo's split into batches by the same criteria (subject and
> location) and back them up.
> After I download from the camera, I run my program this moves the download
> to a temp Folder(1) splits the JPG's/Raw into Folders(2& 3) and backs up the
> Raw. I then manually move Folders(2& 3) to where I want them. all folders

> now empty.
>
> OK no to the root dir, I will have to decide which drive to use, ensure it
> exists. Say I choose C:\ and it does not exists whats the best way to cover
> that possibility? I have 2 friends who are showing interest so I want to get
> the best solution possible.

If they are temporary, use the temporary folder.
Simples.

--
Dee Earley (dee.e...@icode.co.uk)
i-Catcher Development Team

iCode Systems

Nobody

unread,
Dec 4, 2009, 7:34:06 AM12/4/09
to
Use the temp folder. Below is the code I am using. It calls GetTempPath()
API function, then makes sure that the folder exists or creates it, as it
doesn't necessarily exist(See MSDN for details). Just call GetTempDir() at
start up, and put the value in a global variable, and use that variable.

' 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


LondonLad

unread,
Dec 4, 2009, 9:03:01 AM12/4/09
to
Hi Nobody
Thanks for that, I will create a test prog. and see how I get on.
May need more help.

Ron

"Nobody" wrote:

> .
>

Larry Serflaten

unread,
Dec 4, 2009, 9:26:34 AM12/4/09
to
On Dec 4, 6:34 am, "Nobody" <nob...@nobody.com> wrote:
> Use the temp folder. Below is the code I am using. It calls GetTempPath()
> API function, then makes sure that the folder exists or creates it, as it
> doesn't necessarily exist(See MSDN for details). Just call GetTempDir() at
> start up, and put the value in a global variable, and use that variable.
>
> ' Form1 code:
>
> Option Explicit
>
> Private Sub Form_Load()
>     sTempDir = GetTempDir()
>     Debug.Print sTempDir
> End Sub


FWIW: I get the same thing from:

Debug.Print Environ("TEMP")

LFS

C. Kevin Provance

unread,
Dec 4, 2009, 9:55:22 AM12/4/09
to
"Larry Serflaten" <serf...@gmail.com> wrote in message
news:83f0d6e2-03e7-4675...@z41g2000yqz.googlegroups.com...

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>


mayayana

unread,
Dec 4, 2009, 10:10:44 AM12/4/09
to

> OK no to the root dir, I will have to decide which drive to use, ensure it
> exists. Say I choose C:\ and it does not exists whats the best way to
cover
> that possibility? I have 2 friends who are showing interest so I want to
get
> the best solution possible.
>

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.


LondonLad

unread,
Dec 4, 2009, 11:21:01 AM12/4/09
to
Hi Nobody
OK I have tested the sample code you gave me and I see what its doing at the
moment I am using 3 folders should I then create 3 temp folders or are you
saying for the other movements to use a global variable?
Not sure how I would get the data back, or how it would display in the
backup folder.
Can you expand further please.

Ron

Nobody

unread,
Dec 4, 2009, 1:06:38 PM12/4/09
to
"LondonLad" <Lond...@discussions.microsoft.com> wrote in message
news:55721618-F503-4A10...@microsoft.com...

> Hi Nobody
> OK I have tested the sample code you gave me and I see what its doing at
> the
> moment I am using 3 folders should I then create 3 temp folders or are you
> saying for the other movements to use a global variable?
> Not sure how I would get the data back, or how it would display in the
> backup folder.
> Can you expand further please.

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"

Nobody

unread,
Dec 4, 2009, 1:10:42 PM12/4/09
to
"Larry Serflaten" <serf...@gmail.com> wrote in message
news:83f0d6e2-03e7-4675...@z41g2000yqz.googlegroups.com...
> FWIW: I get the same thing from:
>
> Debug.Print Environ("TEMP")

<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


LondonLad

unread,
Dec 5, 2009, 3:53:01 AM12/5/09
to
Hi to All

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:

> .
>

Karl E. Peterson

unread,
Dec 7, 2009, 6:50:01 PM12/7/09
to
Nobody explained on 12/4/2009 :

> Use the temp folder. Below is the code I am using. It calls GetTempPath() API
> function, then makes sure that the folder exists or creates it, as it doesn't
> necessarily exist(See MSDN for details).

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!]


Nobody

unread,
Dec 7, 2009, 8:08:51 PM12/7/09
to
"Karl E. Peterson" <ka...@exmvps.org> wrote in message
news:eM7k$f5dKH...@TK2MSFTNGP06.phx.gbl...

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.


Karl E. Peterson

unread,
Dec 7, 2009, 8:21:31 PM12/7/09
to
Nobody has brought this to us :
> "Karl E. Peterson" <ka...@exmvps.org> wrote ...

>> Nobody explained on 12/4/2009 :
>>> Use the temp folder. Below is the code I am using. It calls GetTempPath()
>>> API function, then makes sure that the folder exists or creates it, as it
>>> doesn't necessarily exist(See MSDN for details).
>>
>> 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?
>
> 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+.

Ralph

unread,
Dec 7, 2009, 9:21:44 PM12/7/09
to

"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


Karl E. Peterson

unread,
Dec 7, 2009, 9:40:17 PM12/7/09
to
Ralph expressed precisely :
> "Karl E. Peterson" <ka...@exmvps.org> wrote ...

>> Nobody explained on 12/4/2009 :
>>> Use the temp folder. Below is the code I am using. It calls GetTempPath()
>>> API function, then makes sure that the folder exists or creates it, as it
>>> doesn't necessarily exist(See MSDN for details).
>>
>> 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?
>
> 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.

But what if the user changes the e-var to point at a non-existant
location?

Nobody

unread,
Dec 7, 2009, 11:20:41 PM12/7/09
to
"Karl E. Peterson" <ka...@exmvps.org> wrote in message
news:%23mMcIT6...@TK2MSFTNGP02.phx.gbl...

> I think it'll work on 98 natively, though, and 95 if IE5+ is installed. It
> requires Shell v5.00+.

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.

Karl E. Peterson

unread,
Dec 8, 2009, 7:04:23 PM12/8/09
to

It happens that Nobody formulated :

> "Karl E. Peterson" <ka...@exmvps.org> wrote in message
> news:%23mMcIT6...@TK2MSFTNGP02.phx.gbl...
>> I think it'll work on 98 natively, though, and 95 if IE5+ is installed. It
>> requires Shell v5.00+.
>
> 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.

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?

Bob Butler

unread,
Dec 8, 2009, 7:33:55 PM12/8/09
to

"Karl E. Peterson" <ka...@exmvps.org> wrote in message
news:u643rMGe...@TK2MSFTNGP02.phx.gbl...
<cut>

> Okay, gonna toss this one out to see who can shoot it down quickest...
>
> Public Function MkDirs(ByVal Folder As String) As Boolean

maybe...

MkDirs \\server\share\subfolder


Karl E. Peterson

unread,
Dec 8, 2009, 7:56:04 PM12/8/09
to
Bob Butler has brought this to us :
> "Karl E. Peterson" <ka...@exmvps.org> wrote ...

> <cut>
>> Okay, gonna toss this one out to see who can shoot it down quickest...
>>
>> Public Function MkDirs(ByVal Folder As String) As Boolean
>
> 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

--

Nobody

unread,
Dec 8, 2009, 7:59:57 PM12/8/09
to
"Karl E. Peterson" <ka...@exmvps.org> wrote in message
news:u643rMGe...@TK2MSFTNGP02.phx.gbl...

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

Karl E. Peterson

unread,
Dec 8, 2009, 8:21:12 PM12/8/09
to
Nobody formulated on Tuesday :

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.

Nobody

unread,
Feb 22, 2010, 12:21:01 PM2/22/10
to

"Nobody" <nob...@nobody.com> wrote in message
news:OjWy0rGe...@TK2MSFTNGP04.phx.gbl...


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


0 new messages