legomyeggos

New to AutoIt, trying to build a rename function

15 posts in this topic

I feel like this should be an easy solution but I cannot seem to come up with it. I am fairly new in the world of programming and just started learning java so I am struggling with syntax on this. I have a project coming up and am on a deadline to have it done and autoit seemed like the solution. Was hoping for some guidance.

 

I receive reports daily at work but they are typically all named report. I just save them in a directory Reports on the C drive.

 

I have them set to receive when I am not in the office to save them but it ends up being fruitless due to the file names typically being the same so it doesnt copy over well.

Seems like this should be simple, but my google has failed me.

 

 

 

 

Share this post


Link to post
Share on other sites



legomyeggos,

That sounds like a fairly easy task. Use FileExists at suitable intervals to check if the file has arrived and then use FileMove to move it to the "Reports" folder under a new name. Something like this should work:

; Set a HotKey to escape from the infinite loop
HotKeySet("{ESC}", "_Exit")

; Get current hour
$iHour = @HOUR

; Start infinite loop
While 1
    ; Once an hour (look for the change)
    If @HOUR <> $iHour Then
        ; Reset timer
        $iHour = @HOUR
        ; Look for the "Report" file - use your required file path
        If FileExists(@ScriptDir & "\Report.txt") Then
            ; Move the file to a new folder - again use your required files paths
            FileMove(@ScriptDir & "\Report.txt", @ScriptDir & "\Bin\Report_" & @YEAR & @MON & @MDAY & ".txt")
        EndIf
    EndIf
    ; Give the CPU a breather
    Sleep(10)
WEnd

Func _Exit()
    Exit
EndFunc

Please ask if you have any questions.

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

Hey Melba. I'll give it a shot first thing in the morning. For some reason I never saw the reply until just now! I greatly appreciate the input. Will follow up tomorrow : )

Share this post


Link to post
Share on other sites

Alright, I got a bit antsy and decided to give it a shot. I made a few changes to kind of fit what I was looking for a bit more custom.

I want it to run every 15-20 minutes, and I don't want a report.txt file to exist, if it finds one, I want it renamed to report1.txt then the next one report2.txt, etc.

 

How does this look?

 

 

HotKeySet("{ESC}", "_Exit")

; Get current hour
$iMinute = 5
$rTimer = Timerinit()

; Start infinite loop
While 1
    ; Once an hour (look for the change)
    If If TimerDiff($hTimer) > ($iMinutes * 60000) Then
        ; Look for the "Report" file - use your required file path
        If FileExists(@ScriptDir & "\Report.txt") Then
            ; Move the file to a new folder - again use your required files paths
            FileMove(@ScriptDir & "\Report.txt", @ScriptDir & "\Bin\Report_" & i++ ".txt")
        EndIf
    EndIf
    ; Give the CPU a breather
    Sleep(10)
WEnd

Func _Exit()
    Exit
EndFunc

Share this post


Link to post
Share on other sites

I apologise greatly for multiple posts.. Can't find an edit button..

 

I have ran this script with modifications from above to better define the variables but something is missing to make it work. I'm assuming its because I need to set file paths maybe?

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Hi lego.....

It helps us all greatly, if you use the <> button when providing code.

The following line will not work and will cause an error. i++ is meaningless and you are missing an ampersand anyway.

FileMove(@ScriptDir & "\Report.txt", @ScriptDir & "\Bin\Report_" & i++ ".txt")

You would be better off to use a unique instance anyway, I imagine. Something like -

$unique = @YEAR & "-" & @YDAY & "-" & @HOUR & "-" & @MIN & "-" & @SEC
 FileMove(@ScriptDir & "\Report.txt", @ScriptDir & "\Bin\Report_" & $unique & ".txt")

You could of course, just used _Now() etc and replacements instead, for date & time macros.

Welcome to the forum. :)

Edited by TheSaint

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites

legomyeggos,

This is how I would code it to meet your requirements - the trick is the use of Mod to action the internal code every 5 minutes:

HotKeySet("{ESC}", "_Exit")

; Set required interval in minutes
$iMinute = 5
; Set current minute
$iCurrMin = @SEC
; Set counter
$iCount = 1

; Start infinite loop
While 1
    ; At the defined interval
    If @SEC <> $iCurrMin And Mod(@SEC, 5) = 0 Then
        
        ; just for display
        ConsoleWrite(@SEC & @CRLF)

        ; Reset current minute
        $iCurrMin = @SEC
        ; Look for the "Report" file - use your required file path
        If FileExists(@ScriptDir & "\Report.txt") Then
            ; Move the file to a new folder - again use your required files paths
            FileMove(@ScriptDir & "\Report.txt", @ScriptDir & "\Bin\Report_" & $iCount & ".txt")
            ; Increase the count
            $iCount += 1
        EndIf
    EndIf
    ; Give the CPU a breather
    Sleep(10)
WEnd

Func _Exit()
    Exit
EndFunc   ;==>_Exit

