INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Help needed with code for copying file to new location and renaming
2

Help needed with code for copying file to new location and renaming

Help needed with code for copying file to new location and renaming

(OP)
Hello,

I am using the code below and have the following problems...

If the file exists and I say I don't want to overwrite the code continues to execute.

Also the files are copying but not retaining their correct file extentions.

I am just learning coding so I would much appreciate and advice in regard to modifying the code to make it better in any areas.

Me.txt_File_Selected is a text box on my form containing the full source path and file name i.e. c:\temp\dog.jpg
Me.txtfile is a text box on my form containing the destination path and file name i.e. c:\temp\dog



Many thanks Mark


CODE -->

Dim fso As Object
   Dim sourceFile As String
   Dim targetFile As String
   Dim answer As Boolean

  Set fso = CreateObject("Scripting.FileSystemObject")
    sourceFile = Me.txt_File_Selected
    targetFile = Me.txtfile
    
  If fso.FileExists(sourceFile) = False Then
  MsgBox "The source file can not be found - please search for the file again"
  Exit Sub
  End If


  If fso.FileExists(targetFile) = True Then
     answer = MsgBox("File already exists in this location. " _
        & "Are you sure you want to continue? If you continue " _
        & "the file at destination will be over written!", _
            vbInformation + vbYesNo)
      If answer = vbNo Then
        Exit Sub
        End If
        End If
      
fso.CopyFile sourceFile, targetFile
Set fso = Nothing
MsgBox "The file has been Added" 

RE: Help needed with code for copying file to new location and renaming

Would this work for you?

CODE -->

If Dir(txtfile) <> "" Then
        yesno = MsgBox("File exists.  Overwrite/Replace?", vbYesNo, "File Exists")
        If yesno = vbNo Then
            GoTo exitsub
        End If
    End If 

Also, instead of using setting up with fso, you could do the file copying with this line instead.

FileCopy SourceFile, DestinationFile ' Copy source to target.

RE: Help needed with code for copying file to new location and renaming

(OP)
Thank you.

I am not sure which to use.

Is FSO better or is FileCopy??

I am using the code below which is now working, but do you think the FileCopy method is better?

Many thanks Mark

CODE -->

Private Sub btn_Save_File_DblClick(Cancel As Integer)

   Dim fso As Object
   Dim sourceFile As String
   Dim targetFile As String
   Dim answer As Boolean

  Set fso = CreateObject("Scripting.FileSystemObject")
   sourceFile = Me.txt_File_Selected
   targetFile = Me.txt_File_Save_Name_Full
    
 If fso.FileExists(sourceFile) = False Then
  MsgBox "The SOURCE file can NOT be found - please search for the file again"
  Exit Sub
 End If

 If fso.FileExists(targetFile) = True Then
     answer = MsgBox("File already exists in this location. " _
        & "Are you sure you want to continue? If you continue " _
        & "the file at destination will be over written!", _
            vbInformation + vbYesNo)
      If answer = vbNo Then
      MsgBox "The SOURCE file can NOT be found - please search for the file again"
      Exit Sub
      End If

End If
      
fso.CopyFile sourceFile, targetFile

Set fso = Nothing

MsgBox "The file has been Added"

End Sub 

RE: Help needed with code for copying file to new location and renaming

(OP)
When i choose 'No' as the answer below, the code still continues to copy - can anyone see whats wrong... Thanks Mark

CODE -->

If fso.FileExists(targetFile) = True Then
     answer = MsgBox("File already exists in this location. " _
        & "Are you sure you want to continue? If you continue " _
        & "the file at destination will be over written!", _
            vbInformation + vbYesNo)
      If answer = vbNo Then
      MsgBox "The SOURCE file can NOT be found - please search for the file again"
      Exit Sub
      End If

End If 

RE: Help needed with code for copying file to new location and renaming



CODE

Dim answer As Boolean   '<== Here is your problem

'Try this instead:
If fso.FileExists(targetFile) = True Then
     If vbNo = MsgBox("File already exists in this location. " _
        & "Are you sure you want to continue? If you continue " _
        & "the file at destination will be over written!", _
            vbInformation + vbYesNo) Then
        MsgBox "The SOURCE file can NOT be found - please search for the file again"
        Exit Sub
      End If
End If 

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.

RE: Help needed with code for copying file to new location and renaming

FYI,
Boolean can only hold True/False, which is either -1/0 or 1/0 or "anything but 0"/0

If you do this:

CODE

Debug.Print "vbYes - " & Val(vbYes)
Debug.Print "vbNo - " & Val(vbNo)
Debug.Print "vbCancel - " & Val(vbCancel) 
you get:
vbYes - 6
vbNo - 7
vbCancel - 2

That's why your answer was always True (vbNo = 7 and 7 <> 0) smile

Have fun.

---- Andy

A bus station is where a bus stops. A train station is where a train stops. On my desk, I have a work station.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close