I have a specified path for a file, for example, C:\Folder1\1.txt, and I want to use filecopy to copy a file to that location. Now, if Folder1 does not exist, what is the easiest way to acheive my goal?
Printable View
I have a specified path for a file, for example, C:\Folder1\1.txt, and I want to use filecopy to copy a file to that location. Now, if Folder1 does not exist, what is the easiest way to acheive my goal?
Is your goal to just copy the file to that folder? If folder1 does not exist then you could just use the MkDir() function to create it before you copy to the folder...
Otherwise I'm confused about what you mean.
You may use Dir$() function to check if folder exist first:
VB Code:
If Dir$("c:\MyDir", vbDirectory) = "" Then MkDir "c:\MyDir" End If
Well, there may be a number of different non-existant folders before the file, and I won't necessarily know what they are. Is my only option to programatically find the directory the folder will be in and use MkDir?
If you don't know what they are, then how can you find them?
If they don't exist, then how will you know what to make?
This is very confusing.
Also, MkDir will only make one folder at a time. For example, if you triedMkDir would return an error. In order to make that folder list, you would have to doVB Code:
MkDir "c:\Hack\Hack1\"If you need to make multiple folders with one pass, then user the MakeSureDirectoryPathExists API.VB Code:
MkDir "c:\Hack\" MkDir "c:\Hack\Hack1\"
Use MakeSureDirectoryPathExists.
It will create all the folders in 1 shot.
http://msdn.microsoft.com/library/de...pathexists.asp
or you could use this code
Put this in you general declaration section
VB Code:
Private Declare Function MakeSureDirectoryPathExists _ Lib "imagehlp.dll" (ByVal lpPath As String) As Long
and when you want your program to create the file folders you require then put this code in that procedure
VB Code:
MakeSureDirectoryPathExists App.Path & "\FOLDER NAME\" MakeSureDirectoryPathExists App.Path & "\FOLDER NAME\"
MakeSureDirectoryPathExists is the way I'd do it to.
Straight out of the API guide
VB Code:
Private Declare Function MakeSureDirectoryPathExists Lib "imagehlp.dll" (ByVal lpPath As String) As Long Private Sub Form_Load() 'KPD-Team 2000 'URL: [url]http://www.allapi.net/[/url] 'E-Mail: [email][email protected][/email] 'create the directory "c:\this\is\a\test\directory\", if it doesn't exist already MakeSureDirectoryPathExists "c:\this\is\a\test\directory\" End Sub
Some people don't bother reading replies at all which results in multiduplicate posts. :rolleyes:
Hack - Today, 06:16 AM Post #5
mayurvb - Today, 06:41 AM Post #6 (25 minutes later)
Redangel - Today, 07:49 AM Post #7 (1 hour 33 minutes later)
Atribune - Today, 10:12 AM Post #8 (almost 4! hours later)
Really is amazing!
I really missed that last line from the post # 5.
Sincere apologies! :-(