Jump to content

I need help for a script


Go to solution Solved by Mateocedillo,

Recommended Posts

Hello,
I need help for this script, because it seems to not work correctly and I want you to help me find a solution please. The script tries to register on a server the number of times a program is executed, that is, the user each time it executes it registers itself on the server, and I don't know what I'm doing wrong because every time I execute it the number of times it adds one more number in the text document created, when I want it not to add one more number but the number that is in the file, add more 1, that is, add 1 to the number that is on the server plus execution . The sum of 1 means that the program was executed once, hence when it is executed more times it will add more one and so on, but I can't find a solution, I don't know what I'm doing wrong and I need your help. Below I attach the au3 file:
I will be grateful for your responses.

Runtime.AU3

Edited by Mateocedillo
Link to comment
Share on other sites

39 minutes ago, Subz said:

the wrong flags also for FileOpen, it's $FO_... not $FC_...

Good eye! 

Apparently $FC_OVERWRITE is a valid constant, but for FileCopy.  Unfortunately, it’s a 1, he needs a 2.

Code hard, but don’t hard code...

Link to comment
Share on other sites

39 minutes ago, Subz said:

Don't use FileOpen, just create the directory first and then use FileWrite($program & "\.txt", ...)

nb: Just noticed you were using the wrong flags also for FileOpen, it's $FO_... not $FC_...

Hello, thanks for your corrections.
I have applied them to the script. I had already created the folder manually before, and it works, but it keeps adding one more number to the file, when I want it to be a unique number, a number that is added +1 and not add a 1, if not add 1 to that number.

Link to comment
Share on other sites

And running this script more than 2 times, more numbers are generated when I want only that current number to be added and no other to be added, for example:
This program has been ran dialog 0123 Times
I don't know how to fix this, I don't know if I'm doing wrong math or it occurs to me that I must first remove the old number to add the current one. It's a bit cumbersome, but I hope it made me understand.
This program has been ran dialog 0123 Times

Link to comment
Share on other sites

1 hour ago, Mateocedillo said:

And running this script more than 2 times, more numbers are generated when I want only that current number to be added and no other to be added, for example:
This program has been ran dialog 0123 Times

Post your new code with the corrections.

Why? As @Subz pointed out, you were not truncating the file, but appending it.  It makes perfect sense why it was going 01234...

Anyway, another way to fix it would be to delete it just before you write it: 

If @error then
MsgBox(0, "Error", "Cannot get the data from the file.")
EndIf
;Write the current number of executions:
FileDelete($openfile) ;NEW CODE
$asRunned = FileWrite($openfile, $getdata)

 

Code hard, but don’t hard code...

Link to comment
Share on other sites

12 hours ago, JockoDundee said:

Post your new code with the corrections.

Why? As @Subz pointed out, you were not truncating the file, but appending it.  It makes perfect sense why it was going 01234...

Anyway, another way to fix it would be to delete it just before you write it: 

If @error then
MsgBox(0, "Error", "Cannot get the data from the file.")
EndIf
;Write the current number of executions:
FileDelete($openfile) ;NEW CODE
$asRunned = FileWrite($openfile, $getdata)

 

Below I attach the corrected code and you can strangely notice what I commented a few hours ago, and I have also applied the deletion of the file and write the new results, but it does not matter.

Runtime.AU3

Link to comment
Share on other sites

Try something like:

#include <File.au3>

_Example()

Func _Example()
    Local $sNumber, $sFilePath = @ScriptDir & "\Example.txt"
    If Not FileExists($sFilePath) Then
        ;~ If file doesn't exist, create a new blank file
        Local $hFilePath = FileOpen($sFilePath, 10)
        FileClose($hFilePath)
    Else
        ;~ Read file content
        $sNumber = FileRead($sFilePath)
        ;~ Check if the file is blank or not
        If StringStripWS($sNumber, 8) = "" Then
            ;~ If blank write the first count
            FileWrite($sFilePath, 1)
        Else
            ;~ Add + 1 and write the results to the first line of the file
            _FileWriteToLine($sFilePath, 1, Number($sNumber) + 1, True)
        EndIf
    EndIf
EndFunc

 

Link to comment
Share on other sites

Can you see if the following works for you:

#include <FileConstants.au3>
#include <FTPEx.au3>
$program = "Example"
Runscript()
func Runscript()
    ;Ftp:
    $sServer = "ftpupload.net"
    $sUsername = "xxx"
    $sPass = "pass123"
    Local $hOpen = _FTP_Open("Example")
    ;Ftp connect:
    Local $hConn = _FTP_Connect($hOpen, $sServer, $sUsername, $sPass)
    If @error Then
        MsgBox(16, "Error", "Unable to connect to server: Reason: " & @error)
    EndIf
    ;Set variable for file:
    $openfile = $program &".txt"
    ;Get the current execution number from the server.
    $abririnternet = _FTP_FileOpen ($hConn, "htdocs/runs/" &$program &".txt")
    If @error then
        MsgBox(0, "Error", "Cannot create file.")
    EndIf
    ;Read statistics from ftp:
    $getdata = _FTP_FileRead($abririnternet, 8)
    If @error then
        MsgBox(0, "Error", "Cannot get the data from the file.")
    EndIf
    $rundata = _RunData($getdata)
    _FTP_FileClose ($abririnternet)
    sleep(1000)
    ;Send the file with the sum of the new execution:
    $Enviararchivo = _FTP_FilePut ($hConn, $program &".txt", "htdocs/runs/" &$program &".txt")
    if @error then
        MsgBox(0, "Ops", "The file could not even be sent")
    EndIf
    MsgBox(0, "This program has been ran", $rundata &" Times")
    FileDelete($program &".txt")
    Local $iFtpc = _FTP_Close($hConn)
    Local $iFtpo = _FTP_Close($hOpen)
