Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Incomplete File Copy objFSO.CopyFile

Status
Not open for further replies.

stvnkrs

Technical User
Sep 17, 2001
16
US
My VBscript that has worked well for years copies text files between network folders. In recent months, the copied files are occasionally incomplete, with only 13-15 of the 20 records in the file. I added a wscript.sleep command for 1 minute to allow time for the copy but it still occurs. Any ideas please? Thanks!
 
Show your code if you want comments on it.

I hope that helps.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Code snippet:

If (Not objFSO.FileExists("U:\UPS_Backup\Archive\" & CurrFileName & ".UPS" ) and Not objFSO.FileExists("U:\UPS_Backup\Archive\" & CurrFileName & ".txt" )) Then
If datevalue(CurrFile.DateCreated) = datevalue(now) Then
objFSO.CopyFile strFile, "U:\UPS_Backup\Archive\"
wscript.sleep(60000)
Else
End If
Else
'wscript.echo "not copying"
End If
 
As this is only a snippet I can't put everything into context. Make sure you have created objFSO above. Also make sure you are checking for the destination drive letter.

I also notice that you use CurrFile when checking the date but use CurrFileName above that. Since this is only a snippet I cannot say if that is supposed to be like that or not but it would appear to be a mistake.

I hope that helps.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
The script works perfectly 98% of the time and used to work 100% of the time until recently. Here's the full code:

strComputer = "." 'Local Computer running on this script
set objFSO = CreateObject("Scripting.FileSystemObject")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\cimv2")
Set colMonitoredEvents = objWMIService.ExecNotificationQuery _
("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE " _
& "Targetinstance ISA 'CIM_DirectoryContainsFile' and " _
& "TargetInstance.GroupComponent= " _
& "'Win32_Directory.Name=""U:\\\\UPS_Backup""'") 'Monitor the files in Folder

wscript.Echo "Script Started"

Do
Set objLatestEvent = colMonitoredEvents.NextEvent

strFile = objLatestEvent.TargetInstance.PartComponent
strFile = Replace(Mid(strFile, InStr(strFile, Chr(34)) + 1), "\\", "\")
strFile = Left(strFile, Len(strFile) - 1)

Set CurrFile = objFSO.GetFile(strFile)
CurrFileName = right(strFile, 18)
CurrFileName = left(CurrFileName, 14)

If (Not objFSO.FileExists("U:\UPS_Backup\Archive\" & CurrFileName & ".UPS" ) and Not objFSO.FileExists("U:\UPS_Backup\Archive\" & CurrFileName & ".txt" )) Then
if datevalue(CurrFile.DateCreated) = datevalue(now) Then
objFSO.CopyFile strFile, "U:\UPS_Backup\Archive\"
wscript.sleep(60000)
Else
End If

Else
'wscript.echo "not copying"
End If

Set colFiles = objWMIService.ExecQuery _
("ASSOCIATORS OF {Win32_Directory.Name='U:\UPS_Backup\Archive'} Where " _
& "ResultClass = CIM_DataFile")

For Each objFile In colFiles
strExtension = objFile.Extension
strExtension = Replace(strExtension, "UPS", "txt")
strNewName = objFile.Drive & objFile.Path & objFile.FileName & "." & strExtension
errResult = objFile.Rename(strNewName)
Next


Loop

wscript.echo "End Script"
wscript.quit
 
What is the purpose of this:

Set CurrFile = objFSO.GetFile(strFile)
CurrFileName = right(strFile, 18)
CurrFileName = left(CurrFileName, 14)

Why not use the following?
Set CurrFile = objFSO.GetFile(strFile)
CurrFileName = CurrFile.Name


I hope that helps.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
To be honest, this is the first and only VBscript I've ever written and it was several years ago so there certainly could be better ways to accomplish this.

strFile
-the full folder path and file name

CurrFileName = right(strFile, 18)
-captures the file name and extension in the string without the folder path

CurrFileName = left(CurrFileName, 14)
-captures the file name in the string without the file extension

I use CurrFileName because I'm working with two different file extensions (.ups which I change to .txt).

Are you suggesting that the way I'm capturing the file name could be causing intermittent problems with copying the file content? Thanks again.
 
if it is working 98% of the time then i presume there is something else going on outside of the scope of the script? silly suggestion, and perhaps only an example of somethign which might effect your script. suppose that the file is generated with some content, then the content is updated with more information, like someone creating a text file then writing more information to it later. in this example would your script copy the file correctly, at the time in question, but then later on when you compare the two, the content is different? as another process has updated the original?

you are not trapping an error from the filecopy job? so i cant tell you if the file is really being copied correctly.
 
Mrmovie, I appreciate the suggestions and will look outside of the script. I will also add error trapping. The files contain "EOF" when they copy completely so I'm wondering if I can determine success by looking for this.

I've manually re-run the script on the exact same file a second time and it worked fine so maybe it's not my script. Thanks

 
That is a good point. I was unaware it would work on the same file when executed again. I was thinking that perhaps the file name was not conforming to the expected standard length.

I hope that helps.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top