1.Dim d As String

2.d = Format(Now(), "dddd, yyyy mmmm dd")

3. MsgBox (d)

4.If Dir("c:/", Photos & d) = "" Then

5.rspCreate = MsgBox("Directory doesn't exist, do you wish to create it?", vbYesNo)

6.If rspCreate = vbYes Then

7. MkDir "My Documents/swapna"

8. End If

i want to create a folder with name photo+currentdate
so i used the above code
but im getting the error type mismatch in line no 4
i want to convert the data variable to string

plz help

Dani AI

Generated

was tripping over two separate issues that produced the "Type mismatch" error: incorrect use of the Dir call and invalid characters in the folder name. Dir takes a path string (or pattern) as its first argument and an optional numeric attributes flag as the second. In the original code a comma was used where string concatenation was intended, so Dir was receiving a string where it expected an attribute number. Also, a date formatted with slashes (for example 27/05/2008) contains characters that are not allowed in Windows file or folder names.

A safe, minimal fix is to build a valid path string, avoid illegal characters (use a format like yyyyMMdd or replace / with -), and call Dir correctly. For example:

Dim folderPath As String
folderPath = "C:\Photos_" & Format(Date, "yyyyMMdd")

If Dir(folderPath, vbDirectory) = "" Then
    If MsgBox("Directory doesn't exist. Create it?", vbYesNo) = vbYes Then
        MkDir folderPath
    End If
End If

Notes and troubleshooting tips:

  • Use backslashes for Windows paths (C:\), not forward slashes. was correct about that.
  • Windows forbids these characters in names: < > : " / \ | ? *. Avoid them by choosing a date format without slashes (for example yyyyMMdd) or by replacing bad chars with - or _.
  • MkDir creates only a single folder level; creating nested folders requires creating parents first or using a helper routine/FileSystemObject.
  • On modern Windows, creating folders directly under C:\ may fail due to permissions—use a user-writable location (Documents) when testing.
  • Add a quick MsgBox folderPath or Debug.Print folderPath before Dir to confirm the exact string being checked.

’s FolderExist approach is a robust alternative if you prefer a reusable check routine.

Recommended Answers

All 6 Replies

its not about date but your pathName is wrong.
should be : C:\

yes i tried still getting the error
i want to create a folder say photos27/05/2008 which is to be in c:
its not a folder inside folder photo
am i clear

Yes, i know it but your sintax is wrong, its should be C:\ not C:/
Also you don't have function to check if folder exist or not. see this following code :

Public Function FolderExist(Path As String) As Boolean

If LenB(Path) = 0 Then FolderExist = False: Exit Function

If LenB(Dir$(Path, vbDirectory)) > 0 Then
If (GetAttr(Path) And vbDirectory) = vbDirectory Then FolderExist = True
Else
FolderExist = False
End If
End Function

Private Sub Command1_Click()
Dim d, Path As String

d = Format(Now(), "dddd, yyyy mmmm dd")
MsgBox (d)

Path = "C:\Photos " & d
If FolderExist(Path) = True Then
    MsgBox "Folder is available"
Else
    rspCreate = MsgBox("Directory doesn't exist, do you wish to create it?", vbYesNo)
    If rspCreate = vbYes Then
        MkDir Path
    End If
End If
End Sub
commented: Nice +1
commented: Great +1
commented: Nice one friend :) +1

yes i tried with c:\
but....
im sorry
but i got this from the net
and in this there is no function
im sending the URL

i wil try ur code

thank u

thanks
and sorry i misunderstood something thats why the mistake

Don't mind it Friend :)
Happy coding.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.