Jump to content

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

 

Link to post
Share on other sites

Why do you need the Windows System Error Code? Doesn't this work for you?

If FileCopy('source.foo', 'destination.bar', 9) Then
    ConsoleWrite('Copied!')
Else
    ConsoleWrite('Failed!')
EndIf

 

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts

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
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
Link to post
Share on other sites

Oh, that makes more sense :D

Could you use xcopy with ShellExecute, or does that not provide enough error information?

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts

Link to post
Share on other sites

I found this UDF, which appears to work from CopyFileExA, which provides error messages via GetLastError. You could try it :)

 

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts

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
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...