In: Computer Science
. Create a batch file (hw3_yourlastname1.bat) which will schedule tasks on the system using Windows 10’s Scheduled Task Console utility - schtasks
Your batch file (hw3_yourlastname1.bat) will include instructions to do the followings:
a. List the directories and files in the current directory
b. Pause the script to see the list of directories and files
c. Clear the screen
d. Pause the script
e. Schedule a task to start Check Disk (chkdsk.exe) every Friday at 5:30pm using Scheduled Task Console (schtasks) utility
f. Schedule a task to start Disk Defragmenter (dfrgui.exe) every 1st day of a month at 11:30pm using Scheduled Task Console (schtasks) utility
g. Display all the scheduled tasks on the system
h. Pause the script to see the list of scheduled tasks
tep 1: Select and open your editor
As mentioned earlier, text documents are a good starting point for batch scripts. To write your own batch file, all you need is an ordinary text editor. You don’t really need features like syntax highlighting, so the Notepad application included with Windows is perfect. To open it, simply type “Notepad” in the Windows search bar and click on the Notepad icon in the search results:
ou don’t have to learn complicated programming language to create batch files. But you doneed to know common system commands and understand how they work in batch files. That’s why you should familiarize yourself with some commands before writing your first script. Here are the most important commands to learn:
n easy introduction to the art of creating batch files is to write a simple script that creates multiple directories on a selected disk on your computer. For example, if you create and run a batch file with the following input, it will create two directories named “Example1” and “Example2” on drive C:
MKDIR C:\Example1
MKDIR C:\Example2
Simply copy the two lines into an empty Notepad document, as shown in the following screenshot:
To save these batch instructions or the script, click File and choose Save As... Specify the save location and enter a name for the script with the extension .bat in the File Name field:
Step 4: Run the new batch script
After you create and save the batch file, you have two options to run it: Either run the script in the familiar Windows Explorer environment or open Command Prompt and run it using a command-line command.
The first option is simpler and easier for beginners because all you have to do is go to the directory where the batch file is located and double-click to run it.
If you want to open the batch file from the command line instead, do the following:
Step 5: Editing batch files retrospectively
You can customize a batch script at any time, for example if you want to add or remove commands or modify directories. To do this, simply go to the folder containing the command line script and right-click it. Then choose Edit:
Examples of more frequently used and complex batch scripts
With the above step-by-step guide, you can create a wide variety of batch files with an unlimited number of different commands. However, scripts that you can use more frequently are definitely more useful in the long run. We’ll conclude with two examples of batch files with long-term value to illustrate the possibilities of batch processing with Windows Command Prompt.
Batch script with simple backup mechanism
The following example shows how useful batch files can be for creating regular back-ups of any directory:
XCOPY C:\Outgoing directory C:\Back-up-directory /m /e /y
When you create and run a batch file containing the line shown above, use the “xCOPY” command to copy the contents from the “source folder” to the “back-up folder.” You’ll need to adjust the directories containing these two folders accordingly. The three parameters at the end have the following effect:
Batch file with complex backup function
The above backup program allows you to copy the source files from the source folder to the destination folder. You can also create a batch file that distributes source data to multiple destination folders, in which case you can use the file type as a selection criterion. To do this, you need a for loop, which allows a command to run repeatedly with a variable argument:
cd C:\Outgoing directory
FOR %%f IN (*.doc *.txt) DO XCOPY C:\Outgoing directory"%%f" C:\Back-up-directory\Texte /m /y
FOR %%f IN (*.jpg *.png *.bmp) DO XCOPY C:\Outgoing directory "%%f" C:\Back-up-directory\images /m /y
The batch code shown above ensures that: