3

When I want to update a web project of mine, I have to do the following tasks all the time:

  • Open cmd.exe.
  • cd into working directory of project.
  • Run localhost command in cmd.exe (~ jekyll s).
  • Launch a new window of Chrome (default browser) and enter the link http://localhost:4000/

How can I make my computer do all this for me on one double click on a file? By scripting or batch files perhaps?

2 Answers 2

3

You need to write a batch file in Windows (in case of Linux they are called shell script). It is very easy to write once you know what commands to use (In your case you already know the commands). In a batch file you need to write down the commands in the order in which you want them to execute. To create a batch file, open a Notepad and write down those commands in order and save the file with .bat extension. Next you just need to double click that .bat file and it will do all your task. Just giving you an example of how your batch file should look like.

cd /your/working/directory/path
jekyll s
start chrome "http://localhost:4000/"

Basic input/output operation in batch file

Taking input from the user: [Command used set]

Syntax: set /p variable_name=[Prompt String]

Example: set /p loc="Enter the directory path : "

This will prompt for an input from the user and will assign it to the variable loc.

Displaying message/output on the screen: [Command used echo]

Syntax: echo <your_message_here>

Example: echo Hello World

Will display "Hello World" on the screen. To print the content of a variable using echo use echo %variable_name%. Say you want to display the content of the variable my_name. Then the command will be echo Hello %my_name%.

Making your script more interactive using these commands.

echo off
::The above command hides the commands that are executed and displays only the output of the commands
set /p loc="Enter your project directory location : "
cd /d %loc%
echo Directory changed...
jekyll s
echo Launching browser...
start chrome "http://localhost:4000/"
echo End of script...
pause

:: is used for commenting. To know more about a particular command use /? after the command name to display the manual for that command which contains what the command does and all the options that are available for it. For example to know more about the cd command enter cd /? in the prompt.

Here is a nice tutorial for beginners.

3
  • 1
    Thank you for your answer, I just had the time to look at it and I made my batch file. Do you have any suggestions for a beginners guide to windows scripting? Would be awesome if you included that in your answer as well:)
    – Varaquilex
    Apr 27, 2015 at 14:17
  • @Varaquilex welcome. :) Certainly, I will add some more info about batch files to the answer if that would help you. I will update the answer whenever I get some time but if you need something specific feel free to mention it and I will update it accordingly.
    – Ayan
    Apr 27, 2015 at 14:31
  • Just the standard basics would do for now, thanks^^
    – Varaquilex
    Apr 27, 2015 at 14:33
0

You would create what is called a batch script. There are many tutorials around online for getting started with batch scripting, such as this one, I would recommend you have a read and test some of the basics to get a feel for it.

As for your specific case, open Notepad (Not a Word doc), and type in the commands that you would normally type into Command Prompt, complete with newlines. So something like:

cd C:/yourdirectory
somecommand /parameter
etc...

Now save the file, choose the 'All Files' option, and save it as scriptname.bat, NOT .txt

Now, if you run the file by double-clicking, it will open a command window and run your commands.

You can open browser windows by using:

start "http://localhost:4000/"

Which will open in your default browser.


Some commands you may find useful whilst building the script:

  • @echo OFF - put this at the top of your batch script and it will stop the window from listing all the commands it's running.
  • echo Use this to display messages, like `echo The copy operation is complete
  • pause - Use this to pause execution of the batch commands until you press a key. It will automatically ask you to "Press any Key to continue"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .