Checking if file name already exists

For security reasons (data loss) I believe that we should check if the file name already exists.
Can we do this?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

The below does that. But your question is vague, so who knows if that's what you were really asking.

Are you checking if the file name exists in the output folder you program is saving the new file to? A specific folder? Group of folders? Etc....?
If Dir("full path filename") <> "" Then
MsgBox "File exists"
MsgBox "File does not exist"
End If

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Also, you might want to do a yes no button.
If Dir("full path filename") <> "" Then
    Dim Answer As String
    Answer = MsgBox(MyNote, vbQuestion + vbYesNo, "???")
    If Answer = vbNo Then
        'Code for Yes
        'Code for Yes
    End If
    MsgBox "File does not exist"
    ' Code here to save the file or do what you want
End If

Open in new window

One line above was wrong
Answer = MsgBox('The file already exists. Click Yes to overwrite the file or No to cancel.', vbQuestion + vbYesNo, "???")

Open in new window

Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

QuickstepbrAuthor Commented:
Hi KGNickI,

Thanks for your answer.
 This question is related to a previous question that you can find at the link below. It will also answer your questions
Previous question
Curt LindstromCommented:
Try changing your macro like this.

Sub SaveWorkbook()
On Error Resume Next

ActiveWorkbook.SaveAs Filename:=GetDesktopFolder & "\CD-" & Format(Sheets("Tabela").Cells(1, "B"), "yymmdd") & ".xlsm" _
                        , FileFormat:=xlOpenXMLWorkbookMacroEnabled

End Sub

Function GetDesktopFolder()
Dim objShell
Set objShell = CreateObject("WScript.Shell")
GetDesktopFolder = objShell.SpecialFolders("Desktop")
Set objShell = Nothing
End Function

Open in new window

dim FSO as Object
dim fName as string

fName = "string path\name of file"
if FSO.FileExists(fName) then
   'file exists
  'file doesn't exist
end if
Hi :

Here is code. It gives a message if file already exists. You can play around it and change according to your specific needs.

Let me know if you need any help on this.

Automation Guru

Public Sub Find_File()

Dim fso
Dim file As String, sfol As String, dfol As String

file = "Business_List" 'File Name

sfol = "D:\Prabhat\Automation Guru\" & file & ".xls"
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(sfol) Then
    MsgBox "This File Already Exists", vbOKOnly, "Duplicate Error"


End If

End Sub

Open in new window

QuickstepbrAuthor Commented:
Given the code I already had from the previous question, this was the simplest solution.

Thank you very much
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.