Jump to content

Recommended Posts

Hi Guys,

Is it possible to get a variable on your For..Next loop? 
 

Local $Lines1 = _FileCountLines(C:\temp\test.txt)
Local $linesToCount2 = $Lines1 + 2
$var = Number($linesToCount2)

For $count = 1 To _FileCountLines($FileRead2) Step 1
    For $i = $var To $count
    Next
    ;Code does stuff here
Next

Somehow my code doesn't work even though I thought I could convert the variable to a Integer / Number.

This code I posted above does not move to the next value.

But the code below does... why is that?

For $count = 1 To _FileCountLines($FileRead2) Step 1
    For $i = 2 To $count
    Next
    ;Code does stuff here
Next

 

Why is the For loop resetting itself?
Is it because the program does not cache the variable and needs to keep on acquiring this variable each time?

If so , how would you make this variable static?

 

Edited by Skeletor

Share this post


Link to post
Share on other sites
14 minutes ago, Skeletor said:

$count = 1 To _FileCountLines($FileRead2) Step 1

Why do you need to do this? :)


Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites
For $count = 1 To _FileCountLines($FileRead2) Step 1

Made a mistake.. suppose to put the For in the front...updated Original Post.

Edited by Skeletor

Share this post


Link to post
Share on other sites

@Skeletor

#include <File.au3>

Global $intStart = _FileCountLines(@ScriptDir & "\File.csv"), _
       $intTo = 10, _
       $intStep = 1

ConsoleWrite("The For...Next...Step loop will count from " & $intStart & " to " & $intTo & " with the step of " & $intStep & @CRLF)

For $i = $intStart To $intTo Step $intStep
    ConsoleWrite($i & @CRLF)
Next

Content of File.csv:

"SomeValue";"SomeValue";"SomeValue";"SomeValue"
"SomeValue";"SomeValue";"SomeValue";"SomeValue"
"SomeValue";"SomeValue";"SomeValue";"SomeValue"
"SomeValue";"SomeValue";"SomeValue";"SomeValue"
"SomeValue";"SomeValue";"SomeValue";"SomeValue"

What's the issue then?
I am sorry if I'm not undestanding your question :)
 

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites

@FrancescoDiMuro, thanks for that, yes your code would work, but nest it inside another For Step Next loop.

That's my problem. Example like below... might be vague but hope you get to see what I'm trying here... 
 

Local $Lines1 = _FileCountLines(C:\temp\test.txt)
Local $linesToCount2 = $Lines1 + 2
$var = Number($linesToCount2)

For $count = 1 To _FileCountLines($FileRead2) Step 1
    For $i = $var To $count
    Next
    ;Code does stuff here
Next

 

Share this post


Link to post
Share on other sites

@Skeletor
Mh... Let's try another way :)

What is the goal of your script?
Maybe this approach is not the best for your case...
 

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites

@Skeletor If you used actual numbers you can see why your code won't work for example:

In this example we're assuming test.txt has 5 lines and FileRead2 has 10 lines, you can see how it will fail based on that code, but as FrancescoDiMuro pointed out you need to explain what you're trying to achieve.

