BPCM Posted September 9, 2019 Share Posted September 9, 2019 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 comment Share on other sites More sharing options...
seadoggie01 Posted September 9, 2019 Share Posted September 9, 2019 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 functionsAcro.au3 UDF - Automating Acrobat ProToDo Finder - Find #ToDo: lines in your scriptsUI-SimpleWrappers UDF - Use UI Automation more Simply-erKeePass UDF - Automate KeePass, a password managerInputBoxes - Simple Input boxes for various variable types Link to comment Share on other sites More sharing options...
BPCM Posted September 11, 2019 Author Share Posted September 11, 2019 (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 September 11, 2019 by BPCM Grammar Link to comment Share on other sites More sharing options...
Sidley Posted September 11, 2019 Share Posted September 11, 2019 (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 September 11, 2019 by Sidley Link to comment Share on other sites More sharing options...
seadoggie01 Posted September 11, 2019 Share Posted September 11, 2019 Oh, that makes more sense 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 functionsAcro.au3 UDF - Automating Acrobat ProToDo Finder - Find #ToDo: lines in your scriptsUI-SimpleWrappers UDF - Use UI Automation more Simply-erKeePass UDF - Automate KeePass, a password managerInputBoxes - Simple Input boxes for various variable types Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted September 11, 2019 Share Posted September 11, 2019 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 ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
seadoggie01 Posted September 11, 2019 Share Posted September 11, 2019 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 functionsAcro.au3 UDF - Automating Acrobat ProToDo Finder - Find #ToDo: lines in your scriptsUI-SimpleWrappers UDF - Use UI Automation more Simply-erKeePass UDF - Automate KeePass, a password managerInputBoxes - Simple Input boxes for various variable types Link to comment Share on other sites More sharing options...
BPCM Posted September 12, 2019 Author Share Posted September 12, 2019 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. Link to comment Share on other sites More sharing options...
BPCM Posted September 13, 2019 Author Share Posted September 13, 2019 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. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now