Check if specifik folder exist.

H

HH

Hi'

I have a macro copying files to specifik folders. I only have one
problem, if the folder is not created then the script stops.


How do I let the script ignore this file and just move on to the next?
I
suppose i need to check if the destination folder exist, but how?


Thanks
 
W

WhytheQ

an old post by Bob Philips says:

'################################
1. Use DIR to test, and FileSystemObject to create it


Dim myDir, myFile


myFile = "C:\Billing\Invoices\SepInv\"
myDir = Dir(myFile)
If myDir <> "" Then
MsgBox "Directory already exists"
Else
myDir =
CreateObject("Scripting.FileSystemObject").createFolder(myFile)
End If


2. Use FileSystemObject for both


Dim myDir, myFile


myFile = "C:\Billing\Invoices\SepInv\"
myDir =
CreateObject("Scripting.FileSystemObject").FolderExists(myFile)
If myDir = True Then
MsgBox "Directory already exists"
Else
myDir =
CreateObject("Scripting.FileSystemObject").createFolder(myFile)
End If
'#######################################

I'm sure the above must help
Rgds
J
 
M

MichDenis

| myFile = "C:\Billing\Invoices\SepInv\"
| myDir = Dir(myFile)
| If myDir <> "" Then
| MsgBox "Directory already exists"

This does not work if the folder is empty...

It's better if you use this : myDir = Dir(myFile, vbDirectory)



"WhytheQ" <[email protected]> a écrit dans le message de (e-mail address removed)...
an old post by Bob Philips says:

'################################
1. Use DIR to test, and FileSystemObject to create it


Dim myDir, myFile


myFile = "C:\Billing\Invoices\SepInv\"
myDir = Dir(myFile)
If myDir <> "" Then
MsgBox "Directory already exists"
Else
myDir =
CreateObject("Scripting.FileSystemObject").createFolder(myFile)
End If


2. Use FileSystemObject for both


Dim myDir, myFile


myFile = "C:\Billing\Invoices\SepInv\"
myDir =
CreateObject("Scripting.FileSystemObject").FolderExists(myFile)
If myDir = True Then
MsgBox "Directory already exists"
Else
myDir =
CreateObject("Scripting.FileSystemObject").createFolder(myFile)
End If
'#######################################

I'm sure the above must help
Rgds
J
 
C

Chip Pearson

Use code like the following:

Dim FolderExists As Boolean
Dim FolderName As String
FolderName = "C:\Test1234"
FolderExists = (Dir(FolderName, vbDirectory + vbHidden) <> vbNullString)
If FolderExists = False Then
'''''''''''''''''''''''''''''''''''
' If the folder doesn't exist, use
' MkDir to create the folder.
'''''''''''''''''''''''''''''''''''
On Error Resume Next
Err.Clear
MkDir FolderName
If Err.Number <> 0 Then
MsgBox "An error occurred with MkDir:" & vbCrLf & _
"Err: " & CStr(Err.Number) & vbCrLf & _
"Desc: " & Err.Description
End If
On Error GoTo 0
End If

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top