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?
QuickstepbrAsked:
Who is Participating?
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.

KGNicklCommented:
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"
Else
MsgBox "File does not exist"
End If

Open in new window

0

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
KGNicklCommented:
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
    Else
        'Code for Yes
    End If
Else
    MsgBox "File does not exist"
    ' Code here to save the file or do what you want
End If

Open in new window

0
KGNicklCommented:
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

0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

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
0
Curt LindstromCommented:
Try changing your macro like this.

Curt
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

0
dlmilleCommented:
dim FSO as Object
dim fName as string

fName = "string path\name of file"
if FSO.FileExists(fName) then
   'file exists
else
  'file doesn't exist
end if
0
Automation_GuruCommented:
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.

Regads,
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

Else

End If

End Sub

Open in new window

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

Thank you very much
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.