Jump to content

loop script and use new varibles


TecGuy
 Share

Recommended Posts

I want to run a script then loop it and use the next set of varibles and keep going until i reach the first varible that looks like $Bfld(what ever number is next) = ""

I would like to ignore the first varible in every set and only use the second

I would also like to pull from an ini file.

$Bdir1 = ""

$Bfld1 = "Test1"

$Bdir2 = ""

$Bfld2 = "'Test File'"

$Bdir3 = "folder"

$Bfld3 = "test1"

$Bdir4 = "folder"

$Bfld4 = "test2"

$Bdir5 = ""

$Bfld5 = ""

i cannot figure out how to add the next number after $Bfld(Count) =

Counting has been extremely confusing

If someone could point me in the right direction i am willing to do the work

I also need to use the same with my log files to add a number at the end of the file name if one already exist

Any help would be greatly appreciated.

Thanks,

TecGuy

Edited by TecGuy
Link to comment
Share on other sites

I want to run a script then loop it and use the next set of varibles and keep going until i reach the first varible that looks like $Bfld(what ever number is next) = ""

I would like to ignore the first varible in every set and only use the second

I would also like to pull from an ini file.

$Bdir1 = ""

$Bfld1 = "Test1"

$Bdir2 = ""

$Bfld2 = "'Test File'"

$Bdir3 = "folder"

$Bfld3 = "test1"

$Bdir4 = "folder"

$Bfld4 = "test2"

$Bdir5 = ""

$Bfld5 = ""

i cannot figure out how to add the next number after $Bfld(Count) =

Counting has been extremely confusing

If someone could point me in the right direction i am willing to do the work

I also need to use the same with my log files to add a number at the end of the file name if one already exist

Any help would be greatly appreciated.

Thanks,

TecGuy

Just learn to use arrays. There is some learning curve, but it will still be much easier than this.

There is a good tutorial on arrays in the AutoIt Wiki.

If you insist on kludging together something without arrays, you would have to use Assign() and Eval() to work with your variables. But arrays are the real answer.

:D

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Thanks for your reply. I think i am making it to complicated. I am going to try a different way.

If I am wrong please accept my apology.

i just need to do a file copy

I am going to put in a text file the following

Sorce1

Destination1

Sorce2

Destination2

Sorce3

Destination3

Sorce 4

Destination4

Now i would like to run the following

$Sorce = Sorce1 from file
$Destination = Destination1 from file

FileCopy("C:\"$Sorce"\*.*", "C:\"$Destination"\", 9)

I would like it to Loop my entire script and replace the source and destination with the next one until the file ends.

The reason being is that i need to edit this file frequently

Link to comment
Share on other sites

Thanks for your reply. I think i am making it to complicated. I am going to try a different way.

If I am wrong please accept my apology.

i just need to do a file copy

I am going to put in a text file the following

Sorce1

Destination1

Sorce2

Destination2

Sorce3

Destination3

Sorce 4

Destination4

Now i would like to run the following

$Sorce = Sorce1 from file
$Destination = Destination1 from file

FileCopy("C:\"$Sorce"\*.*", "C:\"$Destination"\", 9)

I would like it to Loop my entire script and replace the source and destination with the next one until the file ends.

The reason being is that i need to edit this file frequently

This makes a good demo for use of arrays:

Imagine your INI file formatted this way:

[Copy]
Source = C:\Source\Src1.txt
Destination = D:\Dest\Dest1.txt

Source = C:\Source\Src2.txt
Destination = D:\Dest\Dest2.txt

; etc., etc., ...

Source = C:\Source\Src999.txt
Destination = D:\Dest\Dest999.txt

You would read the "Copy" section into an array with IniReadSection(), and then loop through the array to process the entries. Testing for correct pairs of "Source" and "Destination" keys makes sure things stay in sync:

#include <Array.au3> ; Only for _ArrayDisplay()

Global $sIniFile = "C:\Source\Copy.ini"

Global $avCopySec = IniReadSection($sIniFile, "Copy")
If @error  = 0 Then
    _ArrayDisplay($avCopySec, "Demo: $avCopySec")
Else
    MsgBox(16, "Error", "Could not read Copy section of: " & $sIniFile)
    Exit
EndIf

For $n = 1 To $avCopySec[0][0] - 1 Step 2
    ; Test for valid key names and data
    If ($avCopySec[$n][0] = "Source") And FileExists($avCopySec[$n][1]) And _
            ($avCopySec[$n + 1][0] = "Destination") And ($avCopySec[$n + 1][1] <> "") Then
        ; Copy source to destination
        FileCopy($avCopySec[$n][1], $avCopySec[$n + 1][1], 1 + 8) ; Overwrite, create directory
        If @error = 0 Then
            MsgBox(16, "Error", "Error copying file!" & @CRLF & _
                    @TAB & "From:  " & $avCopySec[$n][1] & @CRLF & _
                    @TAB & "To:  " & $avCopySec[$n + 1][1])
        EndIf
    Else
        ; Invalid data
        MsgBox(16, "Error", "Invalid data in Copy section of: " & $sIniFile)
        Exit
    EndIf
Next

Cheers!

:D

Edit: Added _ArrayDisplay() to help explain usage of $avCopySec.

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

I just tested it but i need it to do the first set then start a the beginning of the script and then

Process the next set and so on until there are no more. Some how i need to tell the script which set it

left off with so the next time the script runs it will grab the next set.

I want to

Copy
Source = C:\Source\Src1.txt
Destination = D:\Dest\Dest1.txt

Go back to the beginning of the script
Copy
Source = C:\Source\Src2.txt
Destination = D:\Dest\Dest2.txt

Go back to the beginning of the script
Copy
; etc., etc., ...

Go back to the beginning of the script
Copy
Source = C:\Source\Src999.txt
Destination = D:\Dest\Dest999.txt

then it will go on with the rest of the script

Edited by TecGuy
Link to comment
Share on other sites

Try adding this loop before the "FileCopy" line in PsaltyDS's script:

If FileExists($AvCopySec[$n][1]) And FileExists($AvCopySec[$n + 1][1]) Then
    Local $SourceFileSize = FileGetSize($AvCopySec[$n][1])
    Local $DestFileSize = FileGetSize($AvCopySec[$n + 1][1])
    Local $SourceFileTime = FileGetTime($AvCopySec[$n][1], 0, 1)
    Local $DestFileTime = FileGetTime($AvCopySec[$n + 1][1], 0, 1)
    If $SourceFileSize = $DestFileSize And $SourceFileTime = $DestFileTime Then ContinueLoop
EndIf

It says that if the source and destination files both exists, check their respective sizes are equal and their modified time stamps are equal. If they are, skip the FileCopy and resume the loop.

Alternatively, you could do this:

Before the For Loop, add this:

Local $n = RegRead('HKCU\Software\AutoIt v3', 'FileCopy Operation')
If $n < 1 Then $n = 1

And change the FileCopy line to:

If FileCopy($AvCopySec[$n][1], $AvCopySec[$n + 1][1], 1 + 8) Then
    ;FileCopy successful, then write registry value to start $n from next pair
    RegWrite('HKCU\Software\AutoIt v3', 'FileCopy Operation', 'REG_SZ', $n + 2)
EndIf

It will only write the registry value after a successful FileCopy operation. If the TXT file you source from is only changed by you, then you will be OK..but I suggest PsaltyDS's solution for future provisioning's sake.

Link to comment
Share on other sites

Thank you PsaltyDS and Varian

That solved my problems.

I tried to modify PsaltyDS script but i am getting the following "Array variable has incorrect number of subscripts"

[Name]
File = Src1.txt
File = Src2.txt
File = Src3.txt
File = Src4.txt
File = Src5.txt
File = Src6.txt
etc., etc., ...

I want to use a line ("Then name of the file is" & $Name) and pull the first File=

The next time the section is encountered it will pull the next File = and so on

I am executing the same program over and over but i have to modify the file location each time until there are no more.

I assume this could be a new section in my ini file

This might be a redundent question but I just seem not to be able to grasp how this works. I apologize ahead of time.

Edited by TecGuy
Link to comment
Share on other sites

Thank you PsaltyDS and Varian

That solved my problems.

I tried to modify PsaltyDS script but i am getting the following "Array variable has incorrect number of subscripts"

[Name]
File = Src1.txt
File = Src2.txt
File = Src3.txt
File = Src4.txt
File = Src5.txt
File = Src6.txt
etc., etc., ...

I want to use a line ("Then name of the file is" & $Name) and pull the first File=

The next time the section is encountered it will pull the next File = and so on

I am executing the same program over and over but i have to modify the file location each time until there are no more.

I assume this could be a new section in my ini file

This might be a redundent question but I just seem not to be able to grasp how this works. I apologize ahead of time.

I couldn't make much sense out of that description of what you want. Did you just want to move a single file per runtime and track which was done last? That could be be simply an extra entry in the ini file that is updated on each successful copy:
[LastDone]
Index=5

On the next run, it would start from the 6th entry in the array from IniReadSection(). If Index=n is the the last one, then it could start over from 1.

But I'm having to guess a bit on what you mean.

:D

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

I just found part of what I need. It seems that you probabily already answered my question but I'm not getting it.

my INI file looks like this

[file]
file=bob

$file = InIRead("C:\file.ini","file","file","")

Now I understand that is the way to get the return for a single line from an ini file.

My question is if i had 5 lines like the example below

[file]
file=bob
file=bill
file=mary
file=joe
file=kate

[LastDone]
Index=2

How do i use this command

$file = InIRead("C:\file.ini","file","file","")

to read the first line and put it in the code below as a varible

EnvSet("file name", "c:\temp\"&$file)
run Program.exe

after the program.exe runs then the script will loop back to the beginning and rerun the above command but this time

read the next line out of my ini file.

i want it to continue until each line is read then the script will exit

The next time i execute the script it will start at line 1 and repeat the process.

I hope this is a better explanation of what I need.

Edited by TecGuy
Link to comment
Share on other sites

I just found part of what I need. It seems that you probabily already answered my question but I'm not getting it.

my INI file looks like this

[file]
file=bob

$file = InIRead("C:\file.ini","file","file","")

Now I understand that is the way to get the return for a single line from an ini file.

My question is if i had 5 lines like the example below

[file]
file=bob
file=bill
file=mary
file=joe
file=kate

[LastDone]
Index=2

How do i use this command

$file = InIRead("C:\file.ini","file","file","")

to read the first line and put it in the code below as a varible

EnvSet("file name", "c:\temp\"&$file)
run Program.exe

after the program.exe runs then the script will loop back to the beginning and rerun the above command but this time

read the next line out of my ini file.

i want it to continue until each line is read then the script will exit

The next time i execute the script it will start at line 1 and repeat the process.

I hope this is a better explanation of what I need.

You are still avoiding the concept of arrays, which are the easiest way to do this. Give up and learn arrays. Your scripts will be much improved:
; Never create a loop with no way out
HotKeySet("{ESC}", "_Quit") ; Hit ESC to exit

Global $sFile
Global $sINI = "C:\file.ini" ; INI file
Global $sProgram = "C:\Program Files\MyProg\Program.exe" ; Program to run
Global $avFileSection = IniReadSection($sINI, "file") ; Read section into an array

While 1 ; Repeat forever (or until ESC)
    For $n = 1 To $avFileSection[0][0] ; Repeat for each line in the section
        If $avFileSection[$n][0] = "file" Then ; Verify valid key name = 'file'
            $sFile = $avFileSection[$n][1] ; Copy value to $sFile variable
            EnvSet("file name", "c:\temp\" & $file) ; Set environment variable
            RunWait($sProgram) ; Run program, wait for it to exit before continuing
        EndIf
    Next
WEnd

Func _Quit()
    Exit
EndFunc   ;==>_Quit

:D

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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...