Option Explicit
Dim srcFolder, trgFolder, TSFile
Dim oFSO, oTS, oNw
Dim iReturn
Dim oDrives, i
TSFile = "C:\copylog_" & year(now) & month(now) & day(now) & ".log"
srcFolder = "d:\apl\"
trgFolder = "x:"
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oTS = oFSO.OpenTextFile (TSFile, 8,True)
oTS.writeline ("VBScript started on" & " " & date & " " & "at" & " " & time)
Set oNw = CreateObject("WScript.Network")
Set oDrives = oNw.EnumNetworkDrives
For i = 0 to oDrives.Count - 1 Step 2
If oDrives.Item(i) = "X:" Then
oNw.RemoveNetworkDrive oDrives.Item(i), true
wscript.sleep 3000
End If
Next
On Error Resume Next
Err.clear
iReturn = Nw.MapNetworkDrive("X:","\\server\share", False, "domain\user", "password")
If Err.Number = 0 Then
oTS.writeline (time & " " & "Mapping sucessefull.")
CopyFilesAndFolders srcFolder, trgFolder
ElseIf Err.Number = -2147023570 Then
oTS.writeline (time & " " & "Mapping unsucessefull!!")
Else
oTS.writeline (time & "ERROR on DTRCFPS01: " & Err.Number & " - " & Err.Description)
End If
On error goto 0
oTS.Close
Set oTS = Nothing
Set oFSO = Nothing
Set oDrives = Nothing
Set oNw = Nothing
Sub CopyFilesAndFolders (ByVal strSource, ByVal strDestination)
Dim ObjFSO, ObjFolder, ObjSubFolder, ObjFile, files
Dim TargetPath
Set ObjFSO = CreateObject("scripting.filesystemobject")
'connecting to the folder where is going to be searched
Set ObjFolder = ObjFSO.GetFolder(strSource)
If not Right(strSource,1) = "\" Then strSource = StrSource & "\"
TargetPath = Replace (objFolder.path & "\", strSource, strDestination, 1, -1, vbTextCompare)
Err.clear
On Error Resume Next
'Check all files in a folder
For Each objFile In ObjFolder.files
If Err.Number <> 0 Then Exit For 'If no permission or no files in folder
On Error goto 0
'DO STUFF HERE
objFSO.copyfile objFile.path, TargetPath & objFile.name, True
oTS.writeline now & "_" & objFile.path & "_" & objFile.DateLastModified
Next
'Recurse through all of the subfolders
On Error Resume Next
Err.clear
For Each objSubFolder In ObjFolder.subFolders
If Err.Number <> 0 Then Exit For 'If no permission or no subfolder in folder
On Error goto 0
'For each found subfolder there will be searched for files
ObjFSO.CreateFolder TargetPath & ObjSubFolder.name & "\"
CopyFilesAndFolders ObjSubFolder.Path & "\", TargetPath & ObjSubFolder.name & "\"
Next
Set ObjFile = Nothing
Set ObjSubFolder = Nothing
Set ObjFolder = Nothing
Set ObjFSO = Nothing
End Sub