As you've seen, batch files are essentially simple programs, using the built in command prompt commands as a programming 'library'. In this post, we will be looking into the Anatomy of Batch Programming.
Gives the variable '%%F' the value of every file ending in .txt in the current directory, then passes that variable to the DEL command. This means that every file with the .txt extension in the current directory will be deleted.
In addition to the standard command prompt commands that can be used in batch files, there are a few additional extras like the @ECHO off' command we used in the last batch file we created. These can be used to modify how a batch file works. Let's run through some of them with examples of how they work:
Additional Batch file commands
Call
Can be used to call another batch file from within the current one
call c:\batchfile2.bat
Echo
Used to display information and commands on the screen or prevent them from being displayed.
Echo on
causes all commands in the batch file to be displayed onscreen. This is the default setting.
@Echo off
causes no commands to be displayed. The batch file will run silently unless you use the echo command specifically as below.
echo what you want on the screen Causes whatever text comes after the echo command on the same line to be printed on the screen.
WHAT IS "For" STATEMENT IN BATCH PROGRAMMING?
for statement is used to select a specific set of files and run a command on each of them for (variable) in (set of files) do (command)
In another example, the batch file line;
for %%F in (*.txt) do del "%%F"
HOW TO USE "Goto" IN BATCH PROGRAMMING
Moves to different points within a batch file. The destination point must be indicated with a colon. For example…
goto end
:end
echo this is the end, beautiful friend… the end
BATCH PROGRAMMING "If" STATEMENT
Performs a command depending on a condition. IF must include an ELSE statement which says what happens if the condition is not met. For example:
if exist c:\relatewithme.txt (copy c:\relatewithme.txt d:\relatewithme) else echo relatewithme.txt does not exist
In this example, if the 'relatewithme.txt' file exists, it will be copied to d:\relatewithme. If it does not, a message will be shown indicating this.
HOW TO USE "REM" IN BATCH PROGRAMMING
Rem is used to create comments, or ignored sections in batch files. The computer will ignore any line that begins with REM, so you can use this command to add notations explaining what your file does.
For example:
@echo off
rem this batch file is
rem designed to format
rem floppy disks in the a: drive
format a:
SHARE THE POST USING THE SHARE BUTTONS BELOW

No comments: