1

I have VBScript that I wrote a long while back to identify PDF based on the file name. It then appended data to the file name and moved it to the proper directory. I did it as a Select Case in order for it to loop for many file names. I am now attempting to modify the script to check if the file with the new name is already at the destination directory, and if so, delete the old file, and copy the new one (also if the file is open and can't be overwritten, ignore and move to the next). I've been searching on many forums, and have been able to find pieces of what I am attempting, but have been unable to successfully integrate the processes into my script. Here is what I have for my select case, this section is what gets repeated with the "VariableAddedtoFileName" changed.

Select Case Pname
    Case "FileName"
        sDestinationFolder = "\\Server\FileDir\"       
        sDestinationName = "VariableAddedtoFileName"      
        Set oFSO = CreateObject("Scripting.FileSystemObject")
        sSourceFile = objStartFolder & "\" & objFile.Name
        sDestinationFile = sDestinationFolder & "\" & Pname & " " & _
            sDestinationName & Right(objFile.Name, 4)

        If oFSO.FileExists(sDestinationFile) Then
            Set oFSO = Nothing
        Else
            oFSO.MoveFile sSourceFile, sDestinationFile
            Set oFSO = Nothing
        End If
    Case "StatementScriptTest"
    Case Else
End Select

So if I change theSet oFSO line in the If oFSO.FileExists group to oFSO.DeleteFile sDestinationFile It deletes the file, but won't copy the new one. If Rerun, it then copies the file, since it is no longer there. I have tried multiple combinations of attempting to manipulate the if statements and then with no luck. I also attempted to delete the file prior to the if section with no avail. Any assistance would be greatly appreciated.

If the full script is needed I can provide, I only listed this section as it is the part that gets rerun numerous times. Also I am aware that there are multiple posts similar to this, but I want to figure out how to update my code to work.

Update: I have fixed the overwriting by using CopyFile:

 If oFSO.FileExists(sDestinationFile) Then
    oFSO.CopyFile sSourceFile, sDestinationFile, True
Else
    oFSO.CopyFile sSourceFile, sDestinationFile, True
    Set oFSO = Nothing
End If

But I am still getting errors if the file is open when the attempt to overwrite is made.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
SanLuka
  • 125
  • 2
  • 13

1 Answers1

3

First, you won't need the IF statement if you will have the same code in each branch. Just use the oFSO.CopyFile sSourceFile, sDestinationFile, True and it will do the work for you.

Second, in order to catch the error, you will have to use On Error Resume Next declaration before the copy command and check if some error triggered:

On Error Resume Next ' this tells VB to not throw errors and to populate the Err object when an error occurs

oFSO.CopyFile sSourceFile, sDestinationFile, True

IF Err.Number <> 0 Then
    ' do something when error occurs
    ' ...

    Err.Clear ' clears the error so it will not trigger this on the loop if no more errors occur
End IF

' When you want to stop ignoring the errors
On Error GoTo 0
Ricardo Souza
  • 16,030
  • 6
  • 37
  • 69
  • 1
    Fantastic [rcdmk](http://stackoverflow.com/users/1046610/rcdmk)! It appears I was over complicating things. Time for some more coffee... – SanLuka Jan 25 '16 at 16:22