Make your dev life easier with this good old friend

0
1708

Hi Windowsgeeks! Today I am not gonna talk about a new feature or new tool but a pretty old tool which is coming from the age of DOS. If you are wondering well I will tell you that’s not something other than command prompt. Actually for us developers this tool could be really useful. But I hardly see people, use this to make their day to day life easier. I recently migrated from the linux to Windows. In Linux people use to write shell scripts often. And they use their shells to execute command in their day to day tasks. But in windows this is not the case. But if you start to use command line you will realize sometimes it’s faster than using mouse. Let’s say you want to copy some files from one location to another, or you just wanna delete some files. These all the things could be achieved using command prompt.

But today I am gonna talk about something different. Let’s talk about how to write batch files in windows. So what exactly is a batch file ? Batch file is nothing more than just a set of command write in a file which has the extension of .bat or .cmd so you can execute this file by double clicking it or running it in the command line. Then batch file will execute the commands in it sequentially. Pretty simple right ?

Okay let me start with an example. In my day to day life I often come across with this problem. When I want to start a web project I need to open opera, to search the internet. And I need chrome, because I use dev tools in Chrome. And let’s say we need to open a text editor or some IDE. We need to open this text editor or IDE with the location where I use to store my web projects. For this task  I have to open opera, and then chrome after that I should run xamp or wamp server and then I should go to the location of the web projects and open them. I should start the IDE and then I have to select the web project that I need to edit. How many mouse clicks do I need to start the job ? And I daily do that I am bored now. So can we do this with a one command? or one mouse click ? yes you can. Let’s write a batch file !!!.

Okay first I will open up a command prompt. And I will go to a location where I am gonna save my batch files.  And I should mention you that you should be a bit familiar with commands like “cd” “mkdir” “del” a bit in order to use  the command prompt. But don’t worry that’s not a big deal. You can just type help command and a list of usual commands will appear. And if you need to see more detail about a command you can just type that command with the “/?” tag at the end which will display a full description of the command. Try commands like “cd, mkdir, del” for yourself. Okay and now I am in the location where I am going to write my batch files. So I would open a notepad, to start writing my first batch file.

2 1

 

With this command notepad will prompt that such a file Is not exist whether we would like to create a new file. You can select the Yes option.  And in the file I just type the below commands.

3

 

That’s nothing more than just changing the title text of the cmd prompt. But I need to pay your attention to two important commands. First one is “echo off” that means when batch file is executed it will not display each and every command in the batch file. You can see the difference by setting the echo command turn on by changing the command into “echo on”. And what is @ sign mean ? this also can be checked by yourself. @ sign mean this echo off command applies to itself as well. And the next important thing is “::” symbol. That means the line is a comment. So it will not executed. Instead you can use the word “Rem” as well for comments.

Okay now how do we run this batch file? you can just double click on the file. Or you can type in the command prompt batch file name and enter. Remember here you can use the name of the file rather than with the file extension. It’s no difference if you prefer you can use the extension as well.

4

See you can use just the name of the batch file without the extension. And note that command prompt title text has been changed.

Okay now since you are familiar with batch files. Let’s move on to writing a more complex batch file. I mentioned you earlier that I need to open opera, chrome, xamp and a text editor or IDE with the location of my current working directory.

Open up a new batch file with the command. “notepad webdev.bat”. May be in your case you may need to open up different set of applications. But the process is same. So you can do the same for your set of applications.

5

Write these commands in the newly opened batch file.

@echo on

:: opening up web development tool set

Rem opening up opera

“C:\Program Files\Opera\launcher.exe”

Rem opening up chrome

“C:\Program Files (x86)\Google\Chrome\Application\chrome.exe”

Rem opening the xampp control panel

“C:\xampp\xampp-control.exe”

6

In case if you are wonder from where you can find these executable paths like “C:\Program Files (x86)\Google\Chrome\Application\chrome.exe” you can just right click on a shortcut of the application and view it’s properties, then you can find the location in the target property. Okay now all set good to go. So execute the batch file like before. All good? Are you sure about that ? no !!!  Actually in the code I explicitly set the echo to turn on. So that we can see where we went wrong. Some applications will hold the command prompt so that the applications comes next will not execute until you close these application. For an example here google chrome will run on command line. It won’t run separately so the xampp will not open. How we can address this issue? We can use the “start” command to start in a separate process. In simple words in a new command prompt. But opening several command prompts all over the place wouldn’t be so nice. So it’s better to execute them separate but not without opening too many command windows. Let’s see how we can do that. If you need to get an in-depth idea how start command works, just type “start /?” on the command prompt. Okay lets modify our code like below.

@echo off

:: opening up web development tool set

Rem opening up opera

start “” /B “C:\Program Files\Opera\launcher.exe”

Rem opening up chrome

start “” /B “C:\Program Files (x86)\Google\Chrome\Application\chrome.exe”

Rem opening the xampp control panel

start “” /B “C:\xampp\xampp-control.exe”

7

Works like a charm right ?  Now what is left is we should store our work location. For this we can use variables. For me the location will be “C:\xampp\htdocs\MyProjects”. So I will store that location in a variable. Later I can take some command line arguments and switch between folders. But I will discuss that in a later article since this is already a bit lengthy article. Okay, modify your code like below.

@echo off

:: opening up web development tool set

Rem opening up opera

start “” /B “C:\Program Files\Opera\launcher.exe”

Rem opening up chrome

start “” /B “C:\Program Files (x86)\Google\Chrome\Application\chrome.exe”

Rem opening the xampp control panel

start “” /B “C:\xampp\xampp-control.exe”

set work=”C:\xampp\htdocs\MyProjects”

cd %work%

code .

What is happening here is we store our working location in work variable by this “set” command. And then we change the directory to the working directory. And then since my IDE is VS code I use the command “code .” to open the editor. In your case this might be a little different based on your editor. Hope you would be able to figure it out. But if you find difficulties be my guest to comment.

We can do one more thing. Hope you have notice that to execute batch file we have to go to the location where batch files are stored. But this will not be the case if we add the path of the batch file location to our path variable in windows. With that whenever we open a Command prompt we can execute our custom commands. So let’s go ahead and do that as well.

Hope you know how to set the environment variable. In case if you are wondering. Just right click on my computer icon go to properties then advance system settings then environment variables. Then select the path variable under variable for you. And then edit. Now you can put a semicolon and add the path to your batch files. And then okay. Now close the command prompt and reopen again. Now you can just type “webdev” which is the file we have just created. And the command will work just like a charm !!!

Now I don’t have to make several clicks when I am going to start my web developing projects. All I have to do is just open up the command prompt and issue my custom command. Hope you enjoy it. And please explore it yourself more. There is a good article in the tutorials point. I recommend you take a look at that as well.

References : https://www.tutorialspoint.com/batch_script/index.htm

Comments

comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here