Click here to Skip to main content
16,016,489 members
Articles / Batch
Tip/Trick

Simplify your work life using a batch!

Rate me:
Please Sign up or sign in to vote.
4.55/5 (8 votes)
2 Aug 2016CPOL5 min read 18.3K   77   8  
A simple batch code that can help you start your work day
This is an old version of the currently published tip/trick.

Introduction

Ok, so this is monday mornig. Your brain is still sleeping and you need someone to tell you what to do with this bright screen. What about someone that can help you, at least by starting the applications you usually need?

The code

By writing a simple batch file and let it run automatically in the start-up phase of our operating system, can achieve our target: automate the start of usual applications. Let's start from the basics:

Create the batch file

You can use a lot of different tools to do that, but we will simply use Notepad. Create a new file, save it as "StartApplications.bat" or whatever you want. The important thing here is the ".bat" extension.

Now we can populate the file with our script. 

Turn off the command line echo

In this case we don't want to print out to the console every command we are using. So the first line of our batch will be...

C++
@echo off

We will use echo to print out information only when we need them.

Start all the apps or only the ones you need today

Let's say that you have a list of application that you use every day. In my case these applications are: a browser (let's say chrome), an IDE (let's say Visual Studio), an e-mail client (let's say Outlook), an so on...

So I have my personal list of applications, you will have yours...

But suppose that today you will do something different. We don't want to open all these application only to close them after few seconds. We want to choose if start all these application or select the ones we need.

And thats how we will do that:

First of all, we print out the list of applications that we can start:

ECHO Here are some apps you will probably need today: 
ECHO. -Visual Studio 
ECHO. -SourceTree 
ECHO. -Outlook 
ECHO. -Chrome
ECHO. -Spotify

Then we choose if run all the applications or not:

SET /p startall=Do you want to start them all? [y/n]: 

IF "%startAll%"== "y" GOTO StartAll

SET all=0

Note the use of the SET comand with the /p parameter. Using this parameter we are telling the program to read the value of the startAll variable from the next user input.

What's the meaning of the GOTO command, and what will happen when we will go to "StartAll"? What's the aim of the all variable?

We will answer this question in a minute, but first, let's say that the user don't want to start all the applications, so the startAll variable is not "y" and the all variable is set to "0". Let's see how to proceed.

Senario #1: We want to choose which apps will be started

Let's say that today we don't want to read our e-mails or we don't feel like writing code (!!!). No problems, our little program will ask us about each of our apps to see if we want to start it or no:

SET /p visualStudio=Do you want to start Visual Studio? [y/n]: 

As before, we are using the SET comand to read the user input. Let's use this value:

IF "%visualStudio%"=="y" GOTO StartVisualStudio:

:AfterVisualStudioStart

[...].
:StartVisualStudio (
CD "C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE"
START devenv.exe

IF %all%==0 GOTO AfterVisualStudioStart
)

Here we are. If we need to start Visual Studio what we need to do is to use the CD command to reach the executable directory and use the START command to run the executable (please note that these paths can be different in your machine!).

After the start of Visual Studio we need to return to our code to ask the user for the other apps. We do this only is the all variable is set to "0", so only if we want to choose which apps will be started and which not. 

Let's repeat these operation for the other applications using the same schema: 

Ask the user: Do you want to start this app?
if yes --> go to the code that start the app and then turn back

If we type something different from "y" we will simply skip to the other question.

Senario #2: Start all the apps

Let's say that we wan't all the apps we have in our list. So let's skip all the previous questions and go to the point immediatly before the start of our pseudo routine. As we see before, we do that by calling the GOTO command:

IF "%startAll%"== "y" GOTO StartAll

Well, so we can place the StartAll bookmark after the questions block and then set the all variable to any value different from "0".

:StartAll
SET all=1

In this way, none of our routines will return to a previous point in the code. Thats because we have insterted a specific condition:

IF %all%==0 GOTO AfterVisualStudioStart

All the apps will be started with no user interaction, we just need to grab our hot coffe and wait for them.

Comfortable, don't you think? What a lazy person...

Add the .bat file to the start-up programma

So, we are done with our file. Now we need to let this batch run every time we log in.
 
We can do that by adding our file to the star-up folder. We just need to press Windows key + R and run "shell:startup” (without the quotes) in the “Open” edit box and click “OK.” By copying the batch here, we are telling Windows to run it every time a new user session start.

Points of Interest

I hope you have enjoyed this brief article. One of my targets was to explain some of the basic commands that we can use in a batch script and to do this in a practical and funny way. I think that, as programmer, sometimes is good to see us from the outside to apply our knowledge in our daylife.  

In the attachment you can find a copy of my personal batch, so you can start by modify if don't want to start from scratch (sorry, I will upload it in the next hours!).

 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Italy Italy
I'm a young back-end developer with an hidden passion for front-end development (but not enough time!)

I'm a guitarist and an avid music listener, one of my most important development tools are my headphones!

I'm still learning a lot of things in the field (English language is one of these things!), so I'm very open to suggestions and comments.

Comments and Discussions

Discussions on this specific version of this article. Add your comments on how to improve this article here. These comments will not be visible on the final published version of this article.