allen2111

Incremental File Name

12 posts in this topic

#1 ·  Posted (edited)

Hi, I am very new to AutoIt. I want to make a script that performs some actions and then loops 100 times. Each time I want it to increment a file name in two locations. So "file1.txt" would become "file2.txt"..."file100.txt"

I was able to make the base scripts but having trouble with the incrementing.

I also found this http://www.autoitscript.com/forum/topic/77153-loop-through-files-in-directory/

but adapting it to my scripts has been futile.

any help is appreciated.

#region --- Au3Recorder generated code Start (v3.3.9.5 KeyboardLayout=A0000409)  ---

#region --- Internal functions Au3Recorder Start ---
Func _Au3RecordSetup()
Opt('WinWaitDelay',100)
Opt('WinDetectHiddenText',1)
Opt('MouseCoordMode',0)
Local $aResult = DllCall('User32.dll', 'int', 'GetKeyboardLayoutNameW', 'wstr', '')
If $aResult[1] <> 'A0000409' Then
  MsgBox(64, 'Warning', 'Recording has been done under a different Keyboard layout' & @CRLF & '(A0000409->' & $aResult[1] & ')')
EndIf



EndFunc



Func _WinWaitActivate($title,$text,$timeout=0)
    WinWait($title,$text,$timeout)
    If Not WinActive($title,$text) Then WinActivate($title,$text)
    WinWaitActive($title,$text,$timeout)
EndFunc



_AU3RecordSetup()
#endregion --- Internal functions Au3Recorder End ---

_WinWaitActivate("LTspice IV - 20150505 KED Model with Flexible Current Source.asc","20150505 KED Model w")
MouseClick("right",631,622,1)
_WinWaitActivate("Independent Current Source - I1","PULSE(I1 I2 Tdelay T")
MouseClick("left",291,190,1)
_WinWaitActivate("Piecewise Linear File","Files of &type:")
MouseMove(147,527)
MouseDown("left")
MouseMove(65,527)
MouseUp("left")
Send("file1.txt")
MouseClick("left",609,531,1)
_WinWaitActivate("Independent Current Source - I1","PULSE(I1 I2 Tdelay T")
MouseClick("left",540,442,1)
_WinWaitActivate("LTspice IV - 20150505 KED Model with Flexible Current Source.asc","20150505 TED Model w")
MouseClick("left",143,65,1)
MouseClick("left",126,123,1)
_WinWaitActivate("LTspice IV - 20150505 KED Model with Flexible Current Source","20150505 TED Model w")
MouseClick("left",30,39,1)
MouseClick("left",109,254,1)
_WinWaitActivate("Select Traces to Export","Select Waveforms to ")
MouseClick("left",314,72,1)
_WinWaitActivate("Save As","Save as &type:")
MouseClick("left",310,47,1)
MouseClick("left",54,122,2)
MouseClick("left",57,100,2)
MouseMove(341,525)
MouseDown("left")
MouseMove(72,523)
MouseUp("left")
Send("file1.txt")
MouseClick("left",610,522,1)
_WinWaitActivate("Select Traces to Export","Select Waveforms to ")
MouseClick("left",315,316,1)
#endregion --- Au3Recorder generated code End ---

 

Edited by allen2111

Share this post


Link to post
Share on other sites



allen2111,

Welcome to the AutoIt forums.

You need to put the code into a loop - look at For...Next...Step in the Help file. Then for each pass you can use the loop counter to increment the filename.  Here is a short example to show the principle:

#include <MsgBoxConstants.au3>

; Just 10 passes for this example
For $i = 1 To 10

    ; Your code runs here to get the data

    ; Create the filename - note the use of StringFormat to force leading 0s so the files sort correctly
    $sFileName = "File" & StringFormat("%03i", $i) & ".txt"

    ; Just to show what was produced
    MsgBox($MB_SYSTEMMODAL, "Filename", $sFileName)
    
    ; And here you would use FileWrite to save the file

Next

Please ask if anything is unclear.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

M23,

I replaced the "; Your code runs here to get the data". I think I did something wrong because I am getting errors.

I attached a screenshot so you could see the error.

 

screenshot.jpg

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Jos,

Thank you. I do not know enough about autoit to remedy the code. I attached the full code below. Can you suggest how I can change it to make it work?

#include <MsgBoxConstants.au3>

; Just 10 passes for this example
For $i = 1 To 10

#region --- Au3Recorder generated code Start (v3.3.9.5 KeyboardLayout=A0000409)  ---

