A few months ago, I wrote a bit about using the FOR command to delete files that had been accidentally copied to a hard disk. By putting the original disk into drive A: again and issuing a FOR command you can use the directory of the disk in drive A: to provide the file names to be deleted from the hard disk. The FOR command can be divided up into three sections. Using the same command that we used in the last article: For $x in (*.*) do del c:\%x take the first part: For %x in The %x can be any one letter. Use two sets of % in batch files, thus: %%x. This is because DOS discards the first one in a batch file. This %x is simply a variable. A variable is a character that represents a value. For example if I state that Z = 21 then Z is a variable that is representing the value 21, until it gets changed. The % is there as a mark for DOS to identify it. In the case of the above FOR command the %x will represent in turn, each values that are stated in the next part of the command. That is, whatever appears between the brackets. In this case it is: (*.*) , so that %x represents each file name in turn. The last part of the command: do del c : \ %1 more or less explains itself if you substitute a file name for the %x. Make sure you have a grasp of the above, however feeble, and we'll tip-toe through a few other examples Have you ever tried to use wildcards with the TYPE command. Try: TYPE *.bat or some such. It does not work. However: For %x in (*.bat) do type %x works just fine. Maybe you were looking for a particular line in the batch file. Perhaps it was a particularly juicy use of the FOR command. This is a good opportunity to use another DOS command with the FOR command. For %a in (*.*) do find "For %%x in(" %a Notice I used %a in this example. It makes no difference which letter is used. The above uses the DOS FIND command to look in %a for an occurrence of "For %%x in(", which would only be in a line using the FOR command. The output of the FIND command is enough to provide the full line and name of the file. The above could have been used to look for any string of characters in any file. Don't forget that the FIND command is case sensitive so you must be exactly right when you specify what to find. So far we have looked at files in a list of *.* but there is nothing to stop us from having a different list. As I mentioned in the previous article there is a line in a batch file that is called by my Autoexec.bat file that reads For %x in (chkdsk.com find.com format. com) do copy %x G: I have a lot more files in the list between the brackets than the three shown here but you can get the general idea. G: is a ram disk included in my PATH, where I keep my frequently used utilities, DOS and batch files. It is a bit wasteful to have a 250 kB ramdisk in conventional memory but is a good thing to do if you don't have any other use for some of that 384 kB of extended memory. Anything that DOS will read as list can be placed between the brackets. The PATH is quite OK but you must place it between % marks. Like so: %PATH%. So if you wanted a directory of all the files in your PATH you could issue the command: For %Z in (%PATH%) do DIR %Z All you have to do is think of a way of getting DOS to produce the list and the FOR command becomes a powerful tool to get things done. Reprinted from the July 1991 issue of PC Update, the magazine of Melbourne PC User Group, Australia |