Jump to content
JuanFelipe

Help with variable

Recommended Posts

JuanFelipe

Hello friends, I have a simple question, can I have a variable that is not reset, even if my program is closed?
That is, I want to make a program that can only be opened once, have a variable $ ini = 0, when it is opened for the first and only time it becomes $ ini = 1, so that it can not use the program a second time, as if it was a test software.
I do not want the variable to depend on a reading of an external file that is easily manipulated. Thank you.

Share this post


Link to post
Share on other sites
JLogan3o13

Variables, by their nature, live while your program is alive. You cannot expect the computer to remember the variable setting between runs of your program unless you are writing it somewhere outside of memory (it is called volatile for a reason). That would be an INI file, a Registry entry, a DLL even, but somewhere on the physical drive.

If you do not want to use a file, go with a registry entry written on first run and then checked on subsequent runs.

  • Like 1
  • Thanks 1

√-1 2^3 ∑ π, and it was delicious!

How to get your question answered on this forum!

Share this post


Link to post
Share on other sites
FranckGr

Why not FileDelete(@ScriptFullPath) at the end of your script ...

  • Thanks 1

Share this post


Link to post
Share on other sites
JuanFelipe

Thanks

 

Share this post


Link to post
Share on other sites
mikell
22 hours ago, FranckGr said:

Why not FileDelete(@ScriptFullPath) at the end of your script ...

Just keep a copy of the program and then you can run it twice - and so on  :D

Share this post


Link to post
Share on other sites
Chimp
Posted (edited)

supposing you are using a compiled script, here a simple 'experiment' to have a program runnable only once.
In few words, after the first run, I just append a byte at the end of the exe setted to "1" so at the next run we found the "1" as the last byte and we exit.
just a funny experiment, no claim of bullet-proofness...

If Not @Compiled Then Exit MsgBox(0, "Info", "script not compiled error.")
Local $sThis = @ScriptFullPath

Local $hHndl = FileOpen($sThis, 16) ; open the exe in byte mode
FileSetPos($hHndl, -1, 2) ; $FILE_END (2) -1 = last byte of the file
$LastByte = FileRead($hHndl, 1) ; get last byte

If $LastByte = "1" Or $LastByte = 0x31 Then
    MsgBox(0, "Debug " & $LastByte, "I'm sorry, this program has expired", 5)
    Exit
EndIf

; your one time run program here below
; ------------------------------------
MsgBox(0, "Debug " & $LastByte, "This is a 'one time only' run program" & @CRLF & "next time this program will not run")
;
;
; on exit add a byte to the exe to flag "no more run"
; ------------------------------------
; ping is just to wait 2 seconds so to give time to the main exe to terminate
; You can't write a byte to the exe if is running
Local $sCommand = 'ping localhost -n 2 > nul & echo | set /p dummy="1" >> ' & $sThis
; the command is run 'detached' from the main exe
Run(@ComSpec & " /c " & $sCommand) ; , @SW_HIDE)

 

Edited by Chimp
removed the show_flag from the run command

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites
mikell

Chimp, this doesn't work for me...
Isn't the last on-exit command a part of the still running exe ?

Share this post


Link to post
Share on other sites
Chimp
Posted (edited)

hi @mikell

the last run command delegates the dos environment to execute the 2 concatenated commands in a detached way from the main script. The ping command is there as a sort of delay of about 2 seconds to give time to the main exe to terminate so that it is not locked when the second command will append the last byte to the exe.
However a weird fact happens.... if the show_flag parameter of the run command is set @sw_hide or @sw_minimize then the write of the byte fails.  I've seen that just removing the show_flag  parameter from the run command allows it to work.
...maybe the echo fails if command prompt is hidden...??
suggestions and alternatives to write a byte from dos environment to a file are welcome

Edited by Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites
jchd

Another way but NTFS-only. Kid sister will bypass this but most users won't.

#include <Date.au3>

If Not @Compiled Then Exit MsgBox(0, "Info", "Script must be compiled to run (once only).")

Local $sFname = @ScriptFullPath & ":Dead Beef"

If FileExists($sFname) Then
    MsgBox(0, "Run status", "Sorry this program has expired. It already ran on " & FileRead($sFname), 5)
    Exit
Else
    _Main()
EndIf

Func _Main()
    MsgBox(0, "Welcome", "This is a 'one time only' runable program" & @CRLF & "Next time this program will not run.")

    ; program core here

    FileWrite($sFname, _NowCalc())
EndFunc

 


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
Simpel

As a colon is not allowed in windows inside a file name, what is happening there?


SciTE4AutoIt = 3.7.3.0   AutoIt = 3.3.14.2   AutoItX64 = 0   OS = Win7Pro SP1   OSArch = X64   Language = 0407/german
H:\...\AutoIt3\SciTE     H:\...\AutoIt3      H:\...\AutoIt3\Include     (H:\ = Network Drive)

   88x31.png  Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind.

Share this post


Link to post
Share on other sites
BrewManNH

Alternate File Stream data.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
jchd
2 hours ago, Simpel said:

As a colon is not allowed in windows inside a file name, what is happening there?

My example demonstrates a colon there is perfectly valid syntax.
The magic is named as @BrewManNH said an ADS (Alternate Data Stream).


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
BrewManNH
8 minutes ago, jchd said:

ADS (Alternate Data Stream)

No idea why I keep reversing those.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
jchd

idea The is same the. :D


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
JuanFelipe
Thank you very much to all for your answers, I tell them how I did it, in the end I created a .log file, with information encrypted inside, where I give guidelines to the program of how many times it can be executed and also the limit of queries that can be made , I read them all and I'm going to try the things they told me to see how it goes. Thanks brothers. :ILA2:

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

  • Similar Content

    • SharpDressedMan
      By SharpDressedMan
      I have an array of subarrays, eg:
      local $a[3] = [ 1, 2, 3 ] local $b[3] = [ 4, 5, 6 ] local $c[2] = [ $a, $b ] I can read read a subarray element of the array, as follows:
      local $c12 = ($c[1])[2] ; result: $c12 = 6 However, when I try to set a subarray element as follows, it fails:
      ($c[1])[2] = 12 ; this fails to set subarray element local $c12 = ($c[1])[2] ; result: $c12 = 6 How to set a subarray element ?
      Thanks
    • nooneclose
      By nooneclose
      Good morning everyone, I thought I had already solved this issue but it turns out I did not. 
      My code finds unread emails with this specific subject line of "request" but when I change the subject to SKIPPED + "request" = ("SKIPPED request") the program still finds the email and tries to process it. I only want to process emails with the exact match subject of "request".
      Here is my code that "finds" the unread emails with the subject of "request" or so I thought. 
      Func ListUnreadEmails() ;******************************************************************************* ; Lists all unread E-mails from the folder Outlook-UDF-Test ;******************************************************************************* ; Stores all the unRead emails into an array Global $aItems = _OL_ItemFind($oOutlook, "*\Outlook-UDF-Test", $olMail, _ "[UnRead]=True", "Subject", "request", "EntryID,Subject", "", 1) ; Displays the array of unRead emails If IsArray($aItems) Then ;_ArrayDisplay($aItems, "OutlookEX UDF: _OL_ItemFind - Unread mails") Else MsgBox(48, "OutlookEX UDF: _OL_ItemFind Example Script", _ "Could not find an unread mail. @error = " & @error & ", @extended: " & @extended) EndIf ; Gets the number of unread emails Global $numberOfUnRead = UBound($aItems, $UBOUND_ROWS) - 1 ;MsgBox("", "Number of Unread emails", $numberOfUnRead) EndFunc It acts as if any part of the subject containing the word "request" and the email is unread that it will try to process it. (I think)
    • KhalidAnsari
      By KhalidAnsari
      Hi,
      I need to automate Java application using AutoIt, I have refer forum for the same, as per forum I am using Java Access Bridge to finding control name. But could not set value to control. Below is my sample code 
      Send("#r")
      sleep(1500)
      Send("D:\javaapplication\javaapplication\Hello.jar{Enter}")
      WinActivate("Penjumlahan")
      sleep(1500)
      ControlSetText("Penjumlahan", "", "Name:  A :", "demo")
      Java Application and Java Ferret screen

    • QaisBsharat
      By QaisBsharat
      Hello, 
      May i know how can i automation qt application?
      Using controlclick not mouse and positions
       
      Thanks
    • nooneclose
      By nooneclose
      Good afternoon, I am in need of some help. I am sure this is a stupid question requiring only one or two lines of code. However, I would greatly appreciate the help I cannot figure this out. I also tried searching for the answer on the internet but no one except me apparently seems to be having a hard time figuring this out and or is asking about it. 
      I simply want to change the "status" of an email from unread to read once I have processed it. My code is over 500 lines and I would like not to clutter this post with it. Assume I have all my includes and connections properly defined and stuff. 
      Here is the bit of code where I am trying to change the email that was used from unread to read:
      Func ChangeEmailStatus() ;******************************************************************************* ; changes the status of an email from unread to Read ;******************************************************************************* Local $iRows = UBound($aItems, $UBOUND_ROWS) MsgBox("", "Number of Unread emails (Before Change)", $iRows) _OL_ItemModify($oOutlook,$aItems[$i][0], Default, "Read=True") MsgBox("", "Array Display 1", $aItems[1][0]) MsgBox("", "Array Display 2", $aItems[2][0]) Local $iRows = UBound($aItems, $UBOUND_ROWS) MsgBox("", "Number of Unread emails (After Change)", $iRows) EndFunc  
×