Run batch file during installation

I got new experience on my recent window based application. A requirement came to create windows task scheduler using batch file and also run that batch file along with installation of the application. Follow the below steps to perform same - 

1. Create windows task scheduler using command prompt -

As per requirement, to create a windows task scheduler which will repeat after every three days. 

Task scheduler through command prompt-


  • Delete task scheduler, if it already exist -
schtasks /delete /tn "TestScheduler" /F


  • Create task scheduler which will repeat after every three days

schtasks /create /tn "TestScheduler" /sc DAILY /MO 3 /tr "\"C:\Windows\Notepad.exe" \"AUTOEXTRACTSCHEDULER\""

abbreviation -

tn   - Task name
sc   - Schedule type
MO - Modifier
tr   - Task run

and also we can include an command line argument at last, follow by \"<Argument>".

Check the below links for more detail -

http://technet.microsoft.com/en-us/library/cc748993.aspx

http://msdn.microsoft.com/en-us/library/windows/desktop/bb736357(v=vs.85).aspx


2. Copy and paste required command into notepad file and create a batch file and save it into desktop. (ie.  TestScheduler.bat)

3. Include batch file into setup file of window based applicatoin/WPF setup.

Follow the below steps -

  • In the setup folder of windows application, right click and select           view - [File system]




 

  • Go to application folder and add cmd.exe (C:\Windows\System32\cmd.exe) and also batch file which you have created above(TestScheduler.bat).


  • Once file is added to the application folder, again go back to application setup folder then right click and Go to view - [Custom actions]
  • Requirement was to Create windows task in scheduler during installation and remove windows task during application un-install. So for that we created two batch file for create and delete task respectively.


  • At installation - Under command actions go to commit, right click and add custom action, select cmd.exe file from application folder


  • Right click on cmd.exe under commit, select property window -








  • Replace /commit by /c "[TARGETDIR]\AutoExtraction.bat" in the argument section.





  • Rebuild the solution and create new setup and install the application, windows task should be created under windows task library. In the same manner if you uninstall application then same windows task should be removed from windows task library. To apply the same follow the below steps -
Step 1 - In the same custom action block add one more custom action under Uninstall - (Find the below screen shot for reference)

Step 2 - Go to application folder and add cmd.exe.
(Find the below screen shot for reference)


Step 3 - Right click on cmd.exe and select Property Window and make changes as per below screen shot -
where RemoveAutoExtraction.bat is the batch file which contains delete command of same task scheduler.

Conclusion: In this post i tried to explain how to create and delete a windows task scheduler using a batch file during install and uninstall of WPF application setup.

I hope this will help, Please feel free to post if there are more methods.

Rate this post if it is helpful.


Other references -








Post a Comment

2 Comments

  1. it works for me..
    thank u!

    ReplyDelete
  2. This doesn't actually create the task in VS2012. This does not work.

    ReplyDelete