Link to home
Start Free TrialLog in
Avatar of MrWizzard
MrWizzard

asked on

How to create a script to run synctoy

Hello!
Currently I am using task scheduler to run synctoycmd.exe every morning at 2am to backup my stuff. It works just fine, except doing it this way does not show the "Results" page after the sync is complete. I would like to get to my computer in the morning and see the results waiting for me to review.
I think i need to write a script for this, and then point the task scheduler to the script, but dont know how to write scripts!

Thanks
Avatar of Tursilion
Tursilion
Flag of United States of America image

Not familiar with SyncToy, but it seems people online wanted the opposite - they wanted to hide it! :)
http://www.viper007bond.com/2009/10/04/hiding-microsoft-synctoycmd-exe-for-task-scheduler-purposes/

That said, if you create a file named "runsync.cmd" somewhere convenient, and put your command in there followed by a pause, that probably will do the trick. Like so (but put your actual full path synctoy command in the first line):

synctoycmd.exe
pause
Make scheduler run this batch file instead. That simple.. the command prompt should be sitting there with a "press any key to continue" in the morning. Just make sure that Task Scheduler is not configured to kill it after a certain amount of time (on the settings tab, there is a "Stop the task if it runs for <xx> hours" -- just make sure that's more than long enough).

Avatar of MrWizzard
MrWizzard

ASKER

Thanks for the info Tursilion!
I think your assuming that I know how to write a batch file. Could you elaborate on that?

Thanks!
Heh... no worries. A batch file (or cmd file) is just a normal text file, so you could create it with Notepad. Just open up Notepad (if you can't locate it, it's usually enough to go Start->Run and type "notepad".)

Then, you put one command on each line.

So in the first line, put the synctoycmd.exe -- this should look exactly like the working line that you have in Task Scheduler as 'Run'.

On the second line, just put "pause" all by itself, like I have in the example above.

Next, File->Save As, and this is the important part, the file extension must be ".CMD" (Notepad will offer ".TXT" by default). You can save this in the same folder you have Synctoy.exe in now, that is probably easiest. I would suggest a name like "SyncBatch.CMD".

Once saved, just change the task in task scheduler to run the cmd file instead, and you should be good to go! Naturally, try it and let me know. :)

Excellent! Thank you so much for your explanation! That was very helpful. We are on the right track!

What I am trying to do, is avoid using the synctoycmd.exe that does not produce the result of the sync on my screen. I want to use synctoy.exe.

Here is the problem I ran into. With your help I was able to create the batch file and point task scheduler towards it. Synctoy will open perfectly if I dont add any arguments. If I try to add "-R" at the end it will tell me "The ability to execute folder pairs from the command line is now available from synctoycmd.exe" and the program wont open.

So my question is, how to the write the batch file to open synctoy.exe than tell it to run all folder pairs?

Thank you!!

Well.. just for kicks, I downloaded SyncToy (I also saw a question on their forums, was that you? ;) )

The main executable does not have many options.. if you run "Synctoy.exe /?" from a cmd prompt, it basically looks like only -d, which defines a new folder pair, and -u, which removes (undefines?) a folder pair.

Looking at SyncToyCmd.exe, much like you say, it doesn't have any options for providing a report.

I have a hacky little tool you can try - I've just written this. It specifically finds the SyncToy window, and "clicks" the Run button for you. Since I don't use the tool I don't see any other way around it... source is included but naturally I can't take responsibility for this software, it's completely As-Is. ;)

So extract this into your same folder as the batch file. Then, insert a new line after SyncToy.exe and before 'Pause', and call it like this:

     ClickButton.exe SyncToy Run

Watch case - the upper/lowercase letters have to match the window and button exactly.

So your final .CMD file would look like:

    synctoycmd.exe
    ClickButton.exe SyncToy Run
    pause

That works here. That's the good news. However, this script can't select 'All Folder Pairs' for you, since that's not a button. However, in my testing here, SyncToy loads with whatever setting you last left it on, so if you select All Folder Pairs, then exit it, next time it launches it should remember that.

That's a bit of a hacky solution, but maybe it'll be helpful, and I've needed to write this little tool for a while now anyway. ;)
(Hmm.. did the zip attach? Trying again...)
ClickButton.zip
Thats brilliant! But it didn't work on my end. Ill past my text so you can look at it!

"C:\Program Files\SyncToy2.1\SyncToy.exe"
ClickButton.exe SyncToy Run
pause

It opens synctoy just fine, but no button is ever clicked.

Thanks Tursilion! Your awesome!
BTW if I try to run ClickButton directly it gives me the following error.

"This application has failed to start because the application configuration is incorrect. Reinstalling the application may fix this problem."
Ah... manifests. Basically it's looking for a very specific configuration. I should be able to fix that with a slightly different compile. Sorry about that!

This one is compiled with Visual Studio 6, whose runtime tends to be on most systems, so it should just go.

Also, I noticed a slight difference when I tested it here... when "All Folder Pairs" are selected, the name of the button changes to "Run All", so that changes the command line a little :)

  ClickButton.exe SyncToy "Run All"

Note the quotes are needed because of the space in the middle.

See if that one does it! :)

ClickButtonVS6.zip
Awesome! We are very close!
It works, but only if i run the batch file manually and only if the synctoy window is already open (does not have to be active)
If i leave it to the task scheduler, it will open the program, or maximize it if already open, but will not "Run All" unless synctoy is open and active.

I hope that makes sense

FYI
It seems that the click button only recognizes one word of the button to click, so i put it in quotes and that solved the problem.

"C:\Program Files\SyncToy 2.1\SyncToy.exe"
ClickButton.exe SyncToy "Run All"
pause


I had to put Run All in quotes because it only recognized the first word
That's right.. any phrase with spaces needs to be in quotes.

Most likely the failure is just because the batch file is not waiting for SyncToy to launch before it looks for the button. The easiest way to fix that is to put a small delay between launching SyncToy and trying to click the button.

Luckily, the command prompt offers such a delay command, so let's try 5 seconds (should be lots of time?) -- the command is "sleep 5".

So something like this:

  "C:\Program Files\SyncToy 2.1\SyncToy.exe"
  sleep 5
  ClickButton.exe SyncToy "Run All"
  pause

If the delay is still too short, ClickButton should be printing a warning that it could not find the window or the button - that's your clue to make it longer. There's no harm in it being too long, especially since you aren't there when it runs.

One thing I did not consider.. this probably won't work if you are not logged in.. if you log out of your machine I don't know whether it will do anything at all. I assume that's not the case since you expect to see the summary screen in the morning.
Great idea! But I got an error:
'sleep' is not recognized as an internal or external command, operable program or batch file.

"C:\Program Files\SyncToy 2.1\SyncToy.exe"
sleep 5
ClickButton.exe SyncToy "Run All"
pause
Hm.. that's unexpected. Sleep should be part of the command processor.

A little research shows me that 'Sleep' is part of the resource kit, and not part of the normal operating system install. I've always had it because I always have the development tools installed. I learned something new today. :)

We can use a workaround with ping ;) This is a little bit hacky, but try this line instead of the sleep::

ping -n 5 127.0.0.1 >NUL

Ping is a command that tests to see if a network address is reachable - it sends a packet and waits for a reply.

The -n tells it how many packets to send.. it will wait 1 second between each one (so technically '5' means 4 seconds are needed).

127.0.0.1 is called the "loopback" address. It always points back at the same computer that is originating. This means it won't send any real traffic or try to reach a real host, and the response will always come back.

>NUL just redirects the output to hide it. '>' is the redirect, and 'NUL' is a special filename that means null, or nothing. If you drop that part, you can see the ping work, and it looks like this:

C:\new>ping -n 5 127.0.0.1

Pinging 127.0.0.1 with 32 bytes of data:

Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128

Ping statistics for 127.0.0.1:
    Packets: Sent = 5, Received = 5, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms

So! Like Bullwinkle says, "This time for sure!"

  "C:\Program Files\SyncToy 2.1\SyncToy.exe"
  ping -n 5 127.0.0.1 >NUL
  ClickButton.exe SyncToy "Run All"
  pause

And as described in the bit about sleep, raise the number 5 if it's not giving enough time for SyncToy to get ready. That will depend on your machine and configuration so it may take some fiddling.
Man your smart!
But there is still a problem. The synctoy window opens, and stays there. Nothing else happens. Not even the command prompt advances to the ping unless I close synctoy. No matter how long I leave the synctoy window open, it wont run the ping command untill i close syctoy. Then it will tell me "cant find "run all" button on "synctoy" window.

"C:\Program Files\SyncToy 2.1\SyncToy.exe"
ping -n 5 127.0.0.1 >NUL
ClickButton.exe SyncToy "Run All"
pause
ASKER CERTIFIED SOLUTION
Avatar of Tursilion
Tursilion
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hey sorry about not mentioning the hangup on the batch file!
Your amazing! It works perfectly now! Yeah its a little hacky but who cares! It works great!
Thank you so much for going above and beyond to help me figure this out! I wish i could give you more than 500 ppints!
Tursilion was the most helpful person I have run into so far on this website. Very fast to respond, very personable, very detailed, and very eager to help!

Thank You!