Board index » Visual Studio » MKDir not working
|
Macey
|
|
Macey
|
MKDir not working
Visual Studio231
Can anyone tell me why this is not working, the directory does not get created. GetHomedir and ACK_ARCDIR are pre defined strings An example of DirPath would be C:\ACK\ACKTARC\210605\100 PathDate = Format(Date, "DDMMYY") DirPath = GetHomeDir & ACK_ARCDIR & "\" & PathDate & "\" & ourEFT.auth.t_num MkDir DirPath - |
| Bob
Registered User |
Tue Jun 21 11:31:34 CDT 2005
Re:MKDir not working
"David" <David@discussions.microsoft.com>wrote in message
QuoteCan anyone tell me why this is not working, the directory does not -- Reply to the group so all can participate VB.Net: "Fool me once..." - |
| Karl
Registered User |
Tue Jun 21 14:21:33 CDT 2005
Re:MKDir not working
David wrote:
QuoteCan anyone tell me why this is not working, the directory does not Tips... ================================================================= Create Nested Folders in One Call VB3 VB4 VB5 VB6 Beginner 1 6 - B i t 3 2 - B i t Suppose you need to create a tree of directories, all at once, in code. For example, you could create the tree C:\1stDir\2ndDir\3rdDir\4thDir with one call simply by feeding that path, as a string, into this procedure: Public Function MkDirs(ByVal PathIn As String) _ As Boolean Dim nPos As Long MkDirs = True 'assume success If Right$(PathIn, 1) <>"\" Then PathIn = _ PathIn + "\" nPos = InStr(1, PathIn, "\") Do While nPos>0 If Dir$(Left$(PathIn, nPos), _ vbDirectory) = "" Then On Error GoTo Failed MkDir Left$(PathIn, nPos) On Error GoTo 0 End If nPos = InStr(nPos + 1, PathIn, "\") Loop Exit Function Failed: MkDirs = False End Function If any part of the path already exists, the routine creates only the new part. This routine works on strings representing local and mapped drives-those with a letter, colon, and backslash at the beginning. If the drive designation is left out, the directories are created starting at the default directory on the current drive. Submitted by Frederick Rothstein, Trenton, New Jersey ================================================================= -- Working Without a .NET? classicvb.org/petition">classicvb.org/petition - |
| Rick
Registered User |
Tue Jun 21 16:05:43 CDT 2005
Re:MKDir not workingQuoteSubmitted by Frederick Rothstein, Trenton, New Jersey $25 (I think) per tip back then, I used my real name so it would be used on the check. Rick - MVP - |
| Rick
Registered User |
Tue Jun 21 16:15:06 CDT 2005
Re:MKDir not workingQuote>Submitted by Frederick Rothstein, Trenton, New Jersey how did you manage to find it? Rick - MVP - |
| Ken
Registered User |
Tue Jun 21 16:17:34 CDT 2005
Re:MKDir not working
"Rick Rothstein" <rickNOSPAMnews@NOSPAMcomcast.net>wrote in message
Quote>Submitted by Frederick Rothstein, Trenton, New Jersey -- Ken Halter - MS-MVP-VB - www.vbsight.com">www.vbsight.com DLL Hell problems? Try ComGuard - www.vbsight.com/ComGuard.htm">www.vbsight.com/ComGuard.htm Please keep all discussions in the groups.. - |
| Karl
Registered User |
Tue Jun 21 16:30:41 CDT 2005
Re:MKDir not working
Rick Rothstein wrote:
Quote>Submitted by Frederick Rothstein, Trenton, New Jersey QuoteI meant to add... that is, as I recall, a fairly old piece of code... still, I made a database with all the published ones. I remembered we had published one on recursive MkDir usage, so I just queried on that and it returned this single hit. :-) Later... Karl -- Working Without a .NET? classicvb.org/petition">classicvb.org/petition - |
| Lee
Registered User |
Tue Jun 21 19:46:24 CDT 2005
Re:MKDir not working
Why not use the MakeSureDirectoryPathExists API?
Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" (ByVal lpPath As String) As Long Private Sub Form_Load() 'KPD-Team 2000 'URL: www.allapi.net/">www.allapi.net/ 'E-Mail: KPDTeam@Allapi.net 'create the directory "c:\this\is\a\test\directory\", if it doesn't exist already MakeSureDirectoryPathExists "c:\this\is\a\test\directory\" End Sub Lee On Tue, 21 Jun 2005 14:30:41 -0700, "Karl E. Peterson" <karl@mvps.org> wrote: QuoteRick Rothstein wrote: |
| Karl
Registered User |
Tue Jun 21 20:19:02 CDT 2005
Re:MKDir not working
Lee Peedin wrote:
QuoteWhy not use the MakeSureDirectoryPathExists API? anything less than Windows 2000. (Requires distribution of DbgHelp.dll on 9x and NT4 -- not worth it.) I do have another API-leveraged solution, though, if overkill is what's desired, that I forgot was buried in an obscure module I include in almost every application I write. <g> Public Function PathAddBackslash(ByVal Path As String) As String Dim lpszResult As Long Dim Buffer As String ' Adds a backslash to the end of a string to create the correct ' syntax for a path. If the source path already has a trailing ' backslash, no backslash will be added. Buffer = String$(MAX_PATH, 0) Mid$(Buffer, 1) = Path lpszResult = PathAddBackslashW(StrPtr(Buffer)) ' Results point to position within buffer of null terminator, ' so it's easier to just "do the math", than to use TrimNull. PathAddBackslash = Left$(Buffer, (lpszResult - StrPtr(Buffer)) \ 2) End Function Public Function PathCanonicalize(ByVal Source As String) As String Dim Buffer As String ' Canonicalizes a path. Buffer = String$(MAX_PATH, 0) If PathCanonicalizeW(StrPtr(Buffer), StrPtr(Source)) Then PathCanonicalize = TrimNull(Buffer) End If End Function Public Function PathIsDirectory(ByVal Path As String) As Boolean ' Verifies that a path is a valid directory. PathIsDirectory = PathIsDirectoryW(StrPtr(Path)) End Function Public Function PathIsRoot(ByVal Path As String) As Boolean ' Parses a path to determine if it is a directory root. PathIsRoot = PathIsRootW(StrPtr(Path)) End Function Public Function PathMakeDirs(ByVal Path As String) As Boolean Debug.Print Path ' Recursively create directories, starting from the ' top down, wherever the path ceases to exist. If PathIsDirectory(Path) Then ' Success! Nothing to do. PathMakeDirs = True ElseIf PathIsRoot(Path) Then ' We failed - bad root, no privs? PathMakeDirs = False Else ' Attempt to create this folder. On Error Resume Next MkDir Path Select Case Err.Number Case 76 ' Path not found ' Attempt to make parent of this folder... If PathMakeDirs(PathParent(Path)) Then ' and follow-up by making requested folder. MkDir Path PathMakeDirs = PathIsDirectory(Path) End If Case 75 ' Path/File access error PathMakeDirs = False Case Else Debug.Print Err.Number, Err.Description PathMakeDirs = True End Select End If End Function Public Function PathParent(ByVal Path As String) As String ' Add a backslash and ".." then canonicalize PathParent = PathCanonicalize(PathAddBackslash(Path) & "..") End Function And so on, and so on, ... Yeah, it was "one of those slow days" when I decided to work out all the various Path* functions... <g> -- Working Without a .NET? classicvb.org/petition">classicvb.org/petition - |
| Rick
Registered User |
Tue Jun 21 23:54:30 CDT 2005
Re:MKDir not workingQuote>>Submitted by Frederick Rothstein, Trenton, New Jersey Split you say? How about Split and Reverse both? Public Sub MkDirs(ByVal PathIn As String) Dim X As Long If Right$(PathIn, 1) <>"\" Then PathIn = PathIn & "\" On Error Resume Next For X = 3 To UBound(Split(PathIn, "\")) + 1 MkDir Replace(PathIn, Split(PathIn, "\", X)(X - 1), "") Next End Sub Yeah, I know... it's not a one-liner, but there IS only one active statement in the loop. The opening If statement could be eliminated by concatenating an IIf function call into both the first argument of the Split function in the upper limit expression of For statement and the first argument of the Split function in the active loop statement; but I thought that was would be carrying the minimal code thing too far, so I created a separate line for it. As for the On Error statement... we can't escape using that if we want the function to work at all. So, no one-liner. Oh, and I guess you noticed the function became a Sub along the way... that was to save two extra lines which would be needed to track and return a Boolean status from the function. For the purists out there who insist this be a function, here is what I think is the minimal code (the If statement previously explained notwithstanding) that should work... Public Function MkDirs(ByVal PathIn As String) As Variant Dim X As Long If Right$(PathIn, 1) <>"\" Then PathIn = PathIn & "\" On Error Resume Next For X = 3 To UBound(Split(PathIn, "\")) + 1 MkDir Replace(PathIn, Split(PathIn, "\", X)(X - 1), "") MkDirs = MkDirs + Err.Number Next MkDirs = Not CBool(MkDirs Mod 75) End Function Please note, by the way, that the function returns a Variant with a sub-type of Boolean (I needed to use the function name for addition within the loop before setting it to a Boolean return value in order to minimize the number of lines of code.<g> Oh, one last thing... anyone not in on the OneLiner-Split-Reverse joke involving my more "creative" postings... DON'T USE THE CODE POSTED HERE!!! If you do, your teacher will flunk you, or your boss will fire you... that's a guarantee.<g> Rick - |
| Rick
Registered User |
Tue Jun 21 23:59:43 CDT 2005
Re:MKDir not workingQuote>Hey... that's me! My real name is Frederick and since they were your half of the payment... I think they will oblige you.<bg> Rick - |
| erewhon
Registered User |
Wed Jun 22 02:53:30 CDT 2005
Re:MKDir not working
On Tue, 21 Jun 2005 18:19:02 -0700, "Karl E. Peterson" <karl@mvps.org>
wrote: QuoteLee Peedin wrote: It was certainly in OSR1 Personally I do not use it, but that is because I require more info on any errors. - |
| Lee
Registered User |
Wed Jun 22 06:52:38 CDT 2005
Re:MKDir not working
Karl,
Wasn't aware of these limitations - as per the API-Guide, "Requires Windows NT 3.1 or later; Requires Windows 95 or later". I've used it on Win98SE and other than being sure the VB Runtime Files were present, I've not distributed anything else. Lee On Tue, 21 Jun 2005 18:19:02 -0700, "Karl E. Peterson" <karl@mvps.org> wrote: QuoteLee Peedin wrote: |
| Karl
Registered User |
Wed Jun 22 13:26:55 CDT 2005
Re:MKDir not working
J French wrote:
QuoteOn Tue, 21 Jun 2005 18:19:02 -0700, "Karl E. Peterson" <karl@mvps.org> MSDN, it seems they've removed (undocumented?) it entirely. Huh... -- Working Without a .NET? classicvb.org/petition">classicvb.org/petition - |
| Karl
Registered User |
Wed Jun 22 13:27:46 CDT 2005
Re:MKDir not working
Lee Peedin wrote:
QuoteWasn't aware of these limitations - as per the API-Guide, "Requires declaration(s)? Sorry... Karl -- Working Without a .NET? classicvb.org/petition">classicvb.org/petition - |
| Karl
Registered User |
Wed Jun 22 13:31:45 CDT 2005
Re:MKDir not working
Rick Rothstein wrote:
Quote>>>Submitted by Frederick Rothstein, Trenton, New Jersey Pretty cool, the MkDirs, actually. But is 75 the only potential error? -- Working Without a .NET? classicvb.org/petition">classicvb.org/petition - |
| Lee
Registered User |
Wed Jun 22 13:37:11 CDT 2005
Re:MKDir not working
Karl,
I have this in an ActiveX dll in order to use it in scripting languages such as VBScript, JScript, ooRexx, etc. Here's the declare (watch for line wrap) Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" (ByVal lpPath As String) As Long And here's the function Public Function CreateDir(inDir) MakeSureDirectoryPathExists inDir End Function And it doesn't "blow up" if the folder already exists; then again with a name like "MakeSure", you wouldn't expect it to. :-) Lee On Wed, 22 Jun 2005 11:27:46 -0700, "Karl E. Peterson" <karl@mvps.org> wrote: QuoteLee Peedin wrote: |
| Al
Registered User |
Wed Jun 22 13:40:23 CDT 2005
Re:MKDir not working
"Lee Peedin" <lpeedinREMOVE@UPPERCASEnc.rr.com>wrote in message news:vpbjb1l9pt1fvjgmqst4pmlg155g28aeou@4ax.com...
QuoteKarl, - |
| Lee
Registered User |
Wed Jun 22 13:57:16 CDT 2005
Re:MKDir not working
You know, sometimes I really feel for you folks that have so much time
invested VB5/6 and then be abandoned by M$. There is no reason imaginable for MS not adding the .NET features to the language instead of creating a new language. I have to give IBM credit in that field. When Object Rexx was first released on OS/2 and later for Win95 & up, the very FIRST rule was that all existing scripts should continue to run without change. Since IBM turned over the source code for Object Rexx to The Rexx Language Association and the RexxLA's first release in Mar '05 there have been nearly 18,000 downloads of ooRexx from SourceForge. Being a very active member of the RexxLA, I know there are hundreds if not thousands of ooRexx users that have never written a single line of object code - they still write procedural applications. Me, well my programs are usually a mix of both and they run on Windows, Linux, Mac, etc. Hang in there guys - maybe MS will release VB6 as OpenSource - yea right. :-) Lee On Wed, 22 Jun 2005 14:40:23 -0400, "Al Reid" <areidjr@reidDASHhome.com>wrote: Quote"Lee Peedin" <lpeedinREMOVE@UPPERCASEnc.rr.com>wrote in message news:vpbjb1l9pt1fvjgmqst4pmlg155g28aeou@4ax.com... |
| Rick
Registered User |
Wed Jun 22 14:00:53 CDT 2005
Re:MKDir not workingQuote>>>>Submitted by Frederick Rothstein, Trenton, New Jersey (based on your next question) you meant the 2nd one (with error trapping). QuoteBut is 75 the only potential error? MkDirs return False; 75 is the number for the error you get at the start of the loop when the beginning directories already exist... this is an OK error and is permitted. Once they (existing directories) run out, Err.Number should always be 0 thereafter. If not, those non-75 errors will, when added to the cumulated total, force the Mod function to report a non-zero value. Now, this error checker is not fool-proof. It is theoretically possible for the continuing sum to total in such a way that a multiple of 75 results (I don't stop the loop when a non-75 error is encountered; did that to save the line in order to minimize the code line count<g>); but I think it is highly unlikely for the length of the path coupled with the likely error numbers that result from disk access errors to combine to produce this situation. If one is hell-bent on using this minimally coded function I posted, but is worried about such things, an If-Then test could be embedded inside the loop checking for non-75 errors and executing an Exit For statement if one occurs. You would still need to add the Err.Number to the total being accumulated in MkDirs or, otherwise, you would have to modify the last statement in the function. Rick - |
| Karl
Registered User |
Wed Jun 22 14:13:20 CDT 2005
Re:MKDir not working
Hi Lee --
Okay, now this is interesting. I just used this in Windows 2000: Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" (ByVal lpPath As String) As Long Public Sub Main() Debug.Print CreateDir("c:\test1\test2") End Sub Public Function CreateDir(inDir As String) As Boolean If MakeSureDirectoryPathExists(inDir) Then CreateDir = True Else Debug.Print Err.LastDllError End If End Function And it's just not working at all, at least not in the manner expected! The first time, it returns True, but only the toplevel (\test1) folder is created! No second level. Very weird. If I change the call, to include a trailing backslash: Debug.Print CreateDir("c:\test1\test2\") It does appear to work. Looks like I get identical behavior if I use this declare (different lib) as well: Private Declare Function MakeSureDirectoryPathExists Lib "dbghelp.dll" (ByVal lpPath As String) As Long I'm prepared to say, this function only works with careful persuassion. Thanks... Karl Lee Peedin wrote: QuoteKarl, Working Without a .NET? classicvb.org/petition">classicvb.org/petition - |
| Lee
Registered User |
Wed Jun 22 14:20:03 CDT 2005
Re:MKDir not working
Karl,
Taken directly from the API-Guide: "· DirPath [in] Pointer to a null-terminated string that specifies a valid path name. If the final component of the path is a directory, not a file name, the string must end with a backslash (\) character." Lee On Wed, 22 Jun 2005 12:13:20 -0700, "Karl E. Peterson" <karl@mvps.org> wrote: QuoteHi Lee -- |
| Ken
Registered User |
Wed Jun 22 14:26:19 CDT 2005
Re:MKDir not working
"Karl E. Peterson" <karl@mvps.org>wrote in message
Quote
MakeSureDirectoryPathExists "must end with a backslash (\) character" http://msdn.microsoft.com/library/default.asp?url" rel="nofollow" target="_blank">msdn.microsoft.com/library/default.asp=/library/en-us/debug/base/makesuredirectorypathexists.asp ...which is a bit funny since my "Nested Folders" snip that uses MkDir strips the trailing backslash and my MakeSureDirectoryPathExists snip adds it. -- Ken Halter - MS-MVP-VB - www.vbsight.com">www.vbsight.com DLL Hell problems? Try ComGuard - www.vbsight.com/ComGuard.htm">www.vbsight.com/ComGuard.htm Please keep all discussions in the groups.. - |
| Karl
Registered User |
Wed Jun 22 19:08:12 CDT 2005
Re:MKDir not working
Lee Peedin wrote:
QuoteKarl, -- Working Without a .NET? classicvb.org/petition">classicvb.org/petition - |
| Karl
Registered User |
Wed Jun 22 19:11:25 CDT 2005
Re:MKDir not working
Rick Rothstein wrote:
Quote>Pretty cool, the MkDirs, actually. Quote>But is 75 the only potential error? CODE POSTED HERE!!!" warning out pretty well, huh? <g> QuoteOnce they (existing -- Working Without a .NET? classicvb.org/petition">classicvb.org/petition - |
| Rick
Registered User |
Wed Jun 22 19:42:07 CDT 2005
Re:MKDir not working
I just looked over my last response and, while you understood what I was
talking about, the wording I used looked awkward and convoluted. What's funny is that the logic behind the code seems so clear and straight-forward to my mind's "eye", but trying to describe it turned out to not be so easy. I wonder if this has anything to do with my hating to write documentation for code that I write.<g> Rick "Karl E. Peterson" <karl@mvps.org>wrote in message QuoteRick Rothstein wrote: |
| Karl
Registered User |
Wed Jun 22 20:14:37 CDT 2005
Re:MKDir not working
Rick Rothstein wrote:
QuoteI just looked over my last response and, while you understood what I it was *so* painful! I could wap out a sample of what I wanted to convey in no time. Always had a dozen or more just waiting for a deadline. But sitting down to spell it out... Argh! -- Working Without a .NET? classicvb.org/petition">classicvb.org/petition - |
| Randy
Registered User |
Wed Jun 22 21:34:59 CDT 2005
Re:MKDir not working
For MakeSureDirectoryPathExists, the MSDN actually does show limitations:
Client: Requires Windows XP or Windows 2000 Professional. Server: Requires Windows Server 2003 or Windows 2000 Server. Redistributable: Requires DbgHelp.dll on Windows NT 4.0 and Windows Me/98/95. Header: Declared in Dbghelp.h. Library: Use Dbghelp.lib. So if DbgHelp.dll is not present on NT4 or Win9x/ME, the function won't work. The other ones Karl mentioned also have some minimum requirements, mostly relating to having the shell shipped with IE4 installed on the older systems. -- Randy Birch MS MVP Visual Basic vbnet.mvps.org/">vbnet.mvps.org/ ---------------------------------------------------------------------------- Read. Decide. Sign the petition to Microsoft. classicvb.org/petition/">classicvb.org/petition/ ---------------------------------------------------------------------------- "Lee Peedin" <lpeedinREMOVE@UPPERCASEnc.rr.com>wrote in message : Karl, : Wasn't aware of these limitations - as per the API-Guide, "Requires : Windows NT 3.1 or later; Requires Windows 95 or later". I've used it : on Win98SE and other than being sure the VB Runtime Files were : present, I've not distributed anything else. : : Lee - |
