G
Guest
Hi,
I have spent a day writing a script to map a network drive to a share
folder and then copy files over from a remote server. At the end it deletes
all the files.
When I run it with local folders it works fine, but when I use the mapped
network drive it breaks and throws me errors. I really don't know what is
going on. The mapped network drive has all the permissions it needs to read,
write, and delete. Can someone shed some light on this issue. I have
included my script below.
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("Wscript.Network")
Set objFile = objFSO.OpenTextFile("f:\wutemp\test.txt", ForReading)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
arrItems = Split(strLine, " ")
If arrItems(0) = "Site1" Then
objNetwork.MapNetworkDrive arrItems(1), arrItems(2), "false",
"domain\administrator", "Password"
End If
Loop
objFile.Close
Const OVERWRITE_EXISTING = True
' Global variables
source = "r:\*.*" ' Source Drive
dest = "f:\wutemp\test2\" ' Destination Folder
delSource = "f:\wutemp\test2" ' Source Folder to delete files
logSource = "f:\wutemp\" ' Destination Folder to create log file
fileName = logSource & "filecopy.log"
' Create filesystem objects
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objStream = objFSO.CreateTextFile(fileName, true)
' Write header to log file
objStream.Writeline "File Copy Job Start - " & Time & Date
objStream.Writeline " "
' Copy all root files
objFSO.CopyFile source, dest, OVERWRITE_EXISTING
' Copy all subfolders and files
objFSO.CopyFolder source, dest, OVERWRITE_EXISTING
' write status to log file
If Err = 0 Then
objStream.Writeline "Files Copied Successfully"
objStream.Writeline " "
objStream.Writeline "Deleting source files"
objStream.Writeline " "
' Delete all files in root folder
DeleteAllFiles(delSource)
' Delete all files in subfolders
FindSubFolders objFSO.GetFolder(delsource)
Else
objStream.Writeline "Error: No Files Copied"
End If
' Write footer to log file
objStream.Writeline " "
objStream.Writeline "File Copy Job End - " & Time
objNetwork.RemoveNetworkDrive arrItems(1)
Set objFso = Nothing
Set objStream = Nothing
Set oFs = Nothing
Set ObjNetwork = Nothing
' method to traverse subfolders recursively
Sub FindSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
objStream.Writeline " "
DeleteAllFiles Subfolder.Path
FindSubFolders Subfolder
Next
End Sub
' method to delete all files within subfolder
Sub DeleteAllFiles(fName)
objStream.Writeline " + " & fName
Set oFs = CreateObject("Scripting.FileSystemObject")
If oFs.FolderExists(fName) then
Set oFolder = oFs.GetFolder(fName)
For Each oFile In oFolder.Files
objStream.Writeline " - " & oFile.name & " Deleted"
oFile.Delete
Next
End If
End Sub
I have spent a day writing a script to map a network drive to a share
folder and then copy files over from a remote server. At the end it deletes
all the files.
When I run it with local folders it works fine, but when I use the mapped
network drive it breaks and throws me errors. I really don't know what is
going on. The mapped network drive has all the permissions it needs to read,
write, and delete. Can someone shed some light on this issue. I have
included my script below.
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("Wscript.Network")
Set objFile = objFSO.OpenTextFile("f:\wutemp\test.txt", ForReading)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
arrItems = Split(strLine, " ")
If arrItems(0) = "Site1" Then
objNetwork.MapNetworkDrive arrItems(1), arrItems(2), "false",
"domain\administrator", "Password"
End If
Loop
objFile.Close
Const OVERWRITE_EXISTING = True
' Global variables
source = "r:\*.*" ' Source Drive
dest = "f:\wutemp\test2\" ' Destination Folder
delSource = "f:\wutemp\test2" ' Source Folder to delete files
logSource = "f:\wutemp\" ' Destination Folder to create log file
fileName = logSource & "filecopy.log"
' Create filesystem objects
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objStream = objFSO.CreateTextFile(fileName, true)
' Write header to log file
objStream.Writeline "File Copy Job Start - " & Time & Date
objStream.Writeline " "
' Copy all root files
objFSO.CopyFile source, dest, OVERWRITE_EXISTING
' Copy all subfolders and files
objFSO.CopyFolder source, dest, OVERWRITE_EXISTING
' write status to log file
If Err = 0 Then
objStream.Writeline "Files Copied Successfully"
objStream.Writeline " "
objStream.Writeline "Deleting source files"
objStream.Writeline " "
' Delete all files in root folder
DeleteAllFiles(delSource)
' Delete all files in subfolders
FindSubFolders objFSO.GetFolder(delsource)
Else
objStream.Writeline "Error: No Files Copied"
End If
' Write footer to log file
objStream.Writeline " "
objStream.Writeline "File Copy Job End - " & Time
objNetwork.RemoveNetworkDrive arrItems(1)
Set objFso = Nothing
Set objStream = Nothing
Set oFs = Nothing
Set ObjNetwork = Nothing
' method to traverse subfolders recursively
Sub FindSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
objStream.Writeline " "
DeleteAllFiles Subfolder.Path
FindSubFolders Subfolder
Next
End Sub
' method to delete all files within subfolder
Sub DeleteAllFiles(fName)
objStream.Writeline " + " & fName
Set oFs = CreateObject("Scripting.FileSystemObject")
If oFs.FolderExists(fName) then
Set oFolder = oFs.GetFolder(fName)
For Each oFile In oFolder.Files
objStream.Writeline " - " & oFile.name & " Deleted"
oFile.Delete
Next
End If
End Sub