After wrote several MSDOS commands articles, then I want to write about creating batch file this time. Batch file is collection of multiple commands in a single file to execute them in one shot. Is it be helpful ? Sure, if you have some routine commands or commands collection that has to execute them in sequence order so you'll need to create batch file.

The most popular of batch file is autoexec.bat. In autoexec.bat the commands that will be loaded in startup will put in there, you startup variable also has set in autoexec.bat file. In MSDOS age or Windows 95, autoexec.bat has importance task to loads all startup item an creating some variable like PATH variable and the other else but in Windows XP 'the jobs' has been took by Windows Variable Environment setting.
By default, every batch file that you create, you must give it .bat or .cmd extention. You can create it using text editor such as Notepad or if you're in MSDOS mode, use copy con command to create it :
> copy con batch_name_file.bat
type your commands here command_1 command_2
(if you have complete it type Ctrl-Z key)
For example, if you want to create batch file that will browse Windows folder content, type them like this :
> copy con browse_windows.bat @echo off REM change current work path to Windows folder ECHO Change current folder to Windows folder cd\windows REM Browse them all ECHO Execute dir command dir | more ECHO Finished .....
(press Ctrl-Z)
(you can create it using Notepad too, so you can correct it if you make some mistakes and if you're ensuring all is right, save it)

OK, we'll analize it now, @echo off is command to hide the ECHO text in result for every ECHO command that executed; REM is command to make a comment (skip by interpreter), ECHO is command to display a message in result. So you can look it that to make batch files you don't need to be a programmer because the sintax is very simple.

Now, we will learn to create batch file using parameters in it. If you will include some parameters in batch file just use %1, %2, .... variable.
For example, if you want browse 2 different folders directly, create batch file like this one :
>copy con look2.bat @echo off ECHO First folder dir %1 ECHO Second folder dir %2 ECHO Finished.....
(Ctrl-Z)
and to use it, type look2 c:\windows c:\data, and it will display Windows folder first and data folder in second order.

Another commands are :
1. PAUSE, to suspend processing of the batch file, usually this will used if you asked user to insert CD-ROM/Floppy/USB, like this :
@echo off ECHO Insert source device, Floppy or CD ROM or USB PAUSE copy c:\data %1 ECHO Completed....
From the example above, you can learn the using of PAUSE command, before copy c:\data %1 will executed, the user asked to source device first and when the user has insert the source device then press ENTER key to continue next commands.

2. CALL, to calls another batch file, to use it, just give command CALL my_another_batch_file.BAT.

3. GOTO, to jump to certain marked section, like this one :
@echo off ECHO CHKDSK will executed, press any key to continue.... CHKDSK %1 /f IF NOT errorlevel 1 GOTO END ECHO An error accurred..... :END
From example above, if CHKDSK command runing propery and don't get any errors, bath file jump to :END section.

4. IF, batch command file for conditional processing, the example avaiable at point number 3, IF CHKDSK get no error, it will jumped END section and its will controled by IF command.

5. CHOICE, command to suspend processing of batch file and prompt to user to make choice for some options given. To use it just type this in your batch file CHOICE /c YN /t 10 /d N /m "Press Y for Yes or N for No" and if executed, the result will display this message Press Y for Yes or N for No [Y/N], unlike PAUSE command that just ask the user to press any key to continue the process of the batch file.

6. SETLOCAL and ENDLOCAL
SETLOCAL to sets temporary added environment variables and ENDLOCAL to ends localization of variable that set by SETLOCAL, like this :
@echo off ECHO Set temporary variable SETLOCAL PATH=%PATH&; c:\old_PHP; c:\old_data CALL old_script.bat ENDLOCAL
From above we can look that PATH variable will added with c:\old_PHP and c:\old_data and when ENDLOCAL command has executed, those added path will removed.

7. FOR, repeats the processing of a command for each file in a group of files, look below for example :
FOR %%A in (C:\DATA\*.xls) do (ECHO %%A)
Variable %%A will change to all .xls files in C:\DATA folder and for each .xls file will displayed as a result until all .xls in C:\DATA have displayed.

8. SHIFT, To change the position of batch parameters in a batch file from %0 through %9 and will copy current parameter to previous paramter, for example if you use parameters from %0 - %4, %4 will copied to %3, %3 to %2 and so on.
Fiuhhhh... I am very sleepy now, hope this article can help you... then I want go to bed now.

0 comments