Jump to content
BPCM

Returning System Error Code with FileCopy

Recommended Posts

Hello,

I need to return & process a Windows System Error Code when copying files. Using the sample below, I'm able to return an Error Code, but it returns code "18 There are no more Files." on a successful completion, instead of the expected 0 as per something like 'xcopy'.

#include <WinAPIError.au3>
FileCopy("source.foo", "destination.bar", 9)
$iLastError = _WinAPI_GetLastError()
$sLastErrorMessage = _WinAPI_GetLastErrorMessage()
ConsoleWrite($iLastError & " - " & $sLastErrorMessage & @CRLF)

I could use the return value of FileCopy itself and overwrite the System Code, but I'm not sure if this is a safe assumption.

#include <WinAPIError.au3>
Local $iFileCopy = FileCopy("source.foo", "destination.bar", 9)
$iLastError = _WinAPI_GetLastError()
If $iFileCopy = 1 Then $iLastError = 0
ConsoleWrite($iLastError & " - " & _WinAPI_GetErrorMessage($iLastError) & @CRLF)

On success, this yields, "0 - The operation completed successfully.".

Do you know if there is a cleaner way to accomplish this? Or if it is safe to simply use the code provided?

Thank you,

BPCM

 

Share this post


Link to post
Share on other sites

My use case is a little more complicated. I'm copying one file to many servers over a network share. Please see sample below:

#include <WinNet.au3>
Local $sShare = "C$", $iLastError
Local $sServerShare = "\\" & $sHostname & "\" & $sShare
Local $bNetConnection = _WinNet_AddConnection2("", $sServerShare, $sUsername, $sPassword, 1)
Local $iFileCopy = FileCopy($sFile, $sServerShare & "\FOO\", 9)
$iLastError = _WinAPI_GetLastError()

Knowing if the file copied or not is OK, but I'd like to be able to return why the copy failed. This could be anything from a permissions issue, to a hard drive being out of space. With so many servers it would be hard to triage each and every failure without an error code.

Edited by BPCM
Grammar

Share this post


Link to post
Share on other sites

Would this work? It'll only return an error if it fails.

#include <WinAPIError.au3>
If Not FileCopy("source.foo", "destination.bar", 9) Then
    $iLastError = _WinAPI_GetLastError()
    $sLastErrorMessage = _WinAPI_GetLastErrorMessage()
    ConsoleWrite($iLastError & " - " & $sLastErrorMessage & @CRLF)
EndIf

P.S. I have no idea why the AutoIt formatting isn't working.

Edited by Sidley

Share this post


Link to post
Share on other sites
3 hours ago, Sidley said:

P.S. I have no idea why the AutoIt formatting isn't working.

After posting the answer, refresh the page :)


Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites

Oh! that UDF looks like it's exactly what I want. I'll experiment with it today and let you all know how it goes. I really appreciate the help with this.

Share this post


Link to post
Share on other sites

After some testing, it seems like the UDF does not return the correct error code. I think leveraging _WinAPI_GetLastError() and returning 0 is my best bet for now.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...