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