Local $Lines1 = 5        ;~ _FileCountLines(C:\temp\test.txt)
Local $linesToCount2 = 7 ;~ $Lines1 + 2
$var = 7                 ;~ Number($linesToCount2) (You don't require Number() function since its already a number)

For $count = 1 To 10     ;~ _FileCountLines($FileRead2) Step 1
    For $i = 7 To 1      ;~ $var To $count (Won't work you need to use Step - 1) However when it reaches 7 it will need to go Step + 1
    Next
    ;Code does stuff here
Next

 

Share this post


Link to post
Share on other sites

Thanks @FrancescoDiMuro

The For Step Next loop works great, but only if the nested For Next loop start value is static... 
Somehow I would like to make that start value static. 

Aim:

Loop through file
Insert Data
Get how many lines in the file
Use that to go to the next line
Use For Next loop to go to the last line (mentioned in step above) and start appending the data from another csv file ....

Share this post


Link to post
Share on other sites

Guys, I have trimmed my code below.. 

Here is my aim, 

Read the first CSV file and insert into test.txt... then the next csv file must be read and start after the first one, 

;Read first CSV file... note the For loop has a static value, thats because I forced the starting postition.
For $count = 1 To _FileCountLines($FileRead2) Step 1
    $string = FileReadLine($FileRead2, $count)
    $data = StringSplit($string, ",", 1)
    $value1 = $data[1]
    $value2 = $data[2]


        For $i = 2 To $count
        Next

    FileWrite("C:\tmp\test.txt", $value1 "A" & $i)
    FileWrite("C:\tmp\test.txt", $value2 "B" & $i)

Next

;Next CSV below must start after the csv above
; CSV must start after the above csv... this must be dynamic
Global $Lines3 = _FileCountLines($FileRead2)
Local $linesToCount3 = $Lines1 + 2

For $count = 1 To _FileCountLines($FileRead3) Step 1
    $string = FileReadLine($FileRead3, $count)
    $data = StringSplit($string, ",", 1)
    $value1 = $data[1]
    $value2 = $data[2]


        For $i = $linesToCount3 To $count
        Next

    FileWrite("C:\tmp\test.txt", $value1 "A" & $i)
    FileWrite("C:\tmp\test.txt", $value2 "B" & $i)

Next

 

Edited by Skeletor
Code was wrong, corrected the code

Share this post


Link to post
Share on other sites

Couldn't you just use an array and add the values you want to a new array variable?

#include <Array.au3>
#include <File.au3>

Local $aFileRead1, $sFileName1 = @ScriptDir & "\FileName1.csv"
Local $aFileRead2, $sFileName2 = @ScriptDir & "\FileName2.csv"

_FileReadToArray($sFileName1, $aFileRead1, 0, ",")
_FileReadToArray($sFileName2, $aFileRead2, 0, ",")

_ArrayConcatenate($aFileRead1, $aFileRead2)

_ArrayDisplay($aFileRead1)

 

Share this post


Link to post
Share on other sites

@Skeletor
Essentially, you could easily work with arrays, instead of getting line by line your text, and writing it to the "destination file".
Expand as much as you want $arrSourceFiles; the content of all the source files will be written in the "Destination File" :)

#include <File.au3>
#include <FileConstants.au3>

Global $strDestinationFile = @ScriptDir & "\DestinationFile.txt", _
       $arrSourceFiles[] = [@ScriptDir & "\SourceFile1.csv", _
                            @ScriptDir & "\SourceFile2.csv"], _
       $arrFileContent, _
       $hdlDestinationFile


$hdlDestinationFile = FileOpen($strDestinationFile, $FO_APPEND)
If $hdlDestinationFile = -1 Then
    ConsoleWrite("Error while opening the file '" & $strDestinationFile & "'." & @CRLF)
Else
    For $i = 0 To UBound($arrSourceFiles) - 1 Step 1
        _FileReadToArray($arrSourceFiles[$i], $arrFileContent, $FRTA_NOCOUNT)
        If @error Then
            ConsoleWrite("Error while reading the file '" & $arrSourceFiles[$i] & "'. Error: " & @error & @CRLF)
        Else
            ConsoleWrite("The file '" & $arrSourceFiles[$i] & "' has been read correctly." & @CRLF)
            _FileWriteFromArray($hdlDestinationFile, $arrFileContent)
            If @error Then
                ConsoleWrite("Error while writing the data into '" & $strDestinationFile & "'. Error: " & @error & @CRLF)
            Else
                ConsoleWrite("The data read from the file '" & $arrSourceFiles[$i] & "' has been write correctly to '" & $strDestinationFile & "'." & @CRLF)
            EndIf
        EndIf
    Next

    FileClose($hdlDestinationFile)
EndIf

Here it is all the "sample" :)

Sample.zip

Edited by FrancescoDiMuro

Click here to see my signature:

Spoiler

Thoughts:

  • I will always thank you for the time you spent for me.
    I'm here to ask, and from your response, I'd like to learn.
    By my knowledge, I can help someone else, and "that someone" could help in turn another, and so on.