I have set the script to fire every 5 seconds so you can see it working - you will need to replace all instances of @SEC with @MIN to get it to look every 5 minutes - and I have added a ConsoleWrite line so you can check that it is indeed working, which can be removed once you are convinced that it does.

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

@Melba: shouldn't be $iCount saved at AuoItExit and reassigned by next start?

Share this post


Link to post
Share on other sites

AutoBert,

Good point - perhaps for later along with using StringFormat to pad the count with leading zeroes so that they display nicely in Explorer. At the moment, I am just trying to help the (by his own admission) inexperienced OP get a basic script working.

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

#10 ·  Posted (edited)

Melba23,

 

Thanks again for your thorough and helpful response. I am able to better read and understand it thanks to just seeing your changes in action. I am curious if I have ran this script incorrectly on my system to get it to function correctly.

 

I have this script in the directory with my Report.txt and am running it from there, but I am not seeing a Bin directory created or the file being renamed. Terribly sorry to be so incompetent with this. For reference this is the au3 script I am running in the folder with my Report.txt

HotKeySet("{ESC}", "_Exit")


; Set required interval in minutes
$iMinute = 5
; Set current minute
$iCurrMin = @MIN
; Set counter
$iCount = 1

; Start infinite loop
While 1
    ; At the defined interval
    If @MIN <> $iCurrMin And Mod(@MIN, 5) = 0 Then

        ; just for display
        ConsoleWrite(@MIN & @CRLF)

        ; Reset current minute
        $iCurrMin = @MIN
        ; Look for the "Report" file - use your required file path
        If FileExists(@ScriptDir & "\Report.txt") Then
            ; Move the file to a new folder - again use your required files paths
            FileMove(@ScriptDir & "\Report.txt", @ScriptDir & "\Bin\Report_" & $iCount & ".txt")
            ; Increase the count
            $iCount += 1
        EndIf
    EndIf
    ; Give the CPU a breather
    Sleep(10)
WEnd

Func _Exit()
    Exit
EndFunc   ;==>_Exit

 

Edited by Melba23
Added code tags

Share this post


Link to post
Share on other sites

legomyeggos,

Firstly, when you post code please use Code tags - see here how to do it.  Then you get a scrolling box and syntax colouring as you can see above now I have added the tags.

Quote

but I am not seeing a Bin directory created or the file being renamed

If the folder does not exist then you need to tell AutoIt to create it for you when it moves the first file:

FileMove(@ScriptDir & "\Report.txt", @ScriptDir & "\Bin\Report_" & $iCount & ".txt", $FC_CREATEPATH)

or you could use DirCreate directly at the beginning of the script. 

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

Alright.. So this is exactly what I am doing and my setup.. Maybe you can point out the flaw in my execution.

reqNwNR.png 

That is the script and report.txt in a test directory by itself. I am opening RunScript(x86) and selecting the au3 script in the folder. I am getting an error that says line 23, variable used without being declared.

pyKWse7.png

 

This is the code I have in the .au3 script for reference.

 

HotKeySet("{ESC}", "_Exit")

; Set required interval in minutes
$iMinute = 5
; Set current minute
$iCurrMin = @MIN
; Set counter
$iCount = 1

; Start infinite loop
While 1
    ; At the defined interval
    If @MIN <> $iCurrMin And Mod(@MIN, 5) = 0 Then

        ; just for display
        ConsoleWrite(@MIN & @CRLF)

        ; Reset current minute
        $iCurrMin = @MIN
        ; Look for the "Report" file - use your required file path
        If FileExists(@ScriptDir & "\Report.txt") Then
            ; Move the file to a new folder - again use your required files paths
            FileMove(@ScriptDir & "\Report.txt", @ScriptDir & "\Bin\Report_" & $iCount & ".txt", $FC_CREATEPATH)
            ; Increase the count
            $iCount += 1
        EndIf
    EndIf
    ; Give the CPU a breather
    Sleep(10)
WEnd

Func _Exit()
    Exit
EndFunc   ;==>_Exit

 

Share this post


Link to post
Share on other sites

legomyeggos,

My apologies, you need to add #include <FileConstants.au3> at the top of the script so that AutoIt knows what value to assign to $FC_CREATEPATH.

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 can not thank you enough for all of your assistance.

 

I have been practicing java, but if I wanted to learn more about coding with Autoit, what resources would you recommend I start looking at for a better understanding? I hope next time I need to use it I can be much better versed and much less of a pain in the butt. Thanks so much for your service in helping me get this running.

Share this post


Link to post
Share on other sites

legomyeggos,

Glad I could help.

As to learning more about AutoIt, reading the Help file (at least the first few sections - Using AutoIt, Tutorials & Language Reference) will help you enormously.  You should also look at this excellent tutorial - you will find other tutorials in the Wiki (the link is at the top of the page). Other than that the forum itself is a pretty good resource - and as you have discovered, we do not bite when asked even simple questions as long as you make some effort yourself.

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

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