If your primary hardware tool for your job is a computer, chances are there are things that you do repetitively every day, probably several times throughout the day.
Maybe you find yourself clicking through several folders to get to a particular file or using the start menu to launch a particular program. Perhaps you run commands with several arguments repetitively. Although this post is targeted for programmers, automating tasks is something that is available to anyone. Rather I should say automating or shortening commands.
This post targets automating tasks in Windows, but automating tasks in Mac or Linux is just as possible and sometimes even more so because the BASH is more capable than the DOS shell. If you are interested in automating tasks in Mac or Linux drop it in the comments and I may just provide a how to post on that.
Let me start with a simple example. Let’s say there is a folder buried deep in your documents folder structure. To get to it you click through various folders to get to the files you need. Let’s say for Example let’s say it’s in ~Documents/accounts/receivable/customers/customer1
That’s a lot of clicking through to get to that folder. Perhaps you’ve added that folder to the Quick Access folder. While that’s a pretty good solution, let me show you another way to get there.
What if I told you that an alternative to getting to the customer folder you could just type the command cust1. Well… if you try this on your machine it won’t quite work. That’s because you have to set up your machine to do that.
So let’s take a look at the steps to do just that.
The first thing you must do is create a batch file to store the command in. Batch files are scripts or files with DOS commands in them. DOS commands are very simple and yet have very useful capabilities. You can type DOS commands at the command prompt or you can store a series of them in a batch file and just call the batch file from the command line instead. If you want to understand the basics of DOS commands I recommend looking at this Top Ten list of DOS commands https://www.computerhope.com/overview.htm
This list of most DOS commands https://www.computerhope.com/overview.htm should give you a good idea of everything you can do with DOS commands.
But before you create the batch file you must create a folder to store it in.
So let’s start by creating a folder. I created the folder fzjscripts to store all my automation scripts in the C drive like this.
In that folder, I’ve added a file named cust1.bat. The bat extension on the file lets Windows know that’s an executable batch file. In that file, I store the following DOS commands
cd C:\Windows start ..\Users\ferna\Documents\accounts \receivable\customers\customer1
There are two lines above but I separated them into three for readability. The first line is the cd command. That command is for changing the directory to C:\Windows. For some reason, the Windows Explorer (start Windows Explorer) only works from inside C:\Windows. So.. then I call “start” with my folder path in a relative manner. Since my customer1 folder is not a subpath of C:\Windows I have to tell it to go back or up one folder (..) and then travel down all the way to customer 1 to open it. That’s like saying C:\Users\… where … is the rest of the path.
So now my fzjscripts folder hast the batch file cust1.bat in it. I can run that batch file if I navigate to the fzjscripts folder in the command line as shown below:
If I am anywhere else in the command line, like my user directory (shown below), I can’t run the command. But… there is a way to run it from there and anywhere. I can do that by telling Windows about my new scripts folder and that I would like Windows to look there for executables to run.
So… to teach windows to look in C:\fzjscripts for executables, I have to set the path environment variable. The path environment variable looks something like this: C:\somefolder\sub;C:\SomeotherFolder\bin.
It is just one big long line of text. The text is composed of windows directories separated by semicolons. Windows looks for any executables you run in the command line, in the folders in the path environment variable. It looks from left to right.
NOTE: With this in mind, it is important that you name your scripts something unique. That means something like word.exe would probably be a bad name since it might open your script and not be able to find word anymore.
Well… the path variable was a big long line of text, and it probably still is and you could clearly see that prior to Windows 10. Windows 10 has improved on it and now it looks like this.
To get to the path environment follow these steps.
Click on the Windows start button and type “This PC”. When “This PC” shows up, click on it.
After, it opens up Windows Explorer, right click on this PC
When you right-click on This PC select “Properties” from the context menu and click it.
Click on Advanced System Settings. Click on Environment Variables. Select the path environment variable for the System variables window. You may have to scroll down to find it.
Once the window for the path environment variable opens up. Click New to add your scripts folder path. In my case, that’s C:\fzjscripts as shown below.
Now that you have everything in place you should be able to run any command you have stored there. Each command has a batch file with a bat extension. For example cust1.bat but you only have to type cust1 from anywhere on your command line for it to work.
This other command, fzcpp, allows me to compile C++ programs from anywhere. As you can see, the command is quite long but I just type fzcpp my.exe and my cpp files compile into the executable my.exe(my.exe can be anything else, example only). It beats typing the full command every time. Also, notice the %1. That %1 is an argument that I pass into my fzcpp command. So.. when I run it I type fzccp my.exe and the program replaces %1 with my.exe.
REM fzcpp.bat g++ -std=c++11 *.cpp -o %1
This command I stored in the file steclipse.bat, starts eclipse. I have eclipse installed using the unzip option instead of the traditional way. So this command makes it really easy to open eclipse from the command line. It beats navigating the folder and clicking on the executable file.
This command, openeclipse, is for opening up the folder in my eclipse workspace folder. Many times I want to copy the files over and send them or for whatever other reason I want to navigate using windows explorer. This command openeclipse stored in the file openeclipse.bat as shown below is quite handy for that.
cd C:\Windows start ..\Users\ferna\eclipse-workspace\%1
Finally, I have one more example. This is my fzjhelp command and all it simply does is list all my other commands to help me remember what I have stored in fzscripts folder. Maybe someday I will put in a way to also provide man help files for these commands or perhaps you are up to the challenge. fzhelp is very simple. It just changes directory to the fzjscripts folder and does a listing of all files there by executing the DOS command dir . The “.” is a reference to the current directory. In this case the “.” refers to C:\fzjscripts.
cd C:\fzjscripts\ dir .
What will you automate with this new knowledge?