#region --- Internal functions Au3Recorder Start ---
Func _Au3RecordSetup()
Opt('WinWaitDelay',100)
Opt('WinDetectHiddenText',1)
Opt('MouseCoordMode',0)
Local $aResult = DllCall('User32.dll', 'int', 'GetKeyboardLayoutNameW', 'wstr', '')
If $aResult[1] <> 'A0000409' Then
  MsgBox(64, 'Warning', 'Recording has been done under a different Keyboard layout' & @CRLF & '(A0000409->' & $aResult[1] & ')')
EndIf

EndFunc

Func _WinWaitActivate($title,$text,$timeout=0)
    WinWait($title,$text,$timeout)
    If Not WinActive($title,$text) Then WinActivate($title,$text)
    WinWaitActive($title,$text,$timeout)
EndFunc

_AU3RecordSetup()
#endregion --- Internal functions Au3Recorder End ---

_WinWaitActivate("LTspice IV - 20150505 KED Model with Flexible Current Source","")
MouseClick("right",740,623,1)
_WinWaitActivate("Independent Current Source - I1","")
MouseClick("left",287,185,1)
_WinWaitActivate("Piecewise Linear File","")
MouseMove(193,515)
MouseDown("left")
MouseMove(47,510)
MouseUp("left")
Send("file1.txt")
MouseClick("left",562,518,1)
_WinWaitActivate("Independent Current Source - I1","")
MouseClick("left",537,433,1)
_WinWaitActivate("LTspice IV - 20150505 KED Model with Flexible Current Source.asc","")
MouseClick("left",148,64,1)
MouseClick("left",91,123,1)
_WinWaitActivate("LTspice IV - 20150505 KED Model with Flexible Current Source","")
MouseClick("left",23,36,1)
MouseClick("left",169,261,1)
_WinWaitActivate("Select Traces to Export","")
MouseMove(289,72)
MouseDown("left")
MouseMove(289,73)
MouseUp("left")
_WinWaitActivate("Save As","")
MouseClick("left",323,49,1)
MouseClick("left",83,122,2)
MouseClick("left",93,101,2)
MouseMove(346,516)
MouseDown("left")
MouseMove(50,497)
MouseUp("left")
Send("file1.txt")
MouseClick("left",576,518,1)
_WinWaitActivate("Select Traces to Export","")
MouseClick("left",333,312,1)
#endregion --- Au3Recorder generated code End ---

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.12.0
 Author:         myName

 Script Function:
    Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here
   ; Create the filename - note the use of StringFormat to force leading 0s so the files sort correctly
    $sFileName = "File" & StringFormat("%03i", $i) & ".txt"

    ; Just to show what was produced
    MsgBox($MB_SYSTEMMODAL, "Filename", $sFileName)

    ; And here you would use FileWrite to save the file

Next

 

Edited by allen2111

Share this post


Link to post
Share on other sites

Then you will have to study a bit. .. ;)
Think it is important you understand the code when you want to use it.

Start with moving your Func-EndFunc protions of code to the end of your script.

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Jos,

Thanks. Good attitude. "Teach me to fish" rather than throw me a fish.

I struggled with moving the functions to the bottom, had to leave behind the region stuff. The code now works as it did prior to moving the functions to the bottom.

I am now working on getting the loop working.

I moved this just above the functions

#include <MsgBoxConstants.au3>

; Just 10 passes for this example
For $i = 1 To 10

and moved this just below the functions. I also tried moving the above code to the very top of the file and leaving the below code at the bottom.

; Create the filename - note the use of StringFormat to force leading 0s so the files sort correctly
    $sFileName = "File" & StringFormat("%03i", $i) & ".txt"

    ; Just to show what was produced
    MsgBox($MB_SYSTEMMODAL, "Filename", $sFileName)
    
    ; And here you would use FileWrite to save the file

Next

no joy. Same "next" errors as before plus a syntax error and "statement cannot be just an expression"

 

 

Share this post


Link to post
Share on other sites

mmm..  though I was pretty explicit about not putting the Func-EndFunc inside of the For-Next loop :)

Just move them both below the Next statement!

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

I just got it going but looks like the loop code needs changes. Let me try some stuff and get back to you. Thanks.

Share this post


Link to post
Share on other sites

The loop and the counter are working!! :D

I have two of these statements "Send("file1.txt")"

This is where Autoit inputs a file name. (you probably knew that).

Do you know the syntax for making the "file1.txt" change to "file2.txt"... with the counter?

Share this post


Link to post
Share on other sites

spoke to soon again. Got it :D

"File" & StringFormat("%i", $i) & ".txt"

Share this post


Link to post
Share on other sites

:) ... Just take your time to understand each step and you'll see you get there where you want to be.

Next you really should check out au3info and see if you can use Controlxxxx commands in stead of the Send() commands build by au3recorder.

Jos


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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