| || |
CopyFile and MoveFile methods not working properly if not in Debug mode
Environment: Windows 7 Enterprise, UFT 12.00
Some really strange behavior when running in Debug mode vs non-Debug mode.
- Folder2 contains the old/previous .png file
- Use DeleteFile method to delete the file (works well in both modes)
- Use CreateBitmap to create a new .png file in Folder1 (with same filename as the one deleted from Folder2)
- Use CopyFile or MoveFile method to copy or move the new .png file from Folder1 to Folder2
If old/previous .png file in Folder2 has time stamp of 10:00am and the new .png file in Folder1 has time stamp of 11:00am, running the script in Debug mode will result to having the Folder2 file copied or moved with the new time stamp.
If the same script is run in non-Debug mode and you watch the activities in Windows Explorer in another monitor, you'll see that the file deletion in Folder2 was done, the file creation in Folder1 was done, the file copy or move was done but the time stamp is from the old 10:00am. Why?
I added Wait() times before copying or moving the file and after the CaptureBitmap created the new .png file but it did not make a difference.
Originally, I was just using the CaptureBitmap method setting the OverrideExisting=TRUE but the .png file is not getting overridden so I thought that for work-around, the CopyFile or MoveFile method will help.
Did you refresh the directory to verify it really still has the old timestamp?
Windows 7 is kind of buggy about that sometimes:
Windows Explorer doesn't refresh when moving/deleting - Part 3
If this still causes you an issue why don't you append the current date/time to the file name to ensure it's a unique file. This also gives you an audit trail for your screenshots.
Thanks for the link you provided. I'll check out people's suggestions in there. Looks like Microsoft never fixed this problem in Windows 7.
What really surprises me is why the problem is not encountered when I am running my script in Debug mode.
Refresh did not make a difference after running in non-Debug mode.
Mark: Yes, I could append the date/time to the file name but in this case, I didn't want to create too many files. We are supposed to check out 100's of PDF files for Accessibility 508 Compliance and one screenshot of the Report/Results should be enough to give the PDF Authors/Remediators. The other type results where I am getting the counts for Failed, Warned, Passed, yes, I do append the date/time.
Well if the manual refresh doesn't fix it then don't think it is that Windows 7 bug.
You may just need to get a fresh handle on that file/directory in fso right before the move/copy step.
SAPIEN Forums ? View topic - VBScript fileSystemObject Refresh Problem
Sorry, I'm not quite sure what you mean by getting a fresh handle.
Do you mean I have to create a separate/new File System Object to use for the copy or move and not use the fso that I used for the DeleteFile method?
Please clarify if get a chance.
Can you post up the code you are using now? Would be helpful in diagnosing.
Code sample below: (I just added a new FSO creation but still did not work)
'Work-around for CaptureBitmap not overwriting existing file - Delete file if it exist.
If objFSO.FileExists(strPDFResultsFolder & "PAC2 Screenshots\" & strPDFpng) Then
objFSO.DeleteFile strPDFResultsFolder & "PAC2 Screenshots\" & strPDFpng
strPngFile = strPDFResultsFolder & strPDFpng
' If objFSO.FileExists(strPngFile) Then
' objFSO.DeleteFile strPngFile,True 'Delete the png file.
' End If
'do screen capture of Results
'SwfWindow("SwfWin_PAC 2 - PDF Accessibility").CaptureBitmap strPDFResultsFolder & "PAC2 Screenshots\" & strPDFpng, True
SwfWindow("SwfWin_PAC 2 - PDF Accessibility").CaptureBitmap strPDFResultsFolder & strPDFpng, True
'Create File System Object
Set objFSO2 = CreateObject("Scripting.FileSystemObject")
'strPngFileCopy = strPDFResultsFolder & "PAC2 Screenshots\" & strPDFpng
'objFSO.CopyFile strPngFile, strPngFileCopy
'objFSO.MoveFile strPngFileCopy, strPDFResultsFolder & "PAC2 Screenshots\"
objFSO2.MoveFile strPngFile, strPDFResultsFolder & "PAC2 Screenshots\"
So sorry. I should have made my statements clearer.
Even if I run on Debug mode, the CopyFile or MoveFile method still fails as long as I click on the Run button instead of the Step Into button. Somehow, it copying or moving of the new file with is only happening when I Step Into each line of code from where I check if the FileExists through the CopyFile or MoveFile.
NOTE: If I manually delete the files I am trying to replace instead of using the DeleteFile method, then it works always.
Could it be that the problem has something to do with the DeleteFile method?
Let me try the GetFile with the Delete method this time to see if it makes any difference.
Just found out "The GetFile and Delete method did not make a difference."
Last edited by Gilbert; 08-19-2014 at 07:42 AM.
Reason: more info added
Sounds like some sort of sync issue if it works when stepping through.
Rather than guess at dumb wait values I would follow up your delete statement with an exist check(potentially loop until it no longer exists) after to be sure it is deleted before moving on.
Yes, I know. I will delete those Wait() statements. They were just "as experiment" to see if there is any timing issue on deleting the file but, it didn't make any difference. If others are not able to duplicate this issue on their machines, maybe it is only happening on my machine since our IT Group controls many things in our PCs like doing "updates" or "installs" without us knowing it.
The diffence in the results between executing the script through Step Into versus Run is something really weird though. HP Support is looking into this issue now.