/*--------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

ALWAYS GOOD TO READ:

 

Share this post


Link to post
Share on other sites

Thanks guys, I was also checking why this did not work.

I tested the For Next loop and from 1 to 3 , I get the loop counting. Meaning 
 

For
    For $i = 1 To $count
    Next
Next
; Each cycle increments by 1 
; starts with 1,2,3,etc...
;////////////////////////////////////////////
For
    For $i = 2 To $count
    Next
 Next
; Each cycle increments by 1 
; starts with 2,3,4,etc...
;////////////////////////////////////////////
For
    For $i = 3 To $count
    Next
Next
; Each cycle increments by 1 
; starts with 3,4,5,etc...
;////////////////////////////////////////////
For
    For $i = 4 To $count
    Next
Next
; Does not increment
; Static is 4 on each cycle
;////////////////////////////////////////////
For
    For $i = 5 To $count
    Next
Next
; Does not increment
; Static is 5 on each cycle
;////////////////////////////////////////////

and the higher you go it does not loop.. Is this a bug?

Share this post


Link to post
Share on other sites
1 hour ago, Skeletor said:

Read the first CSV file and insert into test.txt... then the next csv file must be read and start after the first one, 

;Read first CSV file... note the For loop has a static value, thats because I forced the starting postition.
For $count = 1 To _FileCountLines($FileRead2) Step 1
    $string = FileReadLine($FileRead2, $count)
    $data = StringSplit($string, ",", 1)
    $value1 = $data[1]
    $value2 = $data[2]


        For $i = 2 To $count
        Next

    FileWrite("C:\tmp\test.txt", $value1 "A" & $i)
    FileWrite("C:\tmp\test.txt", $value2 "B" & $i)

Next

;Next CSV below must start after the csv above
; CSV must start after the above csv... this must be dynamic
Global $Lines3 = _FileCountLines($FileRead2)
Local $linesToCount3 = $Lines1 + 2

For $count = 1 To _FileCountLines($FileRead3) Step 1
    $string = FileReadLine($FileRead3, $count)
    $data = StringSplit($string, ",", 1)
    $value1 = $data[1]
    $value2 = $data[2]


        For $i = $linesToCount3 To $count
        Next

    FileWrite("C:\tmp\test.txt", $value1 "A" & $i)
    FileWrite("C:\tmp\test.txt", $value2 "B" & $i)

Next

 

couldn't work , the inner For ... next is skipped as begin is greater end.

Share this post


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

@Skeletor
Essentially, you could easily work with arrays, instead of getting line by line your text, and writing it to the "destination file".
Expand as much as you want $arrSourceFiles; the content of all the source files will be written in the "Destination File" :)

#include <File.au3>
#include <FileConstants.au3>

Global $strDestinationFile = @ScriptDir & "\DestinationFile.txt", _
       $arrSourceFiles[] = [@ScriptDir & "\SourceFile1.csv", _
                            @ScriptDir & "\SourceFile2.csv"], _
       $arrFileContent, _
       $hdlDestinationFile


$hdlDestinationFile = FileOpen($strDestinationFile, $FO_APPEND)
If $hdlDestinationFile = -1 Then
    ConsoleWrite("Error while opening the file '" & $strDestinationFile & "'." & @CRLF)
Else
    For $i = 0 To UBound($arrSourceFiles) - 1 Step 1
        _FileReadToArray($arrSourceFiles[$i], $arrFileContent, $FRTA_NOCOUNT)
        If @error Then
            ConsoleWrite("Error while reading the file '" & $arrSourceFiles[$i] & "'. Error: " & @error & @CRLF)
        Else
            ConsoleWrite("The file '" & $arrSourceFiles[$i] & "' has been read correctly." & @CRLF)
            _FileWriteFromArray($hdlDestinationFile, $arrFileContent)
            If @error Then
                ConsoleWrite("Error while writing the data into '" & $strDestinationFile & "'. Error: " & @error & @CRLF)
            Else
                ConsoleWrite("The data read from the file '" & $arrSourceFiles[$i] & "' has been write correctly to '" & $strDestinationFile & "'." & @CRLF)
            EndIf
        EndIf
    Next

    FileClose($hdlDestinationFile)
EndIf

Here it is all the "sample" :)

Sample.zip

That might be fine to use for a straight copy... however, i'm manipulating the data.. "rearranging it"...Then I would need to insert it into another file... 

 

Share this post


Link to post
Share on other sites

No you just insert data into the array then afterwards you'd just write the entire array to file.

It would be simpler if you show an example of the two files and then what you expect it to look like afterwards

You don't have to use real data just need to understand what you're trying to achieve.

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

    • By Pickpocketz88
      First I would just like to say HELLO! to anyone reading. It has been a while since I've posted to the Forums but I'm always crawling around.
      Now to the matter at hand. I have been looking high and low for a simplistic answer my burned out brain can find but to no avail. I've only recently upped my AutoIt skill and only by a little bit such as ordering my script neatly with my own UDFs and using Global/Dim more often to make my GUI creation understandable and easy to keep things orderly. My current problem however is figuring out how to make my newest endeavor work which is my own "Debugger". I've made a GUI with an Edit Control to display what my Variables are holding and other information from a concurrently running Script. I have access to all of the scripts I'm attempting to connect but I'm dumbfounded on how I would separately read variable information from one running script into another. I'll provide my "Debugger" script that I want to read variables into and a "Meta Script" I'd want to pass info from.
      #Region Include Files #include <AutoItConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <ColorConstantS.au3> #EndRegion #Region AutoIt Options ;Opt("TrayAutoPause", 0) ;Opt("TrayMenuMode", 3) #EndRegion #Region Hotkeys HotKeySet("{ESC}", "ExitProgram") HotKeySet("{PGDN}", "PauseProgram") HotKeySet("!1", "Snippet_1") HotKeySet("!2", "Snippet_2") HotKeySet("!3", "Snippet_3") HotKeySet("!4", "Snippet_4") HotKeySet("!5", "Snippet_5") HotKeySet("!6", "Snippet_6") HotKeySet("!7", "Snippet_7") HotKeySet("!8", "Snippet_8") HotKeySet("!9", "Snippet_9") #EndRegion #Region Global Variables #Region Globals Global $gMain, $ctrlEdit ;, $gParent #EndRegion #Region $gMain Params Dim $gMainW = @DesktopWidth / 2 Dim $gMainH = @DesktopHeight / 2 Dim $gMainX = (@DesktopWidth / 2) - ($gMainW / 2) Dim $gMainY = (@DesktopHeight / 2) - ($gMainH / 2) Dim $gMainStyle = $WS_POPUP Dim $gMainStyleEx = -1 ;Dim $gMainParent = $gParent #EndRegion #Region $ctrlEdit Params Dim $ctrlEditW = Round($gMainW * 0.98) Dim $ctrlEditH = Round($gMainH * 0.98) Dim $ctrlEditX = ($gMainW - $ctrlEditW) / 2 Dim $ctrlEditY = ($gMainH - $ctrlEditH) / 2 Dim $ctrlEditStyle = -1 Dim $ctrlEditStyleEx = -1 #EndRegion #EndRegion #Region GUI Initialization ;$gParent = GUICreate("", -1, -1, -1, -1, -1, $WS_EX_TOOLWINDOW) $gMain = GUICreate("", $gMainW, $gMainH, $gMainX, $gMainY, $gMainStyle, $gMainStyleEx) GUISetBkColor($COLOR_BLACK, $gMain) $ctrlEdit = GUICtrlCreateEdit("MainW: " & $gMainW & @CRLF & "MainH: " & $gMainH & @CRLF & "EditW: " & $ctrlEditW & @CRLF & "EditH: " & $ctrlEditH, $ctrlEditX, $ctrlEditY, $ctrlEditW, $ctrlEditH, $ctrlEditStyle, $ctrlEditStyleEx) GUISetState(@SW_SHOW, $gMain) #EndRegion MainFunction() #Region Main Function (GUI) Func MainFunction() While 1 $msg = GUIGetMsg() If $msg = $GUI_EVENT_CLOSE Then Exit EndIf WEnd EndFunc #EndRegion #Region Functions Func Functions() EndFunc #EndRegion #Region Program 1 Func Snippet_1() EndFunc #EndRegion #Region Program 2 Func Snippet_2() EndFunc #EndRegion #Region Program 3 Func Snippet_3() EndFunc #EndRegion #Region Program 4 Func Snippet_4() EndFunc #EndRegion #Region Program 5 Func Snippet_5() EndFunc #EndRegion #Region Program 6 Func Snippet_6() EndFunc #EndRegion #Region Program 7 Func Snippet_7() EndFunc #EndRegion #Region Program 8 Func Snippet_8() EndFunc #EndRegion #Region Program 9 Func Snippet_9() EndFunc #EndRegion #Region Pause/Exit Functions Func PauseProgram() While 1 Sleep(1000) WEnd EndFunc Func ExitProgram() Exit EndFunc #EndRegion Pause/Exit Functions #Region Snippets #CS #CE #EndRegion #Region Other Information #CS #CE #EndRegion That's the Debugger script. Please forgive anything ignorant but point it out if you will, I'll take any pointers to get better! (I usually use a Select to get $GUI_EVENT_CLOSE but this is early on)
      Now if I made another script with a basic GUI similar to this and wanted to read say the GUI Width ($gMainW) into the Debugger Edit Control could I do it? If so, could I do it for every variable I have in a script? I read something about the Run function and adding the variables as an option parameter I believe which I think I could do with an array to keep it from being super long and ugly but would that be the only way to do this? Any information is going to be appreciated and thank you in advance for your time!
       
      Edit: Sadly it just dawned on me that I could make a UDF that will create a child window that will do this instead of having a separate script trying to invade another... I'll still be keeping an eye on this for any comments but I apologize if I wasted your time!
    • By mlazovjp
      I am going through the AutoIt Wiki and soaking up a lot of great information.  On the “Best Coding Practices” page at https://www.autoitscript.com/wiki/Best_coding_practices#Scopes_of_Variables there seems to be a minor detail missing.  In the “Variable Initialization” section at https://www.autoitscript.com/wiki/Best_coding_practices#Variable_Initialization , there are references to a data type $o, but in the table listing prefixes and their data types, there does not appear to be a prefix “o”.  I assume that this is for Object?
    • By ThePoro
      Hi everyone.
      I want to ask about this : 
      I want it runs from 1 to 100 and It opens 10 firefox profiles then access youtube. After I close a firefox window, the loop runs and wait for another window close until loop ends
      I have a loop like this.
      Func launch() Local $from = Int(GUICtrlRead($input1)) Local $to = Int(GUICtrlRead($input2)) If $to <> "" Then While $from <= $to Local $profile = $to _RunDos("start firefox.exe -p " & $profile & " -no-remote youtube.com") $to=$to+1 WEnd Else Local $profile = $to _RunDos("start firefox.exe -p " & $profile & " -no-remote youtube.com") EndIf EndFunc Is there any solution?
      Thank you!
    • By ThePoro
      I have this code 
      It gets variable input from GUI to run a loop and write to a text file then copy it to the clipboard
      But the thing is code runs normally with $px and $py <100 or both > 100 or with $px <10 and $py>100 but It's wrong with 10<$px <100 and $py >100
      Can anyone tell me why?
      Func clone() Local $px = GUICtrlRead($input1) Local $py = GUICtrlRead($input2) Local $temp = "C:\temp.csv" FileDelete($temp) While $px <= $py Local $name = "P"&$px FileWrite($temp, " & $name & ""& @CRLF) $px+= 1 WEnd $a=FileRead($temp) ClipPut($a) MsgBox($MB_ICONINFORMATION,"","Copied to Clipboard") EndFunc  
    • By Rhidlor
      First off, the project I'm working on revolves around AS/400 "Client Access software", it's foundation is directly influenced by the thread linked at the bottom of this post. Moving on, to explain the problem I'm facing; my project utilizes an infinite While loop and automatically performs semi-hard coded monotonous tasks to save users time and effort, the problem is, occasionally and unexpectedly "Display Messages" will popup and the core script will continue executing instead of dismissing said message and the script "breaks". As a countermeasure I've added some code to the While loop in an effort to intervene and dismiss these display messages before the core script has a chance to do anything... however it doesn't work. I hope I didn't do too bad of a job explaining that. 
      So I think my question is: How can I temporarily "pause" the core script when these messages spontaneously appear?
      The only other solution I thought of would be to check if a display message has appeared before executing every line of code but that obviously isn't very practical.
      Any and all help is greatly appreciated!
      Thanks
       
      ;Loop to keep script running and handle display messages While 1 dismiss_display_messages() Sleep(50) WEnd ;Function to dismiss display messages Func dismiss_display_messages() If $ps.SearchText("Display Messages") Then $ps.SendKeys("[enter]", 8, 1) ;This while loop waits until the display message has disappeared to resume the core script While $ps.SearchText("Display Messages") Sleep(100) WEnd EndIf EndFunc  
       
×
×
  • Create New...