You are on page 1of 24

ADVANCED BATCH FILES III

Advanced III Preliminary Read This First!


DEL-OLD.bat
Delete any Number of Oldest Files

DKH.bat
Write Batch Files From the Command Line

DRT.bat
List Files by Type

LOC.bat
Locate Files

NOTE.bat
A Note Maker (with DDNOTE.bat and NOTET.bat)

SEQ.bat
Copy Files in Order (Includes PROJ-SEQ.bat)

TRAN.bat
Transfer Files Straight to any Directory of Choice

PRELIMINARY
Here is a third set of advanced batch files. The explanation style will emulate that of the more
complicated examples as seen in the first two sets. This will be done by first showing the batch file then following it with an explanation and perhaps notes.

Because this is the third advanced page, it is assumed by now that you know the basics of batch files
and their commands, and are familiar with the utilities and updated DOS commands used in the previous examples. They will not be explained further here. See the Batch File Basics and other Advanced Batch Files sections if you need. Links to them are at the top and bottom of this webpage.

As always, the "DR" command is a batch file that runs an after-market directory program I use called
Color Directory. You may substitute DOS' "DIR" command with its switches set to your preferences. Also, it is assumed that you are placing all batch files into the "C:\BATCH" directory which is in your path.

Finally, be aware that Doctor DOS will not be responsible for any problems resulting from the use or mis-use of anything presented here.

An advisory to non-Canadians: Some characters shown in some batch files here may not be able to be reproduced on your system unless the 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 PAGES MAY NOT BE REPRODUCED WITHOUT PERMISSION FROM THE AUTHOR THE BATCH FILES ARE FOR PERSONAL USE ONLY. THEY MAY NOT BE SOLD OR OTHERWISE DISTRIBUTED.

THE BATCH FILES


DELETE OLDEST FILES
This batch file will allow you to delete any number of oldest files in a given directory, independently of their dates. That is, you will not have to know or enter any date. It will simply select the oldest `n' files and remove them. This batch file uses XSET and assumes it is in your C:\DOS directory.

:: DEL-OLD.bat :: Deletes the Oldest `n' Files :: @ECHO OFF SET LOOP=0 :LOOP DIR /A-D /OD /B | C:\DOS\XSET /LINE 1 FILE DEL %FILE% C:\DOS\XSET /MATH LOOP=%LOOP% +1 IF NOT "%LOOP%" == "%1" GOTO LOOP F:\BATCH\DR SET LOOP= :END ________

DEL-OLD.bat

Explanation
The file begins by setting a DOS environmental value "%LOOP%". It then uses the DIR command to ignore directories (/A-D) and get a list of the files in date order (/OD) in a bare format (/B). This is piped to XSET which uses its "/LINE" switch to make an environment value (FILE) equal to the first line. That first line is the oldest file in that directory. Next, that value is used by DEL (Delete) to remove the actual file from the directory. Then XSET increases the value of "%LOOP%" by one. Since it had been initially set to `0'. it is now equal to `1'. Now, an "IF" statement is used to see if the new value of "%LOOP%" is equal to that typed at the command line by yourself. If you wanted to delete five files, you would have typed "DEL-OLD 5". Since the value of `1' is not equal to `5', the process is directed to the "LOOP" label and the procedure is

repeated until the oldest five files have been deleted. At that point, `5' will be equal to `5' so the delete sequebce ends and the batch file gives a directory listing to show the results. Finally, the "%LOOP%" value is removed from memory.

Usage
To delete the five oldest files, go to the desired directory, and simply type "DEL-OLD 5". If you type a number greater than the total number of files in the directory, you will get some error messages, but the deletion process will complete properly and the final directory listing will show that directory as being empty. Note that you may switch this batch file to being able to delete the newest `n' files by changing the "/OD" DIR switch to "/O-D". XSET may be obtained from -xset.tripod.com.

AUTO-BATCH and COMMAND RECALL


Have you ever just typed a series of commands - a series you have typed often - and thought: "I am tired of typing this. I should place them into a batch file.", but never seem to get around to doing it? Well, this next batch file will help with just that. As a bonus, it will allow recall of a given command or set of commands. This is all done via DOSKEY. That command came out in MS-DOS 5, but there are other similar utilities available which could be used for a similar purpose as is about to be shown. The batch file also requires CHOICE, which came out in MS-DOS 6. Again, similar utilities are available, but you will have to re-write the batch to make them work as intended here. A bonus to loading DOSKEY is that you can recall any command previously typed, up to the limit of the buffer which keeps track of those commands. Although there will be more on this later, I will not go into it too much. See your DOS manual or on-screen help. First, let's have a look at that batch file:

DKH.bat
:: DKH.bat :: Saves DOSKEY History to a File :: Allows Previous Commands to be Viewed and Edited :: @ECHO OFF C:\DOS\DOSKEY /H > C:\BATCH\DK-HIST ECHO. ECHO ECHO. ECHO ECHO. Edit DOSKEY History? `E' `Escape' Exits

CHOICE /C:E<- /N > NUL IF ERRORLEVEL 2 GOTO END

IF ERRORLEVEL 1 GOTO EDIT GOTO END :EDIT C:\DOS\EDIT C:\BATCH\DK-HIST CLS ECHO. ECHO ECHO. ECHO ECHO. Delete DOSKEY History? `Escape' Exits and Keeps DOSKEY History File

CHOICE /C:D<- /N > NUL IF ERRORLEVEL 2 GOTO END IF ERRORLEVEL 1 GOTO DELETE GOTO END :DELETE DEL C:\BATCH\DK-HIST :END ________

(Note that ` <- ' represents the "Escape" character. To create it, press ^P (Control-P) in DOS Edit, then the "Escape" key. Other text editors allow one to create the "Escape" character by pressing ^V first.)

Explanation
DOSKEY's "/H" switch is used to send the command history to the DK-HST.txt file. It will save all the commands previously typed, up to the limit of the command buffer. Should you wish, you may increase the size of this buffer to hold more commands. See DOSKEY help on screen or in your DOS manual. Next, a choice to edit this file or not is displayed and the CHOICE command is then used to direct the batch file to the appropriate section via GOTO commands. If ESCAPE is pressed, the batch file ends. If `E' is pressed, the DK-HST.txt file is loaded into EDIT where one may eliminate all but those lines required to make a new batch file. After exiting EDIT, another CHOICE allows for deletion or not of the DK-HST.txt file. The batch file then ends.

Usage
First, you must load DOSKEY via your AUTOEXEC.bat, preferably into upper memory. See your DOS manual for how to do this. Then enter commands as usual so that they will be remembered by DOSKEY. You then have a few things to consider when using the DOSKEY technique. If you simply want to review the command buffer's entries, you may press "F7" and DOSKEY will show a list right at the command line. However, if you wish to be able to scroll back & forth or need to edit the history file, enter "DKH". The DOSKEY buffer's contents will be copied into a text file which will in turn be loaded into EDIT. You may then peruse and/or edit any part of it at will. Be aware that if you had kept a previous DKH.txt file, that it will overwritten when you run this batch file.

There are other DOSKEY features, but I won't get into them here because the main idea of "DKH" is to be able to use it to do most of the writing of a batch file while seeing the results of the commands as they are typed. If you decide to keep some of the commands as a batch file, save only them under your proposed batch file name. To do this, simply edit out any command which is not part of the intended batch file. Add a title, some comments, and an "ECHO OFF" command, and you will have a basic batch file. Rename the edited version as a batch file. Alternatively, you may cut or copy out the lines of commands you want, open a new file under the proposed batch file name and paste the cut or copied lines into it. Add the title, comments, and "ECHO OFF" and anything else required. Then save it. A shortcut may be used provided you don't want to keep any current history buffer. Pressing "ALT-F7" before typing the commands you intend to use in the batch file, will clear the buffer. Then type the commands you wish to use. The commands will be issued as you type them so you may see the results. Afterwards, enter "DKH", and only those commands will appear "DK-HST.txt". Edit the file and add a title, comments, etc.; then rename it as a batch file.

LIST FILES BY TYPE


Here is a simple batch file which will allow one to quickly list files of a given extension when in any directory. This removes clutter from any listing and allows one to quickly peruse a specific file type. I have geared this to the DOS "DIR" command instead of the Color Directory program I use. This is because you have DIR but may not have "Color Directory". Using the latter program means the batch file has to be written differently, and I did not want to confuse you when it came to using this batch file with DIR.

DRT.bat
:: DRT.bat :: Lists All or Specified .txt Files :: @ECHO OFF ECHO. IF "%1" == "" C:\BATCH\DIR *.TXT GOTO END SET C:\DOS\XSET=/UPPER C:\DOS\XSET SWITCH=%1 IF "SWITCH" == "/D" C:\BATCH\DIR *.TXT /O:D GOTO END IF "SWITCH" == "/Z" C:\BATCH\DIR *.TXT /O:S GOTO END IF NOT "%1" == "" C:\BATCH\DIR %1.TXT :END ________

Explanation
First, a blank line is placed on screen via "ECHO." This separates the succeeding directory listing from the prompt above. Next, if no parameter is issued with the batch file name at the command line, a directory listing of all .txt (text) files is displayed with all information, and then the batch file ends. However, if a parameter is issued, the batch file goes to the next lines. These use XSET - first to make all parameters given at the command line be seen as upper case, then to set the variable called "SWITCH" to be equal to the parameter given at the command line. Now, when the IF statements are issued, no matter whether the parameter is lower or upper case, it will be seen as upper case. This is done because "IF" is case sensitive in many DOS versions. Making all given parameters be upper case means only having to have one IF statement per parameter. So even if you were to type `/d', the IF statement for that switch would be true because XSET has told it to be passed to IF as `/D'. If you don't want to use XSET, simply issue one line for each case:
IF "SWITCH" == "/d" C:\BATCH\DIR *.TXT /O:D IF "SWITCH" == "/D" C:\BATCH\DIR *.TXT /O:D

So, the next two IF lines check to see if the parameter is `/D' or `/Z'. If it's `/D', the .txt files will be listed by ascending date order; if it's `/Z', the files are shown in ascending size order. After either, the batch file ends. (To reverse the order, place a minus sign ( - ) in front of the date or size letter: /O:-D or /O:-S. Now, if it's neither, the last line is read. It will display the selected text file(s) as given at the command line. This allows you to get a directory of one specific .txt file or by using the ` * 'or ` ? ' wild cards, all those beginning with say, "READ" (DRT READ*). Once something is displayed, the batch file ends. If no matching file(s) is found, a prompt will be shown to that affect.

Usage
Simply issue "DRT" to see all text files in the current directory. To see a specific one, issue "DRT (file name)". Remember, wild cards ` * ' and ` ? ' are permitted. However, be sure to give the file name with no dot or extension. The batch file fills that in for you. To see all text files by date or size, issue "DRT /D" or "DRT /Z" I did not use "/S" for "size" because DIR already uses "/S" to mean "subdirectories". I did not want to confuse users of this batch file by using "/S" in this instance. Be aware, that the way this batch file is currently written will not sort specific file names by date or size. Now, take this batch file example further: By substituting a different last letter in the batch file name, and substituting a different extension within the batch file itself, one may list any type of file wished. So, now make a series of these batch files: DRC, DRD, DRE, DRH, DRZ, etc. You will be able to view a list of .com, .doc, .exe, .htm, .zip, etc. files. XSET may be obtained from -xset.tripod.com.

Locate Files Anywhere On any Drive

You have created a file but it went to some unintended directory, or you have simply forgotten where some file resides. This batch file will allow you to find that file, provided you know at least the first part of the its name. You may also use it to find file types anywhere on the drive.

