How to Write a Batch File

preview_player
Показать описание
How to Write a Batch File
steps
1-Open Notepad. Notepad allows you to create code as a text file and then save it when you're done as a batch file. You can open Notepad by opening Start, typing in Notepad, and clicking the blue Notepad app icon at the top of the menu.
2-Learn some basic batch commands. Batch files run a series of DOS commands, so the commands that you can use are similar to DOS commands. Some of the more important ones include:
ECHO - Displays text on the screen
@ECHO OFF - Hides the text that is normally output
START - Run a file with its default application
REM - Inserts a comment line in the program
MKDIR/RMDIR - Create and remove directories
DEL - Deletes a file or files
COPY - Copy a file or files
XCOPY - Allows you to copy files with extra options
FOR/IN/DO - This command lets you specify files.
TITLE- Edit the title of the window.
3-Write a program to create a directory. One of the easiest ways to learn how to create batch files is to focus on doing basic tasks first. For example, you can use a batch file to quickly create multiple directories
MKDIR c:\example1
MKDIR c:\example2
4-Write the code to make a basic backup program. Batch files are great for running multiple commands, especially if you configure it to be able to run multiple times. With the XCOPY command, you can make a batch file that copies files from select folders to a backup folder, only overwriting files that have been updated since the last copy:
@ECHO OFF
XCOPY c:\original c:\backupfolder /m /e /y
5-Write a more advanced backup program. While simply copying the files from one folder to another is nice, what if you want to do a little sorting at the same time? That's where the FOR/IN/DO command comes in. You can use that command to tell a file where to go depending on the extension:
@ECHO OFF
cd c:\source
REM This is the location of the files that you want to sort
FOR %%f IN (*.doc *.txt) DO XCOPY c:\source\"%%f" c:\text /m /y
REM This moves any files with a .doc or
REM .txt extension from c:\source to c:\text
REM %%f is a variable
FOR %%f IN (*.jpg *.png *.bmp) DO XCOPY C:\source\"%%f" c:\images /m /y
REM This moves any files with a .jpg, .png,
REM or .bmp extension from c:\source to c:\images
6-Experiment with different batch commands. If you want inspiration, you can check out the sample batch text at the end of this article.
#batchfile #windows #batch #backup #batchfile #batchfiles #batchfilescript #batchfilesareawesome #batchfileprogramming #batchfileprovisioning #howtowrite
Рекомендации по теме
Комментарии
Автор

With batch files for Windows we can use internal and external Windows commands and with batch files for DOS we can use internal and external DOS commands.

There are some differences between DOS commands and Windows commands. Windows have a lot more commands and some of these commands have the same name like the DOS commands, but have different switches to attach.

I like to make very special batch files for DOS and the DosBox emulator. DOS is running in the 16 bit Real Address Mode of the CPU and starts in the text mode of the display device and Windows is running in the 32 bit or 64 bit Protected Mode using the graphic mode.

In the Protected Mode it is not allowed to access the hardware components of the computer directly with an application. But in DOS it is allowed. That means we can access the hardware components directly with an application without stop running with a message of a protection violation.

And batch files are suitable to contain a routine made with x86 assembler mnemonics to create a brand new executable file with a little help from Debug an external DOS command. So i put all instructions to create&run a new executable file (for DOS only) from the routine inside the batch file.

I made two videos on my youtube channel (with no sound) that shows how it works on DosBox and with download links. Feel free to modify, share and using it for your own projects and have fun with it. If you like it please write a comment on my channel. Thanks.

maxmuster