At this point the code appears correct and you're now running into a runtime error related to the file management. Since we don't have your server setup it would be difficult for us to debug this for you so you might need to debug yourself. However I ran the equivalent code and it correctly copies/creates/deletes files. Here's the updated script that I cleaned up a little while checking to see if there are any issues.
Option Explicit
DIM fso, ObjFolder, ColFiles, ObjFile
DIM Filename, DateOcorStr, DateoCorDate, NameSplited, DateNow, DateBefore
DIM NumPC (21)
NumPC(0)="221"
NumPC(1)="222"
NumPC(2)="223"
NumPC(3)="224"
NumPC(4)="225"
NumPC(5)="226"
NumPC(6)="227"
NumPC(7)="228"
NumPC(8)="229"
NumPC(9)="230"
NumPC(10)="231"
NumPC(11)="232"
NumPC(12)="233"
NumPC(13)="234"
NumPC(14)="235"
NumPC(15)="236"
NumPC(16)="237"
NumPC(17)="238"
NumPC(18)="239"
NumPC(19)="240"
NumPC(20)="241"
NumPC(21)="242"
const cnstCommand = 1
DateNow=Now
DateBefore= cDate(DatePart("m",Datenow) & "/01" & "/" & DatePart("yyyy",Datenow))
Dim Item
Dim targetPath
For each Item in NumPC
Set fso = CreateObject("Scripting.FileSystemObject")
'wscript.Echo "\\pc" & Item & "south\bkp$\"
Set ObjFolder=fso.GetFolder("\\pc" & Item & "south\bkp$\")
targetPath = "\\172.16.2.28\pc" & Item & "\LOG\"
Set ColFiles=ObjFolder.files
For each ObjFile in ColFiles
FileName=ObjFile.Name
If InStr(FileName, "-")>0 then
NameSplited= Split(Filename, "-")
DateOcorStr=NameSplited(1)
DateOcorDate= Mid(DateOcorStr,7,2) & "/" & Mid(DateOcorStr,5,2) & "/" & Left(DateOcorStr,4)& " 00:00:00"
If (NameSplited(0)="nfc") Then
If ((cDate(DateOcorDate) >= DateBefore) AND (cDate(DateOcorDate) < cdate(DateNow)-1)) then
If fso.FileExists(targetPath & ObjFile.name)then
Wscript.Echo "Existing!"
else
If Not fso.folderExists (targetPath) Then
fso.CreateFolder (targetPath)
End If
fso.CopyFile ObjFile.Path, targetPath, true
End If
elseIf (cDate(DateOcorDate) < DateAdd("m", -3, DateBefore) )then
fso.DeleteFile ObjFile.path & "*"
End If
End If
End If
next
Next
I suspect that the issue might be that one of the machines in your array that you're enumerating through is returning Nothing
for one of the FSO calls. Therefore the script fails. You should consider wrapping the contents of the actual work inside the for each
in an error handler so you can skip over machines that have issues.