Script: Network share problems

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
 
M

Marty List

Which line is causing it to break? When you say "throws me errors" that's
not very descriptive, please post the error description.
 
G

Guest

Sorry, the error I'm getting is on line 49 " objFSO.CopyFile source, dest,
OVERWRITE_EXISTING"
 
M

Marty List

And what is the error message???

Since you don't have any error handling enabled in your script, you should be
seeing an error description from the default error handler of the script host
(cscript.exe or wscript.exe). If for some reason you're not getting an error
message, then add a check for Err.Number and Err.Description after the line
that's failing.
 
G

Guest

Ok. Thanks for the tip. I'm not too familiar with shell scripting. Here's
the error message I keep getting.


Script: F;\Wutemp\filecopy.vbs
Line: 49
Char: 1
Error: File not found
Code: 800A0035
Source: Microsoft VBScript runtime error

I hope this helps you better to understand my problem. The line number is
different now because I rearranged the code but the code that is talking
about is still the same one: "objFSO.CopyFile source, dest,
OVERWRITE_EXISTING"

I will try to seach the interenet for the code error but I just maybe
someone has troubleshot this before and knows just what to do. I'm pretty
much out of ideas.
 
G

Guest

Hey Marty,

Thanks for the help. Your tips on error checking helped me figure out the
problem. It took some researching on Microsoft website to fix the problem
but it work now. The issue is that fileobject.CopyFiles will only copy files
if they exist. In order to copy all the files from the root folder without
knowing what they are, I had to write a sub routine to check for file
existence before copying it. Thanks for your help.
 
M

Marty List

The error description is "File not found" which usually means there are no files
to copy from "source". If the folder didn't exist or you did not have security
access then the error should be different. The MSDN documentation says "An
error also occurs if a source using wildcard characters doesn't match any files"
(http://msdn.microsoft.com/library/en-us/script56/html/94e39f9a-c7bf-42be-ae71-5768f034e070.asp)

So maybe there are no files to copy in the root folder? As a test, just create
a temporary file named R:\Test.txt and then run the script. If that's the case
you will need to add more error checking to your script.

OPTION 1. You could check for files first, before calling CopyFile(), something
like this:
''''''''''''''''''''''''''''''''''''''''''''''
objStream.Writeline "File Copy Job Start - " & Time & Date
SourceParent = objFSO.GetParentFolderName(source)
If objFSO.GetFolder(SourceParent).Files.Count > 1 Then
objStream.Writeline "Copying root files from " & source
objFSO.CopyFile source, dest, OVERWRITE_EXISTING
Else
objStream.Writeline "No files found to copy from " & source
End If

''''''''''''''''''''''''''''''''''''''''''''''


OPTION 2: Enable automatic error handling with "On Error Resume Next":
''''''''''''''''''''''''''''''''''''''''''''''
On Error Resume Next
objFSO.CopyFile source, dest, OVERWRITE_EXISTING
If Err.Number <> 0 Then
If Err.Description <> "File not found" Then
objStream.Writeline "Failed to copy " & source & ", " & Err.Description
End If
End If
On Error GoTo 0
''''''''''''''''''''''''''''''''''''''''''''''

Your script has no error checking whatsoever, you should read up on error
handling:
http://msdn.microsoft.com/library/en-us/script56/html/0675d0b2-5c1a-4f20-94f3-6749c74984a9.asp
http://www.microsoft.com/technet/scriptcenter/guide/sas_vbs_efuo.mspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top