How to Protect a BAT File with a Password

Table of contents:

How to Protect a BAT File with a Password
How to Protect a BAT File with a Password
Anonim

Protecting access to a BAT file with a password is not a very complex operation, but without the proper instructions it could become so. If you have the time to learn how to password protect your BAT files, you can implement a security system in a few simple steps.

Steps

Part 1 of 2: Creating the Code

Add a Password to a. Bat File Step 1
Add a Password to a. Bat File Step 1

Step 1. Start the "Notepad" program

Access the Windows "Start" menu, click on "All Programs", then choose the "Accessories" option. Inside the "Accessories" section of the "Start" menu you will find the "Notepad" program icon. Alternatively, type the keywords "notepad" into the "Start" menu and press the "Enter" key to launch the corresponding application.

Add a Password to a. Bat File Step 2
Add a Password to a. Bat File Step 2

Step 2. Start writing the security code with the "@ echo off" command

This is the starting line of code for the script. The script that you are going to insert at the beginning of the BAT file will have the purpose of allowing the execution of the rest of the code that makes up your file. After entering the indicated line of code you can continue. Now copy the source code shown below and paste it right after the "@ echo off" line.

  • :TO

  • echo Enter the password to start the program.
  • set / p "pass =>"
  • if NOT% pass% == [enter_the_password] goto: FAIL

Add a Password to a. Bat File Step 3
Add a Password to a. Bat File Step 3

Step 3. Add the finishing touches to complete your batch file

At this point, you need to insert the following code at the end of the script you created or are going to create:

  • : FAIL

  • echo Wrong password.

  • If you want, you can add more code or other commands. If you want to delay the execution of the first line of code and the second line of your program, enter the command "ping localhost [number]" in the middle. In this way, the program will wait the time indicated by the parameter "[number]" before executing the next command. Also insert it between the second and third lines of code. If you want the program to run slower to give users time to follow the instructions they will be given, increase the value of the "[number]" parameter. The wait between the execution of each program command is determined by the time it will take for the operating system to execute the "ping localhost" command. If you want the program to print the word "Hello" and after five seconds the sentence "How are you?", You will have to insert the command "ping localhost 5" between the two lines of code.
  • goto: end

  • : end

Part 2 of 2: Completing the Code

Add a Password to a. Bat File Step 4
Add a Password to a. Bat File Step 4

Step 1. Change the parameter "[enter_la_password]" to the password you have chosen to use

You can enter the password you want and it can be as long as you want. Remember to put it in quotes.

Add a Password to a. Bat File Step 5
Add a Password to a. Bat File Step 5

Step 2. Save the file by adding the ".bat" extension at the end of the name

The default extension for text files is ".txt", so you will need to change it manually. If you have already saved the file, go to the "File" menu, choose the "Save As" option and change the existing extension to ".bat". If the ".txt" extension is not visible, you may need to select the "Show extension" check button.

Add a Password to a. Bat File Step 6
Add a Password to a. Bat File Step 6

Step 3. Go to the "Security and Maintenance" or "Performance and Maintenance" tab of the Windows "Control Panel", then select the "Scheduled Tasks" link and navigate to the folder where you saved your batch file

By taking advantage of the features of Windows, you can make the BAT file run automatically at a certain time, for example when a user logs in, when a certain file or folder is opened, or when any other event occurs.

Advice

  • If you don't know how to create a BAT file correctly, read this article to find out how to do it and to review some examples.
  • The code shown in this article is very simple. Anyone with a minimum of knowledge about the structure of a BAT file will still be able to access the source code.

Recommended: