Rename fails in Power Saver mode

cweiland

New member
Local time
Today, 01:03
Joined
May 9, 2018
Messages
2
Hello,

I have an odd issue.

In VBA, I first delete a file (e.g. FileName.pdf) and then rename another file (e.g. FileName2.pdf) to the name of the deleted file (e.g. FileName.pdf):

The code snippet is:

Code:
varTemp = funDeleteFile(strPath & "" & strFile)
If varTemp <> gcstrfunDeleteFile_Success Then
   funPrintISS = funPrintISS & " " & varTemp & "."
   GoTo lblExit
End If

varTemp = funRenameFile(strMergeFile, strPath & "" & strFile)
If varTemp <> gcstrfunRenameFile_Success Then
    funPrintISS = funPrintISS & " " & varTemp & "."
    GoTo lblExit
End If

This works fine as long as not switching my notebook from power plan “Balanced” to “Power Saver”.

If I do this then the rename function fails.

The code of the function is:

Code:
Function funRenameFile(sSourceFile As String, sDestinationFile As String)
Dim lngTimeElapsed As Long

Const clngTimeWait As Long = 500            ' [ms]
Const clngTimeMax As Long = 10000           ' [ms]

funRenameFile = gcstrfunRenameFile_Error
    
On Error GoTo lblError

Name sSourceFile As sDestinationFile
  
lblRetry:

' Wait until the file is renamed
lngTimeElapsed = 0
Do Until (Dir(sDestinationFile) <> "")
    Sleep clngTimeWait
    lngTimeElapsed = lngTimeElapsed + clngTimeWait

    ' it takes too long -> error
    If (lngTimeElapsed >= clngTimeMax) Then

        ' set return value = error message
        funRenameFile = "TIMEOUT (" & lngTimeElapsed & " ms) deleting file."

        GoTo lblExit
    End If

    DoEvents
Loop

' regular end
funRenameFile = gcstrfunRenameFile_Success

lblExit:

On Error Resume Next
DoCmd.Hourglass False

Exit Function

' --- error-handling routines --- --- --- --- --- --- ---
lblError:

If (lngTimeElapsed >= clngTimeMax) Then
    
    funRenameFile = funRenameFile & " " & " <Error (" & Err.Number & "): " & Err.Description & ">"
    
    Resume lblExit

Else
    
    Sleep clngTimeWait
    lngTimeElapsed = lngTimeElapsed + clngTimeWait
    
    Resume lblRetry

End If

End Function

In “Power Save” mode, the rename fails and returns a time out.

If I add “sleep 250” between the delete and the rename then it works OK, but I don't want to slow down the overall functionality. Additionally, it could be that 250 ms is not enough if run on a slower machine. So, sleep is not really an option.

What is really odd, is that if I remove “On Error GoTo lblErr” then the rename works! But I don’t want to remove the error handling.:banghead:

Somebody got an idea how to solve that?

Regards
Christian
 
If you can find code that detects whether power saver mode is on, you could use an if...else cause to modify the max time accordingly
 
For your laptop, the hard drive is physical, correct? As such, when it receives the "Rename" it should have to wait a moment while the drive spins up so it can update the file name. I'm surprised you don't have this issue with the delete.

P.S. Had this type of issue a long time ago when trying to access a file on disk. Found out sleeping computers don't like telling you what files are around under 95 if the drive isn't spinning...
 
Hello guys,

thanks a lot for the replies.

Checking the system setting and adjusting the sleep time is an idea but seems a little too unsafe for my taste.

Maybe I should check the file size as well and not only the presence of the file.

I thought about using the Windows API command for delete and rename but didn't find a way to do it so far. By the way, I get the error, in the meantime, during delete as well.

The hard drive is an SSD, so no “spin-up time” here but I think that in power saver mode the OS behaves differently, with some kind of delay.

Regards
Christian
 
If you DO find a solution, please let us know! As I posted, the only time I'd ever seen this was when the disk drive needed to spin up first. IIRC it was a manufacture-specific issue from a long time ago.
 

Users who are viewing this thread

Back
Top Bottom