Below is VBS code to copy a file from one folder to another, and then delete
it. I've stripped it out of a larger function just to isolate the problem
pieces. The folders are referenced in UNC format because the longer-term
goal is to use it across the network.

If I run it from scratch, the file copy works fine, but the file delete
fails. If I then run the script again, the file copy fails. It seems the
file copy will only succeed if the file doesn't already exist in the target
folder, so it would sound like a permissions issue. However, I have full
Change permissions on both source and destination folders, and if I attempt
the same thing from the same command line using DOS, it succeeds. Anyone
have a pointer as to what I might be doing wrong? Thanks.

If you try to run this yourself, change the sSourcePath and sResultsFN
values in the main script, and the oFolder path value in the Function to
something that will work on your computer.


Option Explicit

Dim sTargetPC, oNet, oFSO, oShell
Public sSourcePath, sResultsFN

sSourcePath = "\\127.0.0.1\d$\work\Temp\"
sResultsFN = "Results!.txt"

Set oNet = CreateObject("WScript.Network")
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oShell = CreateObject("WScript.Shell")

sTargetPC = "127.0.0.1"
Call subDeleteTempFiles(sTargetPC)



Sub subDeleteTempFiles(sComputer)
Dim oFSO1, oFolder
Set oFSO1 = WScript.CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO1.GetFolder("\\" & sComputer & "\d$\Work")

On Error Resume Next
oFSO1.CopyFile sSourcePath & sResultsFN, oFolder.Path & "\"
If Err.Number <> 0 Then
MsgBox Now & vbTab & "Halt: Can't copy " & sSourcePath & sResultsFN & "
to " & oFolder.Path
WScript.Quit
Else
Err.Clear
msgbox "File should be copied now."
End If

On Error Resume Next
oFSO1.DeleteFile(oFolder.Path & "\" & sResultsFN)
If Err.Number <> 0 Then
MsgBox Now & vbTab & "Halt: Cannot delete " & oFolder.Path & "\" &
sResultsFN
WScript.Quit
Else
Err.Clear
msgbox "File should be gone now."
End If

End Sub

Re: Why is this file copy/delete code failing? by Owen

Owen
Mon Jul 23 17:39:56 CDT 2007

On Jul 20, 4:40 pm, Matt Bell <MattB...@discussions.microsoft.com>
wrote:
> Below is VBS code to copy a file from one folder to another, and then delete
> it. I've stripped it out of a larger function just to isolate the problem
> pieces. The folders are referenced in UNC format because the longer-term
> goal is to use it across the network.
>
> If I run it from scratch, the file copy works fine, but the file delete
> fails. If I then run the script again, the file copy fails. It seems the
> file copy will only succeed if the file doesn't already exist in the target
> folder, so it would sound like a permissions issue. However, I have full
> Change permissions on both source and destination folders, and if I attempt
> the same thing from the same command line using DOS, it succeeds. Anyone
> have a pointer as to what I might be doing wrong? Thanks.
>
> If you try to run this yourself, change the sSourcePath and sResultsFN
> values in the main script, and the oFolder path value in the Function to
> something that will work on your computer.
>
> Option Explicit
>
> Dim sTargetPC, oNet, oFSO, oShell
> Public sSourcePath, sResultsFN
>
> sSourcePath = "\\127.0.0.1\d$\work\Temp\"
> sResultsFN = "Results!.txt"
>
> Set oNet = CreateObject("WScript.Network")
> Set oFSO = CreateObject("Scripting.FileSystemObject")
> Set oShell = CreateObject("WScript.Shell")
>
> sTargetPC = "127.0.0.1"
> Call subDeleteTempFiles(sTargetPC)
>
> Sub subDeleteTempFiles(sComputer)
> Dim oFSO1, oFolder
> Set oFSO1 = WScript.CreateObject("Scripting.FileSystemObject")
> Set oFolder = oFSO1.GetFolder("\\" & sComputer & "\d$\Work")
>
> On Error Resume Next
> oFSO1.CopyFile sSourcePath & sResultsFN, oFolder.Path & "\"
> If Err.Number <> 0 Then
> MsgBox Now & vbTab & "Halt: Can't copy " & sSourcePath & sResultsFN & "
> to " & oFolder.Path
> WScript.Quit
> Else
> Err.Clear
> msgbox "File should be copied now."
> End If
>
> On Error Resume Next
> oFSO1.DeleteFile(oFolder.Path & "\" & sResultsFN)
> If Err.Number <> 0 Then
> MsgBox Now & vbTab & "Halt: Cannot delete " & oFolder.Path & "\" &
> sResultsFN
> WScript.Quit
> Else
> Err.Clear
> msgbox "File should be gone now."
> End If
>
> End Sub

Try taking out On Error Resume Next so you can see what error it's
getting. Do this on the delete bit as well.
you'll probably want to condition your actions as well, for example

sDestPath=oFolder.Path & "\" & "Name of file(s) to copy"
If not FileExists(sDestPath) Then
oFSO1.CopyFile sSourcePath & sResultsFN, oFolder.Path & "\"
End If

As you have already discovered, this is not the most user friendly or
robust of the VBScript functions