DOS Batch File Tutorial: Advanced Examples IDoctor. DOSBetamax's. This is the first page. These are more intricate than the. As before, the title. The more ambiguous lines. They are not part of the file. Do not type. them in. For the most complicated examples, explanations follow the. Before delving into. Batch File Basics. Note that because. Again, remember, the. DR" command is a batch file that runs the. Color Directory. program I use. You may substitute DOS' "DIR" command with its. Finally, be aware. Doctor DOS will not be responsible for any problems resulting. An advisory to non- Canadians: Some characters shown in some batchfiles here may not be able to be reproduced on your system unlessthe Country Code is changed or you type them in as ASCII characters. Consult your text editor/word processor manual to see how to do the latter. EXCEPT FOR THE BATCH FILES THEMSELVES,INFORMATION ON THESE BATCH FILE PAGESMAY NOT BE REPRODUCED WITHOUT PERMISSIONFROM THE AUTHOR ©THE BATCH FILES ARE FOR PERSONAL USE ONLY. THEY MAY NOT BE SOLD OR OTHERWISE DISTRIBUTED. THE BATCH FILES. : : Changes to an Adjacent Directory on the Same or Higher Level. Displays the Results. IF "%1" == "1" GOTO LEVEL- 1. IF "%1" == "2" GOTO LEVEL- 2. IF "%1" == "3" GOTO LEVEL- 3. IF NOT "%1" == "1" GOTO LEVEL- 1. A. The file first determines if you used a number at the command line and. The results branch to one of the LEVEL labels. The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing. Batch files, tips and techniques for DOS, Windows (all flavors) and OS/2. Our first batch file example is going to list all the files in a folder and put the list in a new text file. We will use the directory command 'dir' that is discussed on another page. Open Notepad and enter the line '@. PRELIMINARY Here are more examples of advanced batch files. As with the first Advanced Batch File Examples, the title remarks should suffice so you'll understand their operation, although some may be preceded by a. 1. As we have seen in the previous example, FTP uses the display station for command INPUT and message OUTPUT, and this needs to be overridden for use in batch mode. We use the OVRDBF command to overwrite these files with the. I will consider a number of examples that illustrate the use of 'For' and its switches. Simple iteration through a list. The set of things that are to used can be listed explicitly. For example, the set could be a list of. A batch file is a kind of script file in DOS, OS/2 and Windows. It consists of a series of commands to be executed by the command line interpreter, stored in a plain text file. A batch file may contain any command the. CD (Change Directory) command is issued. Each. " .\ " represents the next- level- up (parent) directory. Subsequent additions. Try. it yourself at the command line from any sub- directory. CD .\" will take. This file allows one to change to a directory adjacent to the current one. That is, if the directory one level up is PARENT- 1 and you are in CHILD- 1. CDS CHILD- 2" or "CDS 1 CHILD- 2", you will be whisked sideways. Note that this batch. ADVANCED BATCH FILES The IF Command. The IF command is used to control the way the batch file runs depending on the circumstances. User Parameters. When you type a parameter after a batchfile mane the parameter can be used. Either will take you to. Here is a display of that structure to help you visualise this better. Level 3 GRANDPARENT. Level 2 PARENT- 1 PARENT- 1. A. - -- -- -- -- -- -- - -- -- -- -- -- -- . Level 1 CHILD- 1 CHILD- 2 CHILD- 1. A CHILD- 2. A. You may also go to an adjacent directory two or three levels higher by. So, let's say you are. CHILD- 1. The next level up is still PARENT- 1. Adjacent to it on. PARENT- 1. A, and above both PARENT directories is. GRANDPARENT. To get to CHILD- 1. A under PARENT- 1. A, issue "CDS 2. PARENT- 1. A\CHILD- 1. A". You may do the same thing with the directory above. GRANDPARENT by using the number `3'. Note that the name "PARENT- 1. A" exceeds 8 characters. It is just used here. If your DOS is not long file name capable, this directory. PARENT- 1". As such, it could not be created. Again, this was just. Of course, in actuality. When you use this batch file, don't forget the space on either side of. If you have a good visual idea of. Directory Display? For those of you thinking about having the batch file show the. I tried that but found it to be confusing if there. If you wish to try it, add the. LEVEL" label: TREE . MOREPAUSE. After "TREE", enter the same number of dots and backslashes as are after. CD" line. When the file runs, a directory tree will be. The batch file will then pause to allow you to view the last screen of TREE. After TREE finishes, pressing any key will then take you to the directory. If you typed the wrong one, simply. CONTROL- C" to stop the batch file. Then run it again with the proper. I also found that having the tree displayed was annoying every time I. A better method would be to have the batch. Perhaps I'll write that. This sort of changing method can be tedious if you have a complicated. It can be eased with. Toddy or 4. DOS, but a. Directory Maven. Links to. DOS Websites Directory. AUTOMATIC COPYING BY DATE. Here's the scenario: You work all day on some web page files in a. UPLOAD. directory to transfer them on line to your server. Unfortunately though. You could issue a DIR /O: D or XDIR /Tcommand to sort. You could open up a file manager, hunt. Then you'd copy the marked files to the UPLOAD directory.. What would happen if you wanted files from the. That adds an extra element of complexity. As always. efficient DOS can do this for you automatically. This next batch file uses the DOS environment to hold the value of. This is called an "environment variable". That's because. the variable may have different values at different times - in this case. The. "environment" is an area of memory which, in part, holds various parameters. See. DOS Variables for more information.) The batch. XCOPY operation. to copy only today's files, or alternatively, those from a few days, weeks. This is very slick; it saves. Realise that this example was designed for those using DOS versions. See "Improvements" for further. Syntax: CTULD (Optional Date). Copies Files Modified or Created Today. Upload Directory, or Copies. Specified Date Forward. If no Date is Given, Today's. IF "%1" == "" GOTO COPY- TODAY Files will be Copied. IF NOT "%1" == "" GOTO COPY- DATE Otherwise, Files will be Copied. Forward from the Date Typed. COPY- TODAY (See Text, Below). ECHO. | DATE | FIND /I "Current" > C: \BATCH\CUR- DATE. BAT. ECHO @SET CUR- DATE=%%4 > C: \BATCH\CURRENT. BAT. CALL C: \BATCH\CUR- DATE. BAT. XCOPY *.* C: \UPLOAD /D: %CUR- DATE% > NUL Copies Files based on. Today's Date("NUL" Hides Screen Messages). COPY- DATE Copies Files based on. XCOPY *.* C: \UPLOAD /D: %1 The Date Given at the. Command Line. ECHO. Separates the Listing. CALL C: \BATCH\DR. BAT C: \UPLOAD Gives a Listing for the. UPLOAD Directory to. Show the Operation's Success. SET CUR- DATE= Removes the Date Variable. From the Environment. DEL CUR- DATE. BAT Deletes the. DEL CURRENT. BAT Temporary Batch Files. Remember: This DATE method and explanationis for those using a DOS version that does notinclude a built- in DATE variable. Those with onebuilt in may skip to the: XCOPY section. For the "COPY- DATE" section, a partial explanation of what happens may. DATE" at the command line. DOS gives the current date. CMOS battery). Then, DOS also asks you for a new date. This is so that a user may type in a given date, which is normally the. To keep this current date, hit "ENTER". The trick here in this. Enter" for you and also to isolate. This variable may then. XCOPY operation. In the batch file, the "ECHO." at the start of the "COPY- DATE" section. ENTER") to the DATE command, leaving the current. DATE" from the command line. The DATE command is then piped to the FIND command, which is asked to locate. Current". That solitary line in turn is sent. CUR- DATE. bat", which is created by this (CTULD) batch file. The text sent to the "CUR- DATE. Current Date is (day). Note that the fourth parameter (word) after "Current", matches. For the next line, using the DOS "SET" command, a variable called. CUR- DATE" is made from the fourth parameter (designated `%%4'). This is. sent to another created file called "CURRENT. Now everything is in place, so let's review. We have one file called "CUR- DATE. Current Date is (day) (date)". This is called by CTULD. Being. a batch file, DOS tries to run CUR- DATE. Since the first word in that. Current", DOS believes there is a program or file able to be executed. Current". Well, guess what? A batch file called "Current" was just. So DOS in turn runs that, because DOS sees "Current" to be a batch file. The fourth. parameter is the current date. CURRENT. BAT executes its only line which says. CUR- DATE". Now, when CUR- DATE finishes, control is returned to CTULD. XCOPY line that has a variable in it now being defined as the. When the XCOPY line is run, the current date will be substituted for. CUR- DATE" every time, and thus XCOPY will copy files to the UPLOAD. Slick, huh? Since. CTULD. bat is run, the XCOPY date. Even if the files already exist. Regardless of. when you run CTULD. In that case, control is. COPY- DATE" section. For the "COPY- DATE" section, DOS simply substitutes the date you type on. XCOPY based on that date. Thus if you type. CTULD (last week's date), DOS will copy files to the UPLOAD directory on. Remember to type the. Users of this batch file should be aware thatit was based on MS- DOS. Those using other DOSmanufacturers or using DOS under Windows willhave to alter the batch file to match whattheir DATE commands output.). Improvements. This file can be shortened by using updated DOS commands or by using. DOS that includes the `%DATE%' variable built in. For those. using DOS versions without the latter, one method is to get an update of. SET" command, called "XSET". It can set a date variable with just one. This, and many other capabilities are built. XSET command. If used in the above example, a one- line XSET command can set "CUR- DATE". XCOPY- by- date command. It shortens. and simplifies the batch file. See DRTD. bat, farther on. XSET date technique. Another, even shorter method is done via an update to "XCOPY" called. XXCOPY". It can do either operation using just one line: XXCOPY *.* /DA: %1. One would go to the directory in which the command is to be issued, and. CTULD ." for today's files, or enter "CTULD (date)" to copy files on. Excluding a title and comments, this method. CTULD batch file to just one line! Links to these updated commands may seen in the: DOS Websites Directory. Requires DOS 6 or Newer). Although floppy discs have given way to flashdrives, they are still used. This batch file allows. This is handy if you need a blank disc but want to check first. Regarding flashdrives, this batch file could be modified to format one. B' used here. Of course. USB drivers must be loaded and the flashdrive mounted. It will be assumed that the `B' drive is to be used and that the floppy. Change these if yours differ. Shows Directory of `B' Drive. Prompts to Do a Full or Quick Format or No Format. If Quick Format is Chosen, Does Not Save Unformat Information. CALL C: \BATCH\DR B: \. ECHO Full Format B Drive? ECHO Press `F' to Format. ECHO Quick Format B Drive? More Powerful Batch Files Part IIComputers are very good at doing the same thing over and over. The command line contains a powerful and versatile method for carrying out this type of operation. With this method, you can automate many time- consuming tasks. The basic statement is of the form. For those who persist in calling the command line DOS, note that the 3. For" statement is much more powerful than the old 1. DOS version.) A single- letter replaceable variable is used to represent each item as the command steps through the the collection (called a "set"). Note that, unlike most of Windows, variables are case- dependent. Thus "a" and "A" are two different variables. The variable has no significance outside the "For" statement. I will be using X throughout the discussion but any letter will do. In principle, certain non- alphanumeric characters can also be used but that seems like a bad idea to me.) The variable letter is preceded with a single percent sign when using the command line directly or double percent signs in a batch file. Thus the statement in a batch file looks like this: for %%X in (set) do (command) What makes the "For" statement so powerful is the variety of objects that can be put in the set of things that the command iterates through, the availability of wildcards, and the capability for parsing files and command output. A number of switches or modifiers are available to help define the type of items in the set. Table I lists the switches. They are listed in upper case for clarity but are not case- sensitive. Switch. Function/DIndicates that the set contains directories. RCauses the command to be executed recursively through the sub- directories of an indicated parent directory /LLoops through a command using starting, stepping, and ending parameters indicated in the set. FParses files or command output in a variety of ways I will consider a number of examples that illustrate the use of "For" and its switches. Simple iteration through a list. The set of things that are to used can be listed explicitly. For example, the set could be a list of files: for %%X in (file. Care must be taken to use correct paths when doing file operations.) A different example where the set items are strings is: For %%X in (eenie meenie miney moe) do (echo %%X)Wildcards can be also be used to denote a file set. For example: for %%X in (*. This will carry out the command on all files in the working directory with extension "jpg". This process can be carried further by using several members in the set. For example to carry out a command on more than one file type use: for %%X in (*. As always, keep in mind that the command line may choke on file names with spaces unless the name is enclosed correctly in quotes. Therefore, you might want to use "%%X" in the "command" section. Looping through a series of values. The well known action of stepping through a series of values that was discussed in connection with "if" and "Goto" statements is succinctly done with the switch /l (This switch is an "ell", not a "one") . The statement has the form: for /l %%X in (start, step, end) do command The set consists of integers defining the initial value of X, the amount to increment (or decrement) X in each step, and the final value for X when the process will stop. On the previous page, I gave an example batch file that listed all the numbers from 1 to 9. If we use a "For" statement, that task can be accomplished with one line: for /l %%X in (1,1,9. X > > E: \numbers. The numbers in the set mean that the initial value of X is 1, X is then increased by 1 in each iteration, and the final value of X is 9. Working with directories. If you wish to use directories in the variable set, use the switch /d. The form of the command is for /d %%X in (directory. Set) do command. An example that would list all the directories (but not sub- directories) on the C: drive is for /d %%X in (C: \*) do echo %%X Recursing through sub- directories. If you want a command to apply to the sub- directories as well as a parent directory, use the switch /r. Then the command has the form: for /r [parent directory] %%X in (set) do command Note that you can designate the top directory in the tree that you want to work with. This gets around the often cumbersome problem of taking into account which is the working directory for the command shell. For example the statement: for /r C: \pictures %%X in (*. X > > E: \listjpg. C: \pictures and its sub- directories. Of course, a "dir" command can do the same thing but this example illustrates this particular command. Parsing text files, strings, and command output. Now we come to a truly powerful switch that was not even dreamed of back in the DOS days of yore. The switch /f takes us into advanced territory so I can only indicate the many aspects of its application. Things become rather complex so those who are interested should consult programming books or the Microsoft documentation. However, here is a brief sketch of what's involved. This version of the "For" command allows you to examine and parse text from files, strings, and command output. It has the form for /f [options] %%X in (source) do command"Options" are the text matching criteria and "source" is where the text is to be found. One of the interesting applications is to analyze the output of a command or commands and to take further action based on what the initial output was.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
October 2016
Categories |