Windows 11

5 Best Ways to Create a Batch file on Windows 11

Mastering Batch File Creation on Windows 11: The Top 5 Methods

19views

Users of Windows 11 may need to create a batch file because of the need to perform repetitive tasks. A batch file is a text file with a “.bat” extension. It contains a series of commands that the system can execute to complete different tasks via the Command Prompt.

A batch file can automate tasks, save time, and simplify processes. While PowerShell allows for more comprehensive scripts, batch files crafted for Command Prompt remain beneficial and easier to use for a variety of tasks.

1. Create a basic batch file

Creating a basic batch file is relatively simple. You may use any text editor like Notepad to create the file. Use the following steps to achieve this:

  1. Hold down the Windows and S keys simultaneously, type notepad in the search field, and hit Enter.
    Create a Batch file on Windows 11
  2. When Notepad opens, type @echo off and move to the next line by hitting Enter.
    Create a Batch file on Windows 11
  3. The command above disables the entire script display, implying you will not see the script while running.
  4. Then, type title followed by the title of your batch file, and hit Enter.
    Create a Batch file on Windows 11
  5. Afterward, type echo followed by the text you want to be viewable on the output display, and hit Enter.
    Create a Batch file on Windows 11
  6. Then, type pause and hit Enter. This command depicts the end of the batch file, and also keeps the Command Prompt window open that would normally close upon successful execution.
    Create a Batch file on Windows 11
  7. Click the File option at the top-left corner of the Notepad window and select the Save as option.
    Create a Batch file on Windows 11
  8. Lastly, name the batch file correctly with a BAT extension.
    i.e. .bat e.g. article.bat.
  9. When you want to run the batch file, locate the batch file and double-click on it. A Command Prompt window will launch, displaying the batch file output.
    Create a Batch file on Windows 11

2. Access network drives and folders

You can also create a batch file to access network drives and folders. Here are the steps on how to create a batch file to access network drives and folders on Windows 11:

  1. Hold down the Windows and S keys simultaneously, type notepad in the search field, and hit Enter.
    Create a Batch file on Windows 11
  2. When the Notepad opens, enter the following code and replace the letter F with your driver letter:
    Echo Create new F: drive mapping
  3. Then, enter the following command and replace the Network path with the path you intend to map to:
    @net use F: \Network path /persistent: yes
  4. You may repeat the process if you intend to add multiple drives.
  5. Then, enter the following lines:
    : exit
    @pause

  6. Lastly, click on File, select Save as, correctly name the file with a .bat extension, select All files for Save as type, and then click Save.
    Create a Batch file on Windows 11
  7. When you want to run the batch file, locate it and double-click on it. A Command Prompt window will launch, displaying the batch file output.

3. Add user inputs

Adding user input to a batch file lets you customize the actions taken by the batch file. For instance, you could create a batch file to prompt a user for the name of a file to copy and then copy that file to a new location. Follow these steps to do that:

  1. Hold down the Windows and S keys, type notepad in the search field, and hit Enter.
    Create a Batch file on Windows 11
  2. Enter the following script when the Notepad opens:
    @echo off
    : start
    set /p input = Enter the Name:
    echo %input% We are thrilled to welcome you to the event!
    pause
    go to start

  3. You may change the Enter the Name instruction and the customized message to match your task and preferences.
  4. Lastly, click on File, select Save as, correctly name the file with a .bat extension, select All files for Save as type, and then click Save.
    Create a Batch file on Windows 11
  5. When you want to run the batch file, locate it and double-click on it. A Command Prompt window will launch, displaying the batch file output.

4. Automate repetitive tasks

Creating a batch file to automate repetitive tasks can save time and effort. It can help you save time, reduce errors, increase productivity, and improve consistency. Here is how to create the batch file:

Here, we will show you how we automated opening frequently used apps on our computer.

  1. Hold down the Windows and S keys, type notepad in the search field and, hit Enter.
    Create a Batch file on Windows 11
  2. The script below opens Google Chrome, Word, Slack, and ShareX:
    @echo off
    cd "C:\Program Files\Google\Chrome\Application\chrome.exe"
    start chrome.exe
    start – "C:\Program Files\Microsoft Office\root\Office16\WINWORD.EXE"
    cd "C:\Program Files\ShareX\ShareX.exe"
    start ShareX.exe
    Exit

  3. Replace the app path with the path you intend to open.
  4. Finally, click on File, select Save as, correctly name the file with a .bat extension, select All files for Save as type, and then click Save.
    Create a Batch file on Windows 11

5. Edit lines of code with the command window

Creating a batch file on Windows 11 to edit lines of code can help save time, reduce errors, and increase consistency. It can also make repeating tasks easier and learn more about the command line. The script in this solution can help you replace lines in a simple code. Follow the steps below:

  1. Hold down the Windows and S keys simultaneously, type notepad in the search field and hit Enter.
    Create a Batch file on Windows 11
  2. Assuming a file named code.txt has oldtext mentioned several times, and you intend to replace it with newtext, use the following script:
    @echo off

    setlocal enabledelayedexpansion

    set "inputFile=code.txt"
    set "outputFile=code_modified.txt"
    set "oldText=oldtext" set "newText=newtext"

    if not exist "%inputFile%" (
    echo Input file not found.
    exit /b
    )

    (
    for /f "usebackq delims=" %%a in ("%inputFile%") do (
    set "line=%%a"
    set "line=!line:%oldText%=%newText%!"
    echo !line!
    )
    ) > "%outputFile%"

    echo Lines with '%oldText%' replaced by '%newText%' have been written to '%outputFile%'.

    endlocal
  3. Replace code.txt with your file name and oldtext and newtext with the lines you intend to use.
  4. Lastly, click on File, select Save as, correctly name the file with a .bat extension, select All files for Save as type, and then click Save.
    Create a Batch file on Windows 11
  5. When you want to run the batch file, locate it and double-click on it. A Command Prompt window will launch, displaying the batch file output.

In conclusion, you can create a batch file to enhance your productivity and efficiency on Windows 11. With various methods available, you can choose the approach that best suits your needs and technical expertise.

Remember, batch files can automate repetitive tasks, execute multiple commands simultaneously, and simplify complex operations. Whether you’re a beginner or a pro, the step-by-step instructions in this article will help you to create batch files and unlock the full potential of your Windows 11 system.

Leave a Response

Richard Omachona
Richard is a techie in providing fixes and solutions for computer issues of various kinds. Among his contemporaries, he is a preferred choice. His experiences are vast in Windows operating systems, and several other skills in programming such as Python, Web Frontend designing implementing at industry standards, best practices in HTML, CSS and JavaScript. and basics in Web Backend. He also loves traveling, gaming and music.