EndFunc

Func _RunData($_vGetData)
    Local $vGetData = Number(StringStripWS($_vGetData, 8))
    Local $sFilePath = @ScriptDir & "\" & $program & ".txt"
    ;~ Create a new blank file
    Local $hFilePath = FileOpen($sFilePath, 10)
        FileClose($hFilePath)
    If $vGetData = "" Then
        FileWrite($sFilePath, 1)
    Else
        FileWrite($sFilePath, Number($vGetData) + 1)
    EndIf
    Return $vGetData + 1
EndFunc

 

Link to comment
Share on other sites

3 hours ago, Subz said:

Can you see if the following works for you:

#include <FileConstants.au3>
#include <FTPEx.au3>
$program = "Example"
Runscript()
func Runscript()
    ;Ftp:
    $sServer = "ftpupload.net"
    $sUsername = "xxx"
    $sPass = "pass123"
    Local $hOpen = _FTP_Open("Example")
    ;Ftp connect:
    Local $hConn = _FTP_Connect($hOpen, $sServer, $sUsername, $sPass)
    If @error Then
        MsgBox(16, "Error", "Unable to connect to server: Reason: " & @error)
    EndIf
    ;Set variable for file:
    $openfile = $program &".txt"
    ;Get the current execution number from the server.
    $abririnternet = _FTP_FileOpen ($hConn, "htdocs/runs/" &$program &".txt")
    If @error then
        MsgBox(0, "Error", "Cannot create file.")
    EndIf
    ;Read statistics from ftp:
    $getdata = _FTP_FileRead($abririnternet, 8)
    If @error then
        MsgBox(0, "Error", "Cannot get the data from the file.")
    EndIf
    $rundata = _RunData($getdata)
    _FTP_FileClose ($abririnternet)
    sleep(1000)
    ;Send the file with the sum of the new execution:
    $Enviararchivo = _FTP_FilePut ($hConn, $program &".txt", "htdocs/runs/" &$program &".txt")
    if @error then
        MsgBox(0, "Ops", "The file could not even be sent")
    EndIf
    MsgBox(0, "This program has been ran", $rundata &" Times")
    FileDelete($program &".txt")
    Local $iFtpc = _FTP_Close($hConn)
    Local $iFtpo = _FTP_Close($hOpen)
EndFunc

Func _RunData($_vGetData)
    Local $vGetData = Number(StringStripWS($_vGetData, 8))
    Local $sFilePath = @ScriptDir & "\" & $program & ".txt"
    ;~ Create a new blank file
    Local $hFilePath = FileOpen($sFilePath, 10)
        FileClose($hFilePath)
    If $vGetData = "" Then
        FileWrite($sFilePath, 1)
    Else
        FileWrite($sFilePath, Number($vGetData) + 1)
    EndIf
    Return $vGetData + 1
EndFunc

 

Hello,
The _Getdata function is nice, the code you corrected works but there is a part that does not.
The counting of numbers already does it well, but now the problem is that I have realized that if you execute more than 2 times the _FTP_FileRead does not work correctly, that is, it does not read the data that is in the text file $program &".txt", if not that if we debug that variable with an MSGBox, this happens:
$getdata = _FTP_FileRead($abririnternet, 😎
MsgBox(0, "results from $getdata",     $getdata)
The second time I run it and put an MSGBox to check the $getdata variable, I find this:
$getdata  dialog  0x31
And finally,
This program has been ran  dialog  50 Times
Now the problem is reading the file from the FTP, the rest is fine. I don't know if I have to put the correct number of bits to read, but sometimes the number of bits can change, I have already experimented by setting that parameter to 1, to 2, but everything is the same, the reading of the ftp file is not the text document, if not a 0xx code.
Anyway, thanks for the help, half the script was solved. I do not know the mystery of reading data in FTP, maybe Autoit does not interpret it well.

Link to comment
Share on other sites

  • Solution

Thank you all very much for the help, I was finally able to fix the problem with the script using _FTP_FileGet.
Really, thank you all.
In case you want, you can visit my GitHub where I have most of the scripts for autoIt, although it is not much of the case. I am not that participatory in this forum, but there I leave it:
Maybe in a short time I will put this script in one of my repositories.
https://github.com/rmcpantoja

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