If File exists Then NewFile="XYZ"

P

PcolaITGuy

I have a macro that is used to copy a source file (dim srcfile as string) out
to a path (dim destpath as string).

user inputs the full source file path into a text box.
(srcfile=txt_source.text)

user input the destination path into a second text box.
(destpath=txt_destpath.text)

there is a command button (cmd_execute_copy) with associated code that
constructs a command line string using copy command with the user inputs
("cmd.exe /c copy " + srcfile + " " +destpath).

This all works just great. However, I would like to be able to construct an
IF statement that first checks to see if the same file name already exists in
the destination path. If it does, then I would like to be able to add ".bak"
to the end of the SRCFILE value and then continue the copy operation. I
guess the trick would be to first parse the SRCFILE string from the end
backwards to the first "\" that it runs into which should then indicate the
filename?

Here is my code so far:
Private Sub cmd_execute_copy_Click()
Dim srcfile As String
Dim destpath As String
Dim copystring As String
srcfile = txt_source.Text
destpath = txt_destpath.Text
copystring = "cmd.exe /c copy " + srcfile + " " + destpath
Shell (copystring), 1
end sub


Thanks in advance,

Pcola
 
J

Jim Rech

If Dir(srcfile) <> "" Then
''Do whatever you want to the file name
End If

You might use FileCopy to avoid the kludgey Shell.
 

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