LOC.bat
:: LOC.bat :: Locates Specified File Name :: Searches from the Root or Current Directory & Below :: Can Also Search a Specified Drive :: Wildcards may be Used in File Name :: :: Use `/S' to Search Current Directory & Below :: @ECHO OFF SET XSET=/UPPER C:\DOS\XSET PARAMETER=%1 IF "%PARAMETER%" == "ALL" GOTO ALL-DRIVE IF "%PARAMETER%" == "C" GOTO SPEC-DRIVE IF "%PARAMETER%" == "D" GOTO SPEC-DRIVE IF "%PARAMETER%" == "E" GOTO SPEC-DRIVE IF "%PARAMETER%" == "/S" GOTO CURRENT IF NOT "%PARAMETER%" == "/S" GOTO ROOT :ALL-DRIVE ECHO. ECHO Searching all Drives for " %2 " ECHO. FOR %%D IN (C: D: E:) DO DIR %%D\%2 /B /P /S GOTO END :SPEC-DRIVE ECHO. ECHO Searching Drive %PARAMETER%: for " %2 " ECHO. DIR %1:\%2 /B /P /S GOTO END :CURRENT XSET CUR-DIRECTORY DIR ECHO. ECHO Searching %CUR-DIRECTORY% & Below for " %2 " ECHO. DIR %2 /B /P /S GOTO END :ROOT ECHO. ECHO Searching Current Drive for " %1 " ECHO. DIR \%1 /B /P /S :END ECHO. SET XSET= SET PARAMETER= SET CUR-DIRECTORY= ________

Explanation

This begins by using XSET to make any command line parameters be passed to the batch file as upper case. This is to eliminate additional "IF" statements having to be used to cover lower-case entries. This technique was seen and explained earlier on this page in DRT.bat. The "IF" statements determine if you entered "ALL", a drive letter, a dot, or just a file name after the batch file name. If you enter "ALL", the "ALL-DRIVE" section comes into play. A message is placed on screen saying that all drives are being searched for the specified file. This section uses a FOR-IN-DO (FOR) command to have DIR search for your file on each drive listed, from its root directory on down. It essentially says: "FOR each Directory listed INside the Brackets, DO a DIR listing for each of those Directories using the succeeding parameters and switches". A blank line is placed on screen as a separator via the "ECHO." in the preceding line, and then "FOR" has the "DIR" command display any matches in a "Bare" format via the "/B". That means just the file and its path are seen. If the screen fills, DIR's `/P' parameter will pause after each screen full. This is done for each drive listed. However, if a specific drive letter was typed, the batch file branches to the "SPEC-DRIVE" section and displays, as above, any matching files found. If you have more or fewer drives, add or eliminate the drive "IF" statements, as necessary. Do the same for the drive letters listed inside the "FOR" brackets. Now, if "/S" was typed, only the current directory and below will be searched. If only a file name is typed, all of the current drive will be searched. In all cases, a message will tell you what is being searched and for what file, and the matching file(s) will displayed with a blank line above and the file with a path name will be shown.

Usage
Simply type "LOC (file name)" to start a search of the current drive. If you wish to search another directory on the same drive from the one in which you are currently, enter its path without a backslash. Thus, if you are in C:\BATCH and wish to search C:\UTIL\ZIP, enter "LOC UTIL\ZIP\(file name)". Use a drive letter (with no colon) and the entire named drive will be searched. To search a subdirectory of that drive, use the directory path without a backslash. So if you were on the `E' drive, to locate all the .pcx files in your graphics directory and its subdirectories on the `C' drive, type "LOC C GRAPHICS\*.PCX". Be sure to have a space on either side of the drive letter. To search only the current directory and below, use "/S" as the first command line parameter. For the .pcx example, type "LOC /S *.PCX". Use "ALL" as a parameter to search every directory on every drive. Wildcards may be used if you want a range of files or can't remember the full name. Thus, typing "LOC ALL READ-ME.TXT" would find all such files anywhere within the drives you specified in the batch file. Similarity, typing "LOC ALL READ*.*" will find any file starting with "READ". If you are in any subdirectory and wish to locate such files just on the current drive, simply type "LOC READ*.*". With no parameters other than a file name, LOC.bat will always search the current drive in full. If you see the file you want and wish to stop the search, enter "CONTROL-C". Be sure to set BREAK to "ON" in your AUTOEXEC.bat so that DOS will stop immediately. If it is off, DOS will take longer to stop the search. XSET may be obtained from -xset.tripod.com.

Make Note Files based On the Current Date or Under a Given File Name
Sometimes you wish to be able to remember something but don't want to have to fire up a word processor or text editor to type it up. This simple batch file allows one to quickly type some items right at the command line which will automatically be placed into a file with a name you specify; or for really fast note-taking, this batch file can automatically name the note file with today's date.

:: NOTE.bat :: Creates a Note File for the Current Date or Given Name :: Able to Append Text to Same :: @ECHO OFF IF "%1" == "" GOTO TODAY :NAME COPY CON C:\NOTES\TEMP.NOT TYPE C:\NOTES\TEMP.NOT >> C:\NOTES\%1.NOT GOTO END :TODAY COPY CON C:\NOTES\TEMP.NOT C:\DOS\XSET CUR-DATE DATE YY-MM-DD TYPE C:\NOTES\TEMP.NOT >> C:\NOTES\%CUR-DATE%.NOT :END DEL C:\NOTES\TEMP.NOT SET CUR-DATE= ________

NOTE.bat

Explanation
The batch file first determines if you typed a file name at the command line. If you have not, it branches to the "TODAY" label. If you have, this "IF" statement will be false and the next section of the batch file will be executed. That section starts off by using the DOS "COPY CON" feature. It will copy whatever you type at the console (keyboard) and save it to a file. In this case, that file is named "TEMP.not" for "Temporary Note". (The usage of "COPY CON" will be explained farther on.) Once you save the TEMP.not file, its information is sent to the permanent note file which will be "%1.not". "%1" is a replaceable parameter, so it will be substituted with what ever file name you typed at the command line. Thus, if you entered "NOTE PROJECT", the "%1" would become "PROJECT", so your final (permanent) note file would be called "PROJECT.not". This may be done from anywhere within DOS, and when finished, you will remain in the directory from which you issued NOTE.bat. Thus, you may quickly interrupt what you are doing and then return immediately to the current task. Notice the use of the DOS append redirector ( >> ). This will create a file of the given name if one does not exist, but if it does, the information will be appended (added) to the file. So when you first type a note

with a new file name, the file will be created, with the ".not" extension added for you by the batch file. However, if you type more notes at a later time under the same name, those new notes will be added to the old file. In either case, the TEMP.not file will be copied to the appropriate permanent note file and this second file will be placed into the NOTES directory. The batch file then goes to the end where upon the "TEMP.not" file is deleted.

Current-Date Notes Now, if you don't give a name at the command line, the opening "IF" statement will be true and the batch file will move to the "TODAY" section. Again, a file called "TEMP.not" will be created and will be copied or appended to a permanent note file in the NOTES directory. However, the file will automatically be named based on today's date. This is done by using an update to the DOS "SET" command called "XSET". It places today's date into an environment variable called "CUR-DATE". This is then used to name the permanent note file, which is again placed into the NOTES directory. The format will be YEAR - MONTH - DAY so that files named with it will be in chronological order in the directory. As before, if this file does not exist, it is created; otherwise any additional notes you type on the given day are added to the existing file. If you type notes on the succeeding days, a new file is created each day with that day's date. Again, the batch file then goes to the end where the "TEMP.not" file is deleted. Plus, the CUR-DATE variable is removed from memory by resetting it to equal nothing. An XSET link will be given farther on. Those of you with the %DATE% variable built into your version of DOS may eliminate the XSET lines that are used to get the date into the environment and reword the remainder of file to take this into account.

EXTRA STEPS? Some of you may be wondering why the TEMP.not file is bothered with instead of just creating or appending the actual file name directly. That could be arranged, but using a temporary file is a safety in case one was in the NOTES directory and decided to type in a note. If an older version of DOS were being used, after exiting the COPY CON part of the batch file, any existing file would be overwritten and additional notes would *replace* the previous and not be added to them. Newer versions of COPY CON prompt for an overwrite, but that is just an extra step for the user. Also, if one wished to append to the file, that is not available from COPY CON, so that possibility would additionally have to be written into the batch file. The method shown here eliminates these COPY CON problems and keeps the batch file short.

Usage
To use the file, first create the "C:\NOTES" directory. Then whenever a "notes" urge strikes, simply type "NOTE" at the command line to place text in a file with the current date in the name. Alternatively, if you have a specific project in mind, then type a space and a name after the "NOTE" command. (Do not add a file extension - the batch file does that for you, and do not use the name "TEMP" because the batch file uses that name and then deletes it afterwards.) Any subsequent notes will be placed in one or the other file. The next day, of course, a new note file will be created if you enter one based on date.

A hint to improve the note file layout is to add an opening blank line to any appended notes. This will give you a separator when you peruse the file after several appends, making it easier to read. It also makes it easier to discern which notes were in one session and those which are in another. Unfortunately, you have to remember to do this, so it's something extra about which to think. (The improved version farther on, will do this for you, plus more.)

COPY CON When you type "NOTE" with or without a file name, the cursor will drop to a new line signalling that you are using COPY CON. Type in your text, pressing ENTER when you wish to start a new line. You may type long lines, but these lines are limited to 127 characters. If your screen setting is less than that, the line will automatically wrap to the next and beep when 127 character spaces are filled. Press ENTER to start a new line. In reality, this batch file in meant for short notes and not an essay, so line-wrap should not really come into play here. I mainly use this batch file to type in a quick address, phone/fax number, or URL. None of those are likely to have a line exceed the screen width or line length limitation. Regardless, I recommend against using this batch file if it might contain lines which exceed the screen width because they might not wrap in some file viewers or text editors/word processors. This means the line will trail off the screen to the right and scrolling will be required. Pressing ENTER at the end of each line during composition of the original file will prevent this if you must use long lines. When you are finished, press "Control-Z", or on some systems, you may press "F6" to get a ^Z character. This character signals the end of the file. Press ENTER once more and the file will be saved and copied to the NOTES directory. If you have hopelessly messed up what you are typing at the command line or simply change your mind about the note file, then at any time before saving the COPY CON file, you may press "Control-C" to stop. That one command will terminate COPY CON and also display a "Terminate batch job?" prompt. Enter `Y' to stop immediately. No file will be saved. In fact, if you enter `N', the batch file will still terminate in this case. You may then try again, if you wish, by reentering the batch file name with or without a note file name. Realise that using "Control-C" means a TEMP.not file will be created and not erased. However, after the next un-terminated NOTE.bat, it will be overwritten, and then deleted when the permanent note file is created. Be aware that in COPY CON, you can only edit the line on which you are currently, and even at that, only by backspacing away the characters. Once you press ENTER and drop to the next line, the previous line becomes inaccessible. If you wish to edit any of these files later, you will have to load the file into a text editor.

The nice thing about this simple method is that since all notes are in a NOTES directory, you may peruse them all at a glance to see what projects or dates have notes attached. You may use the DOS "TYPE" command to look at the contents, or any file viewer may be used because these files are all in plain ASCII text. Here is the link to get XSET.

NOTE.bat (Improved)
So we now have a batch file which can quickly copy notes from the command line. However, the data in the file will be all crammed together and have no title. Therefore, this improved batch file will add a title and spacing. Also, if one were to append to these files regularly and keep them for any length of time, the question might arise as to exactly when the notes were taken. So, in addition, log dates & times for both the "NAME" and "TODAY" versions will be placed before each entry.
:: NOTE.bat (Improved) :: Creates a Note File for the Current Date or a Given Name :: Codes a Time into Current-Date Files :: Codes a Date & Time into Named Files :: @ECHO OFF IF "%1" == "" GOTO TODAY :: Name Section :: ############ COPY CON C:\NOTES\TEMP.NOT C:\DOS\XSET CUR-DATE DATE C:\DOS\XSET CUR-TIME TIME IF NOT EXIST C:\NOTES\%1.NOT GOTO TITLE IF EXIST C:\NOTES\%1.NOT GOTO BODY :TITLE ECHO %1 Note File > C:\NOTES\%1.NOT

:BODY ECHO. >> C:\NOTES\%1.NOT ECHO DATE: %CUR-DATE% >> C:\NOTES\%1.NOT ECHO TIME: %CUR-TIME% >> C:\NOTES\%1.NOT ECHO. >> C:\NOTES\%1.NOT TYPE C:\NOTES\TEMP.NOT >> C:\NOTES\%1.NOT ECHO -------- >> C:\NOTES\%1.NOT ECHO. >> C:\NOTES\%1.NOT GOTO END :: Current Date Section :: ############ :TODAY COPY CON C:\NOTES\TEMP.NOT C:\DOS\XSET CUR-DATE DATE YY-MM-DD C:\DOS\XSET CUR-TIME TIME IF NOT EXIST C:\NOTES\%CUR-DATE%.NOT GOTO TITLE2 IF EXIST C:\NOTES\%CUR-DATE%.NOT GOTO BODY2 :TITLE2 ECHO %CUR-DATE% Note File > C:\NOTES\%CUR-DATE%.NOT :BODY2 ECHO. >> C:\NOTES\%CUR-DATE%.NOT ECHO DATE: %CUR-DATE% >> C:\NOTES\%CUR-DATE%.NOT ECHO TIME: %CUR-TIME% >> C:\NOTES\%CUR-DATE%.NOT ECHO. >> C:\NOTES\%CUR-DATE%.NOT TYPE C:\NOTES\TEMP.NOT >> C:\NOTES\%CUR-DATE%.NOT

