Jump to content

Recommended Posts

Posted

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

 

Posted

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
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Posted (edited)

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
Posted (edited)

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
Posted

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
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Posted

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
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Posted

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.

Posted

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.

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...