HomeinetWrite your first .bat file

Write your first .bat file

A batch file or .bat contains a series of DOS commands, and is usually written to automate frequently performed tasks. Instead of typing the same commands over and over again, you can simply double-click the batch file. Writing a batch file is a lot easier than you might think. windows tools bat bat bat

The hard part is ensuring that everything is done in the right order. A well-written script can save you a lot of time in the long run, especially if you use it frequently for repetitive tasks.

How;

Open Notepad. Notepad allows you to write code as a text file and then save it as a batch file.
You can open Notepad by clicking Start/Programs/Accessories/Notepad, or by right-clicking on the desktop New/Text File.

Basic commands

Batch files execute a series of DOS commands, and the commands you can use are similar to DOS commands. Some of the most important are:

ECHO – displays text on the screen
ECHO OFF – Hides text
START – Executes a file
REM – To write a line of explanation to your program
MKDIR / RMDIR – Creates and removes directories
DEL – Deletes a file or files
COPY – Copy a file or files
XCOPY – Allows you to copy files with additional options
FOR/IN/DO – This command allows you to specify files.
TITLE – Edit the title

One of the easiest ways to learn how to write batch files is to focus on the basics first. For example, you can use a batch file to quickly create a folder:

MKDIR C:\example

Save the file. Once you have written the above code, you can save the file as txt. Then delete the .txt extension and name the text file with the extension .bat or .cmd. If you run the file now, it will create a folder in C:\ named example.
Make sure the encoding is ANSI before saving the file.

This way you can write a basic backup program. Batch files can execute a lot of commands, but today we'll focus on the basics.

With the XCOPY command, you can create a batch file that copies files from selected folders to a backup folder, replacing only files that have been updated since the last backup:

Let's say you want to copy the test folder from C:\ into the secnews.gr folder. The command you would write in notepad would be like this:

ECHO OFF XCOPY c:\test c:\secnews.gr /m /e /y

The command as mentioned above copies the files from the “test” folder to the “secnews.gr” folder. You can replace the folder names we use with the paths to the folders you want. The / m specifies that only updated files will be copied, the / e specifies that all folders inside the test folder will be copied and the / Y will give you a confirmation message each time a file is replaced.

Save the file. Once you have written the above code, you can save the file as txt. Then delete the .txt extension and name the text file with the extension .bat or .cmd. You can try running it.

We are giving you some ready-made files that you may find useful.

Open Microsoft Word

@echo off start /d "C:\Program Files\Microsoft Word" WINWORD.EXE

(Change the path to the folder where your Office is installed)

Make a list of the programs you have in Program Files

@echo off dir "C:\Program Files" > C:\list_of_program_files.txt
📧
Subscribe to the SecNews Newsletter

The most important Security & Technology news in your Inbox.

SecNews
SecNewshttps://www.secnews.gr
In a world without fences and walls, who needs Gates and Windows

SEARCH

FOLLOW US

📧
Newsletter SecNews
The most important Security & Technology news in your inbox.

LIVE NEWS