ECHO -------- >> C:\NOTES\%CUR-DATE%.NOT ECHO. >> C:\NOTES\%CUR-DATE%.NOT GOTO END :END DEL C:\NOTES\TEMP.NOT SET CUR-DATE= SET CUR-TIME=

________

Explanation
This begins as before with the file branching to the appropriate section based on whether or not you have given a file name at the command line. For the "NAME" section in this improved version of NOTE.bat, COPY CON is issued and you type the text to create "TEMP.not", just as in the previous version. After you save & exit, XSET is used to place the current date and time into separate variables. Next, two "IF" statements decide whether the permanent note file has been created. If not, the batch file places a title at the start of the new file. As in the previous version of this batch file, the file name you type at the command line is substituted for "%1" and it becomes the name of the new file. The batch file adds a ".not" file-name extension. After this, the batch file continues with the body of the file. Earlier, if it was found the file already existed because you had previously typed notes, the batch file would have jumped directly to the BODY section. In the BODY section, twin redirectors ( >> ) are used to send the next information to the permanent note file. (Remember that the double redirector appends, not overwrites.) First, a blank line is sent. This separates the title or previous entries from what follows. Next, the current date & time are added; another blank line is added, and then the temporary note file is copied to the new file. Finally, dashed & blank lines are placed into the permanent file to separate it from any notes which might be added later. The batch file finishes by going to the end where the temporary note file is deleted and the environment variables are removed from memory by setting them to equal nothing. Now, if no name is given at the command line, the file goes to the "TODAY" section. Here, a file name is made from the current date and is either created or appended to in the same manner as in the NAME section.

Usage
As with the first version of this batch file, first create the "C:\NOTES" directory. Then simply type "NOTE" at the command line to place text into a file with the current date in the name and with each note section prefaced by a time. Alternatively, typing a space and a name after the "NOTE" command will create the same type of file but under the name you give. See REMIND.bat in Advanced Batch Files II for a way to place notes into the command prompt.

A NOTE.bat Addendum

We have a way to copy notes from the command line. It would now be nice to be able to review the notes and to delete ones no longer needed. Here is an extra batch file to do just that:

DDNOTE.bat
:: DDNOTE.bat :: Displays and Prompts for Deletion of Note Files :: @ECHO OFF IF "%1" == "*TASKS*" GOTO FUNCTIONS FOR %%F IN (C:\NOTES\*.NOT) DO CALL C:\BATCH\DDNOTE.BAT *TASKS* %%F GOTO END :FUNCTIONS CLS MORE < %2 DEL %2 /P :END ________

Explanation
Ignore the opening "IF" statement for a moment and look at the "FOR" line. It translates to: "FOR each File IN the NOTES directory, DO run the DDNOTE batch file with a "*TASKS*" parameter and then the .not file name". So for each .not file, DDNOTE.bat, is rerun. Now, that first "IF" statement will be true because the first parameter has become "*TASKS*". Thus, the batch file will now be directed to the "FUNCTIONS" section. In the FUNCTIONS section, the screen is cleared and the MORE command is directed to take input from each file passed to it by "FOR". It displays the file a screen at a time. Next, the DEL (DELETE) command shows the file name and prompts to delete or not. You may press `Y' to delete, or `N' to keep the file. After this, control returns to "FOR", which passes the next file in the NOTES directory back to DDNOTE.bat with a "*TASKS*" parameter and the process repeats until all files in the directory have been displayed. At that point, the batch file is directed to end.

Usage
When you wish to review your notes, from anywhere in DOS issue "DDNOTE". Each note will appear on the screen. Afterwards, the file name will be given and a prompt to delete or not delete will show. After choosing `Y' or `N', another file will be displayed, and so on, until all files have been gone through. To cancel the batch file at any time, press "Control-C" and answer `Y' to the "Terminate batch job?" prompt. When finished, you will remain in the directory from which you issued the batch file so you may continue with any previous work.

So now, what if you have a zillion notes and just want to peruse one of them instead of cycling through the lot? Here's a version to do that:

DDNOTE.bat (Alternate Version)


:: DDNOTE.bat (Alternate Version) :: Displays and Prompts for Deletion of All or a Specified Note File :: @ECHO OFF IF "%1" == "*TASKS*" GOTO ALL-FUNCTIONS IF "%1" == "" GOTO ALL IF NOT "%1" == "" GOTO SPECIFIC :ALL FOR %%F IN (C:\NOTES\*.NOT) DO CALL C:\BATCH\DDNOT.BAT *TASKS* %%F GOTO END :ALL-FUNCTIONS CLS MORE < %2 DEL %2 /P GOTO END :SPECIFIC IF NOT EXIST C:\NOTES\%1.NOT GOTO NO-FILE CLS MORE < C:\NOTES\%1.NOT DEL C:\NOTES\%1.NOT /P GOTO END :NO-FILE ECHO. ECHO ECHO. :END

File Not Found!

________

Explanation
Again, ignore the opening "IF" statement. The second and third "IF" statements determine if you entered a file name at the command line. If not, the file works as does the previous version. However,if you have given a file name, control is transferred to the "SPECIFIC" part. There, it's checked to see if the file name you gave exists. If not, a prompt is shown and the batch file ends. If the file exists, it is displayed a screen at a time and then you are prompted if you wish to delete it or not and the batch file ends.

Usage
When you wish to review your notes, from anywhere in DOS issue "DDNOTE" or "DDNOTE (filename). The specified or all notes will appear on the screen one at a time. Afterwards, the file name will be given and a prompt to delete or not delete will show. After choosing `Y' or `N', if there are more files to show, you'll see them and be given a chance to delete each. Otherwise, the batch file ends. As before, when finished, you will remain in the directory from which you issued the batch file so you may continue with any previous work.

NOTET.bat A Variation and Improvement


I recently received a suggestion that NOTE.bat be able to append updates to an existing note at the top of the file. That way, the latest information would be first. Placing information at the top of a file is easy. However, I wanted only one title to appear in each file. This means the new information would have to be inserted between the title and previous entries. While writing a batch file to do that, I realised I could combine all of the previous versions of NOTE.bat plus DDNOTE.bat, and I could make the whole file much more efficient than it had been. Here is that version. This will append note updates to the beginning of any existing Note file:

NOTET.bat
:: NOTET.bat (Note Top) :: Creates a Note File for a Given Name or for the Current Date :: Codes a Date & Time into Named Files :: Codes a Time into Current-Date Files :: Appends New Information to the Top of an Existing Note File :: :: Prompts to View and/or Edit the Note file. :: @ECHO OFF IF "%1" == "" GOTO NOTE-TODAY :NOTE-NAME :: Name Section :: (This Section Creates or Appends a Note File by Name) :: ############ C:\DOS\XSET CUR-DATE DATE C:\DOS\XSET CUR-TIME TIME ECHO %1 Note File > C:\NOTES\TEMP.NOT ECHO. >> C:\NOTES\TEMP.NOT ECHO DATE: %CUR-DATE% >> C:\NOTES\TEMP.NOT ECHO TIME: %CUR-TIME% >> C:\NOTES\TEMP.NOT ECHO. >> C:\NOTES\TEMP.NOT TYPE CON >> C:\NOTES\TEMP.NOT ECHO -------- >> C:\NOTES\TEMP.NOT IF NOT EXIST C:\NOTES\%1.NOT GOTO NAME-RENAME IF EXIST C:\NOTES\%1.NOT GOTO NAME-APPEND :NAME-RENAME REN C:\NOTES\TEMP.NOT %1.NOT GOTO VIEW-EDIT :NAME-APPEND TYPE C:\NOTES\%1.NOT | FIND /V "%1 Note File" >> C:\NOTES\TEMP.NOT MOVE /Y C:\NOTES\TEMP.NOT C:\NOTES\%1.NOT GOTO VIEW-EDIT

:NOTE-TODAY :: Current Date Section :: (This Section Creates or Appends a Note File by Current Date) :: ############ C:\DOS\XSET FILE-DATE DATE YY-MM-DD C:\DOS\XSET CUR-DATE DATE C:\DOS\XSET CUR-TIME TIME ECHO %CUR-DATE% Note File > C:\NOTES\TEMP.NOT ECHO. >> C:\NOTES\TEMP.NOT ECHO TIME: %CUR-TIME% >> C:\NOTES\TEMP.NOT ECHO. >> C:\NOTES\TEMP.NOT TYPE CON >> C:\NOTES\TEMP.NOT ECHO -------- >> C:\NOTES\TEMP.NOT IF NOT EXIST C:\NOTES\%FILE-DATE%.NOT GOTO TODAY-RENAME IF EXIST C:\NOTES\%FILE-DATE%.NOT GOTO TODAY-APPEND :TODAY-RENAME REN C:\NOTES\TEMP.NOT %FILE-DATE%.NOT GOTO VIEW-EDIT :TODAY-APPEND TYPE C:\NOTES\%FILE-DATE%.NOT | FIND /V "%1 Note File" >> C:\NOTES\TEMP.NOT MOVE /Y C:\NOTES\TEMP.NOT C:\NOTES\%FILE-DATE%.NOT GOTO VIEW-EDIT :VIEW-EDIT CLS ECHO. ECHO. ECHO. ECHO ECHO. ECHO ECHO. ECHO ECHO.

View Note ? V Edit Note ? E Quit ? Escape

CHOICE /C:VE<- /N > NUL IF ERRORLEVEL 3 GOTO END IF ERRORLEVEL 2 GOTO EDIT-NOTE IF ERRORLEVEL 1 GOTO VIEW-NOTE GOTO END :VIEW-NOTE IF "%1" == "" CALL C:\BATCH\PCTV C:\NOTES\%FILE-DATE%.NOT IF NOT "%1" == "" CALL C:\BATCH\PCTV C:\NOTES\%1.NOT CLS ECHO. ECHO. ECHO. ECHO ECHO. ECHO ECHO.

Edit Note ? E Quit ? Escape

CHOICE /C:E<- /N > NUL IF ERRORLEVEL 2 GOTO END

IF ERRORLEVEL 1 GOTO EDIT-NOTE GOTO END :EDIT-NOTE IF "%1" == "" C:\DOS\EDIT C:\NOTES\%FILE-DATE%.NOT IF NOT "%1" == "" C:\DOS\EDIT C:\NOTES\%1.NOT CLS :END IF EXIST C:\NOTES\TEMP.NOT DEL C:\NOTES\TEMP.NOT SET FILE-DATE= SET CUR-DATE= SET CUR-TIME= ________

(Note that ` <- ' represents the "Escape" character. To create it, press ^P (Control-P) in DOS Edit, then the "Escape" key. Other text editors allow one to create the "Escape" character by pressing ^V first.)

Explanation
Basically, this file works as do the previous versions. It creates a temporary Note file from a name you give at the command line or from the current date. Using that information, an appropriate title is placed at the start of the file. You then type your text and exit. This text is placed below the title and if no existing Note file has the same name, the temporary Note file is renamed to the given name or to the current date. Then, choices are placed on the screen as to whether you wish to view or edit the file you just created. If you choose "View", after viewing the note, the choice to edit it is given again. After either edit, the batch file ends, upon which, any temporary note files are deleted and variables removed from memory. Now, when a note file already exists with the name you gave or with the current date, the batch file will branch to an "Append" section. Here, the existing note file is piped through the DOS "FIND" command. The " /V " switch tells FIND to locate all lines not containing the title string. So it finds everything except for the title. These non-title lines of the existing note file are appended via " >> " to the just-written TEMP.not file, which already has the same title and your new text. The result is that the old notes are placed below the title and new text, which is the same as if the new text was placed between the title and the old text. Finally, the temporary note file is renamed to the existing note file's name via the DOS "MOVE" command, effectively replacing the old file with the new, updated one. The "/Y" tells DOS to overwrite the existing file with no prompt asking you to choose to overwrite or not. This makes the last step automatic.

Usage
Simply type "NOTET" with, or without, a name. After entering the information, a new file will be created. Conversely, any existing one will have its old information placed into the new note file and be overwritten. This overwritten file will contain the title and all information, both new and old. If you don't want the bottom-append versions of this batch file, you may rename this improved file as simply "NOTE.bat". If you want the combined make-view-edit version but prefer bottom appending, you may eliminate the append-to-the-top parts of this last version and any lines pointing to them. Then, the batch file will work as does the previous NOTE.bat whereby the new information is placed underneath the old, but you will now have the view-edit options built in.

COPY FILES IN ORDER


This basic batch file can be expanded to do a number of sequential file chores. The basic version simply copies the same file as a number of sequentially-numbered files. For this example, generic directories and file names will be used. You may substitute your own. Afterward, an example will be shown which can do a more complicated chore.

SEQ.bat
:: SEQ.bat :: Copies Files in Sequential Order :: Makes 4 Copies :: @ECHO OFF SET NUMBER=1 :AGAIN COPY C:\DOCS\TEST.DOC C:\TEMP\TEXT-%NUMBER%.DOC > NUL XSET /MATH NUMBER=%NUMBER% + 1 IF NOT "%NUMBER%" == "5" GOTO AGAIN ECHO. ECHO ECHO. 4 .doc File Copies Created

CALL C:\BATCH\DR C:\TEMP\*.DOC :END SET NUMBER= ________

Here, a variable called "NUMBER" is created to equal the number `1'. Next, the TEST.doc file is copied to the TEMP directory as a new file called "TEXT-1.doc". Note that the new name uses the NUMBER variable. DOS will substitute the assigned value here; thus, since NUMBER equals `1', the file name will be "TEXT-1.doc". The COPY command's message is sent to "NUL", so it is never seen on screen. XSET's "MATH" switch is then used to reset "NUMBER" to be NUMBER plus 1. Since NUMBER was equal to `1', it is now 1 + 1, or a value of `2'. This value is next compared to the preset value of `5'. Since they are not equal, the copy command is run again. However, this time, NUMBER is equal to `2' because it just got re-set. Thus, TEST.doc is now again copied to the TEMP directory, but as "TEXT2.doc". This continues until NUMBER is equal to `5' which results in "GOTO AGAIN" being ignored and the succeeding line in the batch file being read instead. Here, the batch file prompts that 4 files have been created, with blank lines showing above & below that prompt. A directory listing proves the files were created. The NUMBER variable is then removed from memory.

OK. So that's the basic idea. Now, here's how these techniques may be used for a real-world purpose. Let's say you have composed a number of differently named document files for the purpose of a project. Your project is complete but you wish to have the instructional documents be called "MANUAL-1.doc", "MANUAL-2.doc", "MANUAL-3.doc", etc. Here's a method of doing that:

PROJ-SEQ.bat
:: PROJ-SEQ.bat :: Renames Files in Sequential Order :: @ECHO OFF IF "%1" == "*TASKS*" GOTO TASKS IF NOT EXIST C:\PROJECT\WORK\NUL MD C:\PROJECT\WORK SET NUMBER=1 :COPY-PROJ FOR %%F IN (C:\PROJECT\*.DOC) DO CALL C:\BATCH\PROJ-SEQ.bat *TASKS* %%F GOTO DISPLAY :TASKS COPY %2 C:\PROJECT\WORK\MANUAL-%NUMBER%.DOC > NUL XSET /MATH NUMBER=%NUMBER% + 1 GOTO END :DISPLAY ECHO Y | DEL C:\PROJECT\*.DOC > NUL MOVE C:\PROJECT\WORK\*.* C:\PROJECT > NUL CALL C:\BATCH\DR C:\PROJECT RD C:\PROJECT\WORK SET NUMBER= :END ________

Explanation
Ignore the first "IF" statement for now. The next line creates a PROJECT\WORK directory, if one does not exist. The newly-named files will be placed in here temporarily. This creates extra steps, but is done in case one or more of the original files happens to have a name the same as one to be created. If you don't feel you are going to run into this problem, you can shorten the batch file by eliminating all references to the WORK directory and changing the COPY command to a REN (Rename) one. Next, an environment variable called "NUMBER" is set to a value of `1'. Now, a "FOR" command says that FOR each .doc File IN the C:\PROJECT directory, DO call this batch file again, but with "*TASKS*" as the first parameter. So now, that first IF statement is true, and the batch file skips to the "TASKS" section. Here, the 2nd parameter is copied to PROJECT's work directory. That second parameter is the first file selected by "FOR". It has substituted that first file's name for "%%F". This first file is copied under a new name called "MANUAL-(value).doc. The "value" is determined by the value of NUMBER. Recall that it was set to`1', near the start of the batch file when it was first run. So, this time through, the file will be named "MANUAL-1.doc" because the NUMBER variable was substituted with `1'.

Next, XSET's "MATH" switch is used. It re-sets NUMBER to be NUMBER plus 1. Since NUMBER had been 1, it is now 1 + 1, or `2'. The second run-through of the batch file then ends. Now, since "FOR" is not finished from the first run-through, it takes the next file in the PROJECT directory, and does the same thing; except since NUMBER now has a value of `2', the file will be named "MANUAL-2.doc". XSET then re-sets NUMBER to `3', and the process continues to repeat until no .doc files are left in the PROJECT directory. The batch file is then directed to the DISPLAY section via the line after the "FOR" one. In the DISPLAY section, the original PROJECT .doc files are deleted and the new MANUAL files are moved into the PROJECT directory. A directory listing shows that the procedure has been completed. The WORK directory is deleted and the NUMBER variable removed from memory to clean up.

Usage
Simply, issue "PROJ-SEQ" and the files will be renamed in a numerical order. By changing the batch file's parameters, you may select all, or any type of files in any directory and name them to anything you wish. This is, of course, provided you don't use illegal file name characters, or exceed the file name & extension size limits. To use this with other files, substitute your selected file and directory names in the above batch file. Be aware that the file order will be that of the unordered directory. So the first file will be the oldest one created. However, it will be the last file if that oldest-created file was the most recent to be modified. This is because DOS places files in a directory by creation, and a modified file is deemed to be created on the date it was modified. In the future, I may show a version that will number the files based on age, alphabetical order, size, or whatever. XSET may be obtained from -xset.tripod.com.

TRANSFER FILES
Here is a direct way for you to move one or more files from the current directory to any other directory of choice regardless of its drive or level below the root - without having to type a drive letter or path!

TRAN.bat
:: TRAN.bat :: Transfers All or Selected Files or File Groups :: to the Directory of Choice :: :: The Destination Directory Must be able to be Accessed via a Batch File. :: If such a Batch File does not Exist, use a Directory Change :: Program such as "Directory Maven" or Norton's "CDD". :: :: This Program Must be used from within a Batch File to Work Here. :: @ECHO OFF F:\DOS\XSET S-DRIVE FDRIVE . F:\DOS\XSET S-DIRECTORY DIR

IF "%1" == "" GOTO TRAN-ALL IF NOT EXIST %1 GOTO NO-FILE IF NOT "%1" == "" GOTO TRAN-SPEC :TRAN-ALL SET S-FILES=*.* GOTO DESTINATION :TRAN-SPEC SET S-FILES=%1 :DESTINATION ECHO. ECHO. F:\DOS\XSET /COLOR LIGHTGREEN /PROMPT " CALL %DESTINATION% :MOVE-FILES F:\DOS\MOVE %S-DRIVE%\%S-DIRECTORY%\%S-FILES% . SHIFT IF NOT "%1" == "" SET S-FILES=%1 IF NOT "%1" == "" GOTO MOVE-FILES :CHANGE-DIR F:\DOS\XSET CUR-DIR FDIR . ECHO. ECHO. ECHO The Current Directory is: ECHO %CUR-DIR% ECHO. ECHO Return to: %S-DRiVE%\%S-DIRECTORY%? ECHO. CHOICE > NUL IF ERRORLEVEL 2 GOTO CLEANUP :YES CLS %S-DRIVE% CD\%S-DIRECTORY% GOTO CLEANUP :NO-FILE ECHO. ECHO. ECHO ECHO. GOTO END

Destination?

" Destination

Y?

N?

File "%1" does not Exist

:CLEANUP F:\DOS\XSET /CLEAR F:\DOS\XSET /LOAD < C:\DOS\DOSPLUS\XSET\ENVIRO.DEF CLS F:\BATCH\DR :END ________

Explanation
XSET is used here to get the source directory and its drive into the variables "S-DIRECTORY" and "SDRIVE". Note that `.' represents the current directory and it is used by XSET to determine that directory's drive.

Next, the file looks to see if there are any parameters given when TRAN was issued at the command line. If there are none, the file branches to TRAN-ALL (Transfer All Files). If there is at least one parameter, its existence is ascertained and the file branches to TRAN-SPEC (Transfer Specified File). If there is a parameter (file name) but it doesn't exist, a message stating so is displayed on the screen as instructed in the NO-FILE section. After this, the batch file ends. Otherwise, the batch file goes to the TRAN-SPEC section. In either the TRAN-ALL or TRAN-SPEC section, S-FILES (Source Files) is set to equal either all files or the first file on the command line depending on to which section the batch file has branched. Now, XSET is used to display a coloured prompt on the screen requesting a destination for the chosen files. The user's answer (input) is made into a variable called "DESTINATION". This must be a batch file because the next line calls this batch file the user chose as DESTINATION and that is used to make the change to the required directory. So if one has a `DL' shortcut to the C:\DOWNLOAD directory, that would be entered here. XSET will make DESTINATION equal to "DL". The next line now translates to "CALL DL". Since `DL' is a batch file name, that batch file will be run and the user taken to the C:\DOWNLOAD directory. After this, control is returned to TRAN.bat starting at the MOVE-FILES section (the line after the "CALL" command). The batch file continues to execute from there. If a batch file does not exist for the target directory and you don't wish to make one, use a directorychange program and have it controlled via a batch file. Here is the batch file I use to have Directory Maven make the change.
:: DM.bat :: Runs Directory Maven :: @ECHO OFF C:\DOS\DOSPLUS\DIR-MAVE\DM.EXE %1

This is a simple file that runs the Directory Maven (DM) executable from its own directory. It looks at the `%1' parameter filled in by the user when XSET asked for the Destination. "DM DOS" might have been typed, so XSET makes "DESTINATION" equal to "DM DOS". The next line now translates to "CALL DM DOS" and the `DM' batch file runs with `DOS' as its parameter, so it takes the user to that directory. Now we get to the actual transfer. The "MOVE-FILES" section moves the requested drive\directory\files of the source to the current directory, which is the one to which we changed via the batch file or by Directory Maven. Next, the SHIFT command shuffles down by a factor of `1' all the parameters specified when TRAN was first issued. So what was Parameter `2' (%2) is now Parameter `1' (%1), Parameter `3' is now `2', and so on. The purpose of this is to accommodate any additional files that were first given on the command line that still have not been moved. The next two lines in this section say that if there is a `%1' parameter, reset S-FILES to equal the new `%1' file name and to move it to the target directory. This sequence continues until all specified files are moved. Now, XSET is once again used to get the current directory. It is used in the on-screen display to state what the current directory is and to ask if the user wishes to go back to the source directory which had already been set earlier as "S-DIRECTORY". CHOICE is employed to determine if the user pressed `N'. If so, the file branches to "CLEANUP". If not, it continues through the "YES" section. There, the user is returned to the source drive and directory. In "CLEANUP", XSET restores the environment to its default, effectively removing all variables set during this batch file from memory. This is done by first clearing all variables and then by loading the default environment from a previously written text file I have called "ENVIRO.DEF". Finally, a directory

listing confirms the operation whether the user has chosen to remain in the target directory or return to the source directory.

Usage
Simply issue "TRAN" to move all files. A prompt will ask for a destination. Use a batch file to select a directory or to run a directory-change program that will change to the target directory for you. Then answer the question as to whether to return to the source directory or not. To move selected files or file groups, list them after "TRAN". So to move two different files, issue "TRAN FILE-1.EXT FILE-2.EXT". You may also use wildcards: "TRAN *.TXT 1000????.jpg". One may make TRAN.bat a copy batch file by replacing the MOVE command with an XCOPY one. If you wish to have both, name the COPY version "TRANC.bat". Alternatively, one could add another CHOICE section to the TRAN.bat shown here that asks the user if he wishes to copy or Move the files. I personally wouldn't like that because it would add extra steps for me every time I ran the operation. I would rather type "TRAN" or "TRANC" as required, thus skip an extra prompt and key issuing. Finally, if you always want to stay in the target directory, eliminate the CHOICE section and CHANGE-DIR steps. If you prefer to always return to the source, eliminate the CHOICE section and keep just the CHANGE-DIR steps required to return you to the source.

XSET may be obtained from: -xset.tripod.com A directory change program is available at: -Directory Maven

You might also like