Jump to content

Check if included file is called


Recommended Posts

Hello,

 

during development I often work in a script that I include in the main program. The included script has only functions that are called from the main script.

When devloping I press often F5 to run the script and when doing this in the include nothing happens. Is there a way to generate an error when I try to run a included file?

Only thing I can imagine write now is to declare a variable in the main script and check with IsDeclared in the included file and generate a MsgBox. Any better ideas?

Link to comment
Share on other sites

  • Developers
3 hours ago, HurleyShanabarger said:

Is there a way to generate an error when I try to run a included file?

Sure, just add this at the top of the Include file and adapt the "includename.au3" the real name of the include file. ;) 

if @ScriptName = "includename.au3" then
    msgbox(0, "NO you ....", " ..... dumbass ... you are doing it again!!!!! ;)")
EndIf

 Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

#include-once
;~ #include <DontRunIncludeOnce.au3> ; include this UDF in all your other UDFs.
__DontRunIncludeOnce()
; #INTERNAL_USE_ONLY# ===========================================================================================================
; Name ..........: __DontRunIncludeOnce
; Description ...: it runs to stop user from waiting on something that will not happen.
; Syntax ........: __DontRunIncludeOnce()
; Parameters ....: None
; Return values .: None
; Author ........: argumentum
; Modified ......:
; Remarks .......: Just include this UDF in your other UDFs. Exit error is set to 3.
;                  Save this code as "DontRunIncludeOnce.au3" in the path of your includes.
; Related .......:
; Link ..........: https://www.autoitscript.com/forum/index.php?showtopic=205963&view=findpost&p=1482941
; Example .......: Yes, this is also the example. If you run this UDF it will show what it does.
; ===============================================================================================================================
Func __DontRunIncludeOnce()
    If Not StringRight(@ScriptName, 4) = ".au3" Then Return
;~  ConsoleWrite('+ Func DontRunIncludeOnce()' & @CRLF)
    Local $n, $aArray = StringSplit(FileRead(@ScriptFullPath, 2048), @CRLF)
    For $n = 1 To $aArray[0]
        If StringInStr(StringStripWS($aArray[$n], 8), "#include-once") = 1 Then
            MsgBox(0, @ScriptName, "oops on:" & @CR & @ScriptName & @CR & @CR & "this is an ""#include"" file", 3)
            Exit 3
        EndIf
    Next
EndFunc   ;==>__DontRunIncludeOnce

I wasn't going to post. Jos' solution is as good as it gets.
But then again, I'm a copy'n'paste coder and just as prone to err.
With this, you can just copy and paste #include <DontRunIncludeOnce.au3>.
It's easier :)

Edit: This change is thanks to @JockoDundee observation.

Edited by argumentum
better code

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...