Sign in to follow this  
Followers 0
pikelet650

Help with an automatic Sync Script

5 posts in this topic

Hi there,

I am attempting to script a small program that will automatically Synchronize a drive (using SyncToy). I have managed to get it to do that, but i would also like to add an item in the tray menu that will Sync immediately (eg. Sync Now)

I have got both parts working in separate scripts, but cannot find a way to combine them. Both scripts are below:

NB: For the purposes of the script below, i have made it open notepad (commented out the synctoy specific sections) instead of SyncToy as the SyncToy profile is specific to my computer

Tray Menu:

#NoTrayIcon

Opt("TrayMenuMode",1)   ; Default tray menu items (Script Paused/Exit) will not be shown.

$syncnow  = TrayCreateItem("Sync Now")
TrayCreateItem("")
$aboutitem  = TrayCreateItem("About")
$exititem   = TrayCreateItem("Exit")

TraySetState()
TraySetToolTip("AutoSync")
;TraySetIcon("C:\Program Files\SyncToy 2.1\SyncToyCmd.exe", 1)
While 1
    $msg = TrayGetMsg()
    Select
        Case $msg = 0
            ContinueLoop
        Case $msg = $syncnow
            SyncNow()
        Case $msg = $aboutitem
            Msgbox(64, "about:", "Automatically Syncronises H Drive every 10 minutes")
        Case $msg = $exititem
           ExitLoop
        EndSelect
WEnd
    

Func SyncNow()
;Run('"C:\Program Files\SyncToy 2.1\SyncToyCmd.exe" -R "H Drive Sync"')
Run("notepad.exe")
EndFunc

Automatic Sync:

;#NoTrayIcon
#include <get_dns.au3>; Get the current Local DNS Suffix
    
    While $dnsfullname = $dnsfullname; Always TRUE for the purposes of testing
        Sync()
    WEnd

Func Sync()
    ;Run('"C:\Program Files\SyncToy 2.1\SyncToyCmd.exe" -R "H Drive Sync"')
    Run("notepad.exe")
    Sleep(300000)
EndFunc

Is there a way to combine these scripts so i can have the autosync feature as well as being able to right-click the tray icon and "Sync Now"?

Thanks in advance

pikelet650

get_dns.au3

AutoSync.au3

TrayMenuSync.au3

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I can't see what the problem is?

If you remove the "notepad" line and you uncomment your "Run" line from your first example it will work just fine.

Edited by enaiman

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Share this post


Link to post
Share on other sites

I can't see what the problem is?

If you remove the "notepad" line and you uncomment your "Run" line from your first example it will work just fine.

What i want to do is combine the two scripts so that i can have the autosync (synchronizing automatically) as well as the Tray Manu Item (Sync Now)

when i attempted to combine them i could only get one to work. if i put the autosync bit above the tray menu bit then it would wait 5 minutes before processing the tray menu bit. however if i put the autosync bit after the tray menu bit then the autosync wouldnt process.

I tried putting the autosync code:

Sync()

in this section:

Case $msg = 0
Sync()

and it creates the tray menu item, and also processes the autosync. but when i click the Sync Now button nothing happens.

Hope i havent confused you too much and hope this info helps

Regards,

pikelet650

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

What you want to do is combine your while loops into one loop. You will also want to replace your sleep with a timer as sleeping pauses the script and makes using your "sync now" button impossible to use.

Basically you want something like this.

$Interval = 10 * (60 * 1000) ; 10 Minutes
$Timer = TimerInit()
While 1
    $msg = TrayGetMsg()
    Select
    Case $msg = $syncnow Or TimerDiff($Timer) > $Interval
    Sync()
    Case $msg = $aboutitem
    Msgbox(64, "about:", "Automatically Syncronises H Drive every 10 minutes")
    Case $msg = $exititem
    ExitLoop
    Case Else
    ; Do nothing
    EndSelect
WEnd

Func Sync()
    Run('"C:\Program Files\SyncToy 2.1\SyncToyCmd.exe" -R "H Drive Sync"')
EndFunc

Edit: Made a few minor edits

Edited by Deltaforce229

[size="1"]Please stop confusing "how to" with "how do"[/size]

Share this post


Link to post
Share on other sites

What you want to do is combine your while loops into one loop. You will also want to replace your sleep with a timer as sleeping pauses the script and makes using your "sync now" button impossible to use.

Basically you want something like this.

$Interval = 10 * (60 * 1000) ; 10 Minutes
$Timer = TimerInit()
While 1
    $msg = TrayGetMsg()
    Select
    Case $msg = $syncnow Or TimerDiff($Timer) > $Interval
    Sync()
    Case $msg = $aboutitem
    Msgbox(64, "about:", "Automatically Syncronises H Drive every 10 minutes")
    Case $msg = $exititem
    ExitLoop
    Case Else
    ; Do nothing
    EndSelect
WEnd

Func Sync()
    Run('"C:\Program Files\SyncToy 2.1\SyncToyCmd.exe" -R "H Drive Sync"')
EndFunc

Edit: Made a few minor edits

Thanks Deltaforce229,

I had a feeling the sleep() command was the trouble. Thanks for showing me the Timer function.

Its now fully working, i just had to tweak a little bit of the code.

Func Sync()
    Run('"C:\Program Files\SyncToy 2.1\SyncToyCmd.exe" -R "H Drive Sync"')
    $Timer = TimerInit()
EndFunc

What it was doing was waiting the 10 minutes, and then it would loop the run command, and wouldnt stop (as the timer was still over the 10 minute mark) and crash the system (windows can't really handle opening heaps of notepad windows at once constantly) :huggles:

I just re-set the $timer variable after the run command (so that it starts counting again)

Again, thanks for you help :D

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
Sign in to follow this  
Followers 0