Customize Windows Command Prompt Instructions with Batch Script

Ichwan Sholihin
5 min readJust now

--

Photo by Markus Spiske on Unsplash

An operating system is not only composed of a collection of software but also includes various commands that facilitate user interaction, including on the Windows operating system. As one of the most widely used operating systems, Windows offers users the convenience of customizing specific processes through one of its tools, the Command Prompt. The commands in the Windows Command Prompt primarily consist of various .bat files, commonly referred to as batch scripts.

These batch scripts store a variety of commands that can be executed in the Command Prompt and customized according to the user’s needs, such as creating, modifying, deleting, renaming files, and more. To customize commands in the Command Prompt, a batch script is required. In this article, we will discuss processing and managing CSV files using batch scripts on Windows. Batch scripts allow us to automate repetitive tasks, saving time and reducing the risk of human error.

Read/Write File

The echo command is commonly used in PHP to print values, and the same concept exists in batch scripts. However, in batch scripts, the echo command is not only used for printing values but also for customizing file contents using the > or >> operators. The > operator is used to redirect output to a file. If the file already exists, its content will be overwritten with the new output.

The >> operator is used to redirect output to a file as well, but if the file already exists, the new output will be appended to the end of the file without deleting its current content.

To clear the contents of a file, use the NUL command.

  • Variable

We don’t need to specify a data type when storing a value in a variable. In batch scripts, simply use the keyword set as the definition to create a variable. For example: set name=Steve. However, there are additional characteristics when defining a variable in a batch script, such as using /a to define arithmetic values (set /a count=0) and using quotation marks (set "name=Steve") if the variable needs to be explicitly treated as a string.

To access variables in a batch script, use the expression %name% or !count!. The use of ! is helpful when the variable's value changes during iterations in a loop or when accessing a variable's updated value within the same command block.

  • For Loop

Batch scripts can also perform looping processes like for, while, and do while. However, there are specific loop types provided in batch scripts. Here are the types of for loops in batch scripts:

Basic loop: Used to iterate through a list of items separated by spaces or other characters.

basic loop for

Loop numbers: Used to repeat operations within a specified range of numbers, with a customizable increment.

loop range numbers

Loop file: Used to process each line in a file or the output of another command. This example reads the content of a file line by line.

loop files

Loop directory and subdirectory: Used to iterate through files in a directory and its subdirectories.

loop directory and subdirectory
  • Loop directory: Used to iterate through directories within a specific directory.
loop directory
  • Decision Making

Decision-making is crucial for providing a specific condition within a program. In batch scripts, decision-making is not much different from other programming languages in terms of the operators used, but it is simpler. Here is an example of decision-making in a batch script using the IF-ELSE approach:

Besides IF-ELSE, there are other operators used for comparing values, such as:

  • Function CALL and GOTO

The CALL and GOTO commands both execute instructions, but the difference lies in their behavior. GOTO (goes to the label) means the script will directly execute instructions based on the label, whereas CALL invokes a subroutine (a function within the script) or another batch script and, after completion, returns to the line after the call.

Below is an example of using the GOTO command:

In the example above, after the first line is executed, the line labeled SECOND LINE, which contains the ECHO execute second line command, will be executed while skipping the ECHO execute third line command.

Here is an example of using the CALL command:

In the example above, after executing the first line, the program will first execute the ECHO Inside subroutine command within the subroutine, then end the script, and finally execute the ECHO After Call command.

Additionally, there is the @echo off command, which is used to disable the output of commands in the script, ensuring that the commands being executed are not displayed on the user's screen.

Follow me on LinkedIn, Github, Dev.to, and Instagram.

--

--

Ichwan Sholihin
Ichwan Sholihin

Written by Ichwan Sholihin

Software Developer at AXA Insurance Indonesia

No responses yet