<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Vista: Run a script at shutdown</title>
	<atom:link href="http://ukstokes.com/blog/2008/01/19/vista-run-a-script-at-shutdown/feed/" rel="self" type="application/rss+xml" />
	<link>http://ukstokes.com/blog/2008/01/19/vista-run-a-script-at-shutdown/</link>
	<description>tech stuff from a tech bloke</description>
	<lastBuildDate>Fri, 18 Jun 2010 16:00:40 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: ben</title>
		<link>http://ukstokes.com/blog/2008/01/19/vista-run-a-script-at-shutdown/comment-page-1/#comment-11438</link>
		<dc:creator>ben</dc:creator>
		<pubDate>Fri, 04 Jun 2010 16:27:44 +0000</pubDate>
		<guid isPermaLink="false">http://ukstokes.com/blog/?p=85#comment-11438</guid>
		<description>Cool, I am glad you got it to work!</description>
		<content:encoded><![CDATA[<p>Cool, I am glad you got it to work!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jakob G</title>
		<link>http://ukstokes.com/blog/2008/01/19/vista-run-a-script-at-shutdown/comment-page-1/#comment-11416</link>
		<dc:creator>Jakob G</dc:creator>
		<pubDate>Thu, 03 Jun 2010 12:17:31 +0000</pubDate>
		<guid isPermaLink="false">http://ukstokes.com/blog/?p=85#comment-11416</guid>
		<description>No, actually I was wrong (I think). The shutdown didn&#039;t fail because of active dialog boxes. No, it failed because I didn&#039;t run my backup software in silent mode. After correcting this mistake (of mine) your script works flawlessly, Ben! Thanks again and if possible, please delete my previous post :-)</description>
		<content:encoded><![CDATA[<p>No, actually I was wrong (I think). The shutdown didn't fail because of active dialog boxes. No, it failed because I didn't run my backup software in silent mode. After correcting this mistake (of mine) your script works flawlessly, Ben! Thanks again and if possible, please delete my previous post <img src='http://ukstokes.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jakob G</title>
		<link>http://ukstokes.com/blog/2008/01/19/vista-run-a-script-at-shutdown/comment-page-1/#comment-11379</link>
		<dc:creator>Jakob G</dc:creator>
		<pubDate>Wed, 02 Jun 2010 01:17:29 +0000</pubDate>
		<guid isPermaLink="false">http://ukstokes.com/blog/?p=85#comment-11379</guid>
		<description>OK, apparently there&#039;s no need for me to pull the trigger: Although I&#039;m a complete ignorant when it comes to VB scripts, I managed to get yours to work and I&#039;m now testing it with &quot;Data Replicator 3&quot; from Synology. You are the man, Ben Stokes! Thank you very much!</description>
		<content:encoded><![CDATA[<p>OK, apparently there's no need for me to pull the trigger: Although I'm a complete ignorant when it comes to VB scripts, I managed to get yours to work and I'm now testing it with "Data Replicator 3" from Synology. You are the man, Ben Stokes! Thank you very much!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jakob G</title>
		<link>http://ukstokes.com/blog/2008/01/19/vista-run-a-script-at-shutdown/comment-page-1/#comment-11377</link>
		<dc:creator>Jakob G</dc:creator>
		<pubDate>Tue, 01 Jun 2010 23:57:20 +0000</pubDate>
		<guid isPermaLink="false">http://ukstokes.com/blog/?p=85#comment-11377</guid>
		<description>OMG!! Ben, thanks! If this works, I won&#039;t have to kill myself!!</description>
		<content:encoded><![CDATA[<p>OMG!! Ben, thanks! If this works, I won't have to kill myself!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ben</title>
		<link>http://ukstokes.com/blog/2008/01/19/vista-run-a-script-at-shutdown/comment-page-1/#comment-1116</link>
		<dc:creator>ben</dc:creator>
		<pubDate>Fri, 17 Jul 2009 13:17:22 +0000</pubDate>
		<guid isPermaLink="false">http://ukstokes.com/blog/?p=85#comment-1116</guid>
		<description>Awesome - thanks!</description>
		<content:encoded><![CDATA[<p>Awesome - thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: RSquared</title>
		<link>http://ukstokes.com/blog/2008/01/19/vista-run-a-script-at-shutdown/comment-page-1/#comment-1063</link>
		<dc:creator>RSquared</dc:creator>
		<pubDate>Tue, 14 Jul 2009 15:02:10 +0000</pubDate>
		<guid isPermaLink="false">http://ukstokes.com/blog/?p=85#comment-1063</guid>
		<description>Just a correction note.  The End If statement in the first code listing should be moved to the end of the file.  Sorry guys, forgot to move it from a debugging position.</description>
		<content:encoded><![CDATA[<p>Just a correction note.  The End If statement in the first code listing should be moved to the end of the file.  Sorry guys, forgot to move it from a debugging position.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: RSquared</title>
		<link>http://ukstokes.com/blog/2008/01/19/vista-run-a-script-at-shutdown/comment-page-1/#comment-1052</link>
		<dc:creator>RSquared</dc:creator>
		<pubDate>Mon, 13 Jul 2009 21:36:13 +0000</pubDate>
		<guid isPermaLink="false">http://ukstokes.com/blog/?p=85#comment-1052</guid>
		<description>@Ben, You have made me spend the last few days coding and refining your idea!  I have the same problem, but I store all of my data in a subdirectory off of the root (C:\My Data).  This is to ease syncing of data.

Only several programs or settings require me to save data within the Windows standard directory system.  These I need to sync into the My Data locations (Start Menu, App Launcher, Favorites, Send To, etc.).

The problem is to sync in a specific order, and to easily add new Folder Pairs. I also wanted to see what SyncToy actually did (could be at a later time) and I was annoyed at looking at blank Cmd windows.

The following is what I came up with, and I hope you might like it as well.

[code lang=&quot;vb&quot;]Const WriteLogs = True &#039; True to create Log Files for each Sync, False not to
Const ProgramDirectory = &quot;C:\Program Files\SyncToy 2.0&quot; &#039; The Directory where SyncToy is installed
Const LogsDirectory = &quot;C:\My Data\My System\SyncToy\Logs&quot; &#039; The Directory where the Log Files are to be written
Const DataFile = &quot;SyncFolder List.txt&quot; &#039; The name of the SyncFolder data file

Set wshShell = WScript.CreateObject (&quot;WScript.shell&quot;) &#039; Create the WScript Objects to execute the program and display PopUps

Set objFSO = CreateObject(&quot;Scripting.FileSystemObject&quot;) &#039; Create the FileSystemObject
Set objDataFile = objFSO.OpenTextFile( wshShell.CurrentDirectory &amp; &quot;\&quot; &amp; DataFile, 1) &#039; Create the TextStream for the Data File

Set objParameters = WScript.Arguments &#039; Create Argument object to read the parameters passed to the script

strAction = objParameters(0) &#039; Store the Action to be taken, Hibernate, Restart, Shutdown, Standby
strCmd = objParameters(1) &#039; Store the Program and it&#039;s parametets

&#039; Ensure that the requested action is desired
lngMsgBox = MsgBox (&quot;Are you sure you want to &quot; &amp; LCase(strAction) &amp; &quot; the system?&quot;, _
	vbYesNo + VBCritical, strAction &amp; &quot; computer&quot;)

If lngMsgBox = vbYes then &#039; If the user wants to continue, do so

    &#039; This box will disappear after 3 seconds. Display action notification
    intReturn = wshShell.Popup(&quot;The computer will now sync and &quot; &amp; LCase(strAction) &amp; &quot;.&quot;, 3, _
		strAction &amp; &quot; computer&quot;, VBInformation)

	Do While Not objDataFile.AtEndOfStream &#039; Step through the data file and execute the listed Folder Pair

		strFolderPair = objDataFile.ReadLine &#039; Read the line containing the Folder Pair name
		
		If WriteLogs Then &#039; Set up the CmdLine based on whether or not to create log files
		
			&#039; Use CMD.exe to execute the command line using piping - otherwise it won&#039;t work
			&#039; &amp;&amp; allows Cmd.exe to execute multiple commands on 1 line, so display FolderPair in DOS window
			strCmdLine = &quot;Cmd.exe /C Echo Syncing &quot; &amp; strFolderPair &amp; &quot;&amp;&amp;&quot;&quot;&quot; &amp; ProgramDirectory &amp;  _
				&quot;\SyncToyCmd.exe&quot;&quot; -R &quot;&quot;&quot;  &amp; strFolderPair &amp; &quot;&quot;&quot; &gt; &quot;&quot;&quot; &amp; LogsDirectory &amp; &quot;\&quot; &amp; strFolderPair &amp; &quot;.log&quot;&quot;&quot;&quot;&quot;
				
		Else
		
			&#039; Cmd line if no log file is to be written
			strCmdLine = &quot;Cmd.exe /C Echo Syncing &quot; &amp; strFolderPair &amp; &quot;&amp;&amp;&quot; &amp; ProgramDirectory &amp;  _
				&quot;\SyncToyCmd.exe&quot;&quot; -R &quot;&quot;&quot; &amp; 	strFolderPair &amp; &quot;&quot;&quot;&quot;

		End If
	
		wshShell.run strCmdLine, 5, True &#039; Execute the command

	Loop

End If

objDataFile.Close &#039; Close the datafile

&#039; This box will disappear after 3 seconds. Display Sync Completion and action to be taken
intReturn = wshShell.Popup(&quot;Completed syncing files.&quot; &amp; vbcrlf &amp; vbcrlf &amp; &quot;The computer will now &quot; &amp; LCase(strAction) &amp; &quot;.&quot;, 3, _
	strAction &amp; &quot; computer&quot;, VBInformation)

Set objResult = wshShell.exec( &quot;%windir%\System32\&quot; &amp; strCmd ) &#039; Actually carry out desired action[/code]

To use the above code, just create a shortcut to it and add the action (Hibernate, Restart, Shutdown, or Standby) as the first parameter.  The second parameter is the program name with it&#039;s parameters - &quot;Shutdown.exe /h&quot; for Hibernate (/r for Restart, /s for Shutdown).  For Standby it is &quot;C:\My Data\My System\SyncToy\Scripts\Sync with Action.vbs&quot; &quot;Standby&quot; &quot;rundll32.exe PowrProf,SetSuspendState&quot;

The code stores path information in constants so there is only 1 place that needs changing.

The &amp;&amp; separates commands in CMD statements, so the action being carried out is echoed in the DOS window.

Lastly, the textfile SyncFolderList.txt is simply a text file with a listing of all of the folder pairs I want synced (each on it&#039;s own line)

I created a separate file, Sync All.vbs that does all of the syncing without any other action taking place, it is listed below.

[code lang=&quot;vb&quot;]Const WriteLogs = True &#039; True to create Log Files for each Sync, False not to
Const ProgramDirectory = &quot;C:\Program Files\SyncToy 2.0&quot; &#039; The Directory where SyncToy is installed
Const LogsDirectory = &quot;C:\My Data\My System\SyncToy\Logs&quot; &#039; The Directory where the Log Files are to be written
Const DataFile = &quot;SyncFolder List.txt&quot; &#039; The name of the SyncFolder data file

&#039; Create the WScript Objects to execute the program
Set wshShell = WScript.CreateObject (&quot;WSCript.shell&quot;)

Set objFSO = CreateObject(&quot;Scripting.FileSystemObject&quot;) &#039; Create the FileSystemObject
Set objDataFile = objFSO.OpenTextFile( wshShell.CurrentDirectory &amp; &quot;\&quot; &amp; DataFile, 1) &#039; Create the TextStream for the Data File

Do While Not objDataFile.AtEndOfStream &#039; Step through the data file and execute the listed Folder Pair

	strFolderPair = objDataFile.ReadLine &#039; Read the line containing the Folder Pair name

	If WriteLogs Then &#039; Set up the CmdLine based on whether or not to create log files
	
		&#039; Use CMD.exe to execute the command line using piping - otherwise it won&#039;t work
		strCmdLine = &quot;Cmd.exe /C Echo Syncing &quot; &amp; strFolderPair &amp; &quot;&amp;&amp;&quot;&quot;&quot; &amp; ProgramDirectory &amp;  &quot;\SyncToyCmd.exe&quot;&quot; -R &quot;&quot;&quot;  &amp; _
			strFolderPair &amp; &quot;&quot;&quot; &gt; &quot;&quot;&quot; &amp; LogsDirectory &amp; &quot;\&quot; &amp; strFolderPair &amp; &quot;.log&quot;&quot;&quot;&quot;&quot;
			
	Else
	
		strCmdLine = &quot;Cmd.exe /C Echo Syncing &quot; &amp; strFolderPair &amp; &quot;&amp;&amp;&quot; &amp; ProgramDirectory &amp;  &quot;\SyncToyCmd.exe&quot;&quot; -R &quot;&quot;&quot; &amp; 	strFolderPair &amp; &quot;&quot;&quot;&quot;

	End If

	wshShell.run strCmdLine, 5, True &#039; Execute the command line

Loop
	
objDataFile.Close &#039; Close the text file[/code]

To solve the problem of making sure to use these to shutdown, etc., I actually removed all of the shutdown options from the Start Menu and have these shortcuts at the top of my App Launcher on the SideBar.

I hope you and others will find this helpful.</description>
		<content:encoded><![CDATA[<p>@Ben, You have made me spend the last few days coding and refining your idea!  I have the same problem, but I store all of my data in a subdirectory off of the root (C:\My Data).  This is to ease syncing of data.</p>
<p>Only several programs or settings require me to save data within the Windows standard directory system.  These I need to sync into the My Data locations (Start Menu, App Launcher, Favorites, Send To, etc.).</p>
<p>The problem is to sync in a specific order, and to easily add new Folder Pairs. I also wanted to see what SyncToy actually did (could be at a later time) and I was annoyed at looking at blank Cmd windows.</p>
<p>The following is what I came up with, and I hope you might like it as well.</p>
<pre class="brush: vb;">Const WriteLogs = True ' True to create Log Files for each Sync, False not to
Const ProgramDirectory = "C:\Program Files\SyncToy 2.0" ' The Directory where SyncToy is installed
Const LogsDirectory = "C:\My Data\My System\SyncToy\Logs" ' The Directory where the Log Files are to be written
Const DataFile = "SyncFolder List.txt" ' The name of the SyncFolder data file

Set wshShell = WScript.CreateObject ("WScript.shell") ' Create the WScript Objects to execute the program and display PopUps

Set objFSO = CreateObject("Scripting.FileSystemObject") ' Create the FileSystemObject
Set objDataFile = objFSO.OpenTextFile( wshShell.CurrentDirectory &amp; "\" &amp; DataFile, 1) ' Create the TextStream for the Data File

Set objParameters = WScript.Arguments ' Create Argument object to read the parameters passed to the script

strAction = objParameters(0) ' Store the Action to be taken, Hibernate, Restart, Shutdown, Standby
strCmd = objParameters(1) ' Store the Program and it's parametets

' Ensure that the requested action is desired
lngMsgBox = MsgBox ("Are you sure you want to " &amp; LCase(strAction) &amp; " the system?", _
	vbYesNo + VBCritical, strAction &amp; " computer")

If lngMsgBox = vbYes then ' If the user wants to continue, do so

    ' This box will disappear after 3 seconds. Display action notification
    intReturn = wshShell.Popup("The computer will now sync and " &amp; LCase(strAction) &amp; ".", 3, _
		strAction &amp; " computer", VBInformation)

	Do While Not objDataFile.AtEndOfStream ' Step through the data file and execute the listed Folder Pair

		strFolderPair = objDataFile.ReadLine ' Read the line containing the Folder Pair name

		If WriteLogs Then ' Set up the CmdLine based on whether or not to create log files

			' Use CMD.exe to execute the command line using piping - otherwise it won't work
			' &amp;&amp; allows Cmd.exe to execute multiple commands on 1 line, so display FolderPair in DOS window
			strCmdLine = "Cmd.exe /C Echo Syncing " &amp; strFolderPair &amp; "&amp;&amp;""" &amp; ProgramDirectory &amp;  _
				"\SyncToyCmd.exe"" -R """  &amp; strFolderPair &amp; """ &gt; """ &amp; LogsDirectory &amp; "\" &amp; strFolderPair &amp; ".log"""""

		Else

			' Cmd line if no log file is to be written
			strCmdLine = "Cmd.exe /C Echo Syncing " &amp; strFolderPair &amp; "&amp;&amp;" &amp; ProgramDirectory &amp;  _
				"\SyncToyCmd.exe"" -R """ &amp; 	strFolderPair &amp; """"

		End If

		wshShell.run strCmdLine, 5, True ' Execute the command

	Loop

End If

objDataFile.Close ' Close the datafile

' This box will disappear after 3 seconds. Display Sync Completion and action to be taken
intReturn = wshShell.Popup("Completed syncing files." &amp; vbcrlf &amp; vbcrlf &amp; "The computer will now " &amp; LCase(strAction) &amp; ".", 3, _
	strAction &amp; " computer", VBInformation)

Set objResult = wshShell.exec( "%windir%\System32\" &amp; strCmd ) ' Actually carry out desired action</pre>
<p>To use the above code, just create a shortcut to it and add the action (Hibernate, Restart, Shutdown, or Standby) as the first parameter.  The second parameter is the program name with it's parameters - "Shutdown.exe /h" for Hibernate (/r for Restart, /s for Shutdown).  For Standby it is "C:\My Data\My System\SyncToy\Scripts\Sync with Action.vbs" "Standby" "rundll32.exe PowrProf,SetSuspendState"</p>
<p>The code stores path information in constants so there is only 1 place that needs changing.</p>
<p>The &amp;&amp; separates commands in CMD statements, so the action being carried out is echoed in the DOS window.</p>
<p>Lastly, the textfile SyncFolderList.txt is simply a text file with a listing of all of the folder pairs I want synced (each on it's own line)</p>
<p>I created a separate file, Sync All.vbs that does all of the syncing without any other action taking place, it is listed below.</p>
<pre class="brush: vb;">Const WriteLogs = True ' True to create Log Files for each Sync, False not to
Const ProgramDirectory = "C:\Program Files\SyncToy 2.0" ' The Directory where SyncToy is installed
Const LogsDirectory = "C:\My Data\My System\SyncToy\Logs" ' The Directory where the Log Files are to be written
Const DataFile = "SyncFolder List.txt" ' The name of the SyncFolder data file

' Create the WScript Objects to execute the program
Set wshShell = WScript.CreateObject ("WSCript.shell")

Set objFSO = CreateObject("Scripting.FileSystemObject") ' Create the FileSystemObject
Set objDataFile = objFSO.OpenTextFile( wshShell.CurrentDirectory &amp; "\" &amp; DataFile, 1) ' Create the TextStream for the Data File

Do While Not objDataFile.AtEndOfStream ' Step through the data file and execute the listed Folder Pair

	strFolderPair = objDataFile.ReadLine ' Read the line containing the Folder Pair name

	If WriteLogs Then ' Set up the CmdLine based on whether or not to create log files

		' Use CMD.exe to execute the command line using piping - otherwise it won't work
		strCmdLine = "Cmd.exe /C Echo Syncing " &amp; strFolderPair &amp; "&amp;&amp;""" &amp; ProgramDirectory &amp;  "\SyncToyCmd.exe"" -R """  &amp; _
			strFolderPair &amp; """ &gt; """ &amp; LogsDirectory &amp; "\" &amp; strFolderPair &amp; ".log"""""

	Else

		strCmdLine = "Cmd.exe /C Echo Syncing " &amp; strFolderPair &amp; "&amp;&amp;" &amp; ProgramDirectory &amp;  "\SyncToyCmd.exe"" -R """ &amp; 	strFolderPair &amp; """"

	End If

	wshShell.run strCmdLine, 5, True ' Execute the command line

Loop

objDataFile.Close ' Close the text file</pre>
<p>To solve the problem of making sure to use these to shutdown, etc., I actually removed all of the shutdown options from the Start Menu and have these shortcuts at the top of my App Launcher on the SideBar.</p>
<p>I hope you and others will find this helpful.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: nitzy</title>
		<link>http://ukstokes.com/blog/2008/01/19/vista-run-a-script-at-shutdown/comment-page-1/#comment-242</link>
		<dc:creator>nitzy</dc:creator>
		<pubDate>Wed, 27 May 2009 12:53:40 +0000</pubDate>
		<guid isPermaLink="false">http://ukstokes.com/blog/?p=85#comment-242</guid>
		<description>Mate, Thanks for this, I have been trying to work out a best way to sync for sales guys and you my friend has helped me tremendously to make this process of shutting down and syncing ridiculously easy. I tried Lastchance as a program which worked well, however it just logs you off instead of shutting down in XP which was not really acceptable as these guys would just moan cos they have to do an extra step...Thanks again</description>
		<content:encoded><![CDATA[<p>Mate, Thanks for this, I have been trying to work out a best way to sync for sales guys and you my friend has helped me tremendously to make this process of shutting down and syncing ridiculously easy. I tried Lastchance as a program which worked well, however it just logs you off instead of shutting down in XP which was not really acceptable as these guys would just moan cos they have to do an extra step...Thanks again</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Aaron</title>
		<link>http://ukstokes.com/blog/2008/01/19/vista-run-a-script-at-shutdown/comment-page-1/#comment-125</link>
		<dc:creator>Aaron</dc:creator>
		<pubDate>Tue, 05 May 2009 16:03:23 +0000</pubDate>
		<guid isPermaLink="false">http://ukstokes.com/blog/?p=85#comment-125</guid>
		<description>@Ben: Yeah, I found out about Home Premium&#039;s lack of gpedit.msc the hard way, just a few minutes ago, while trying to write a blog entry about setting up shutdown scripts.  Kinda jumped up and bit me in the ass...

Nice script, tho.  I&#039;ll make sure to point people your direction, and I&#039;ll actually probably end up using a variation myself on my new laptop.  Keep up the good work!</description>
		<content:encoded><![CDATA[<p>@Ben: Yeah, I found out about Home Premium's lack of gpedit.msc the hard way, just a few minutes ago, while trying to write a blog entry about setting up shutdown scripts.  Kinda jumped up and bit me in the ass...</p>
<p>Nice script, tho.  I'll make sure to point people your direction, and I'll actually probably end up using a variation myself on my new laptop.  Keep up the good work!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben Stokes</title>
		<link>http://ukstokes.com/blog/2008/01/19/vista-run-a-script-at-shutdown/comment-page-1/#comment-124</link>
		<dc:creator>Ben Stokes</dc:creator>
		<pubDate>Sun, 01 Mar 2009 22:19:39 +0000</pubDate>
		<guid isPermaLink="false">http://ukstokes.com/blog/?p=85#comment-124</guid>
		<description>Hey, cheers for your comment. That would have been my preferred path too (it&#039;s how I used to do this on Windows XP Pro) but as you suspected gpedit.msc is not implemented on Vista Home Premium!</description>
		<content:encoded><![CDATA[<p>Hey, cheers for your comment. That would have been my preferred path too (it's how I used to do this on Windows XP Pro) but as you suspected gpedit.msc is not implemented on Vista Home Premium!</p>
]]></content:encoded>
	</item>
</channel>
</rss>
