A batch file is a script file which contains series of commands that get executed one after the other, in a sequence. A batch file is created in a simple plain text file (Notepad or WordPad) with .bat extension and is executed by the command-line interpreter (cmd.exe known as Command Prompt in Windows).
They are usually created to automate jobs that need to be done periodically such as deleting files or logs to free up disk space, backups on a daily/monthly/weekly basis, testing or checking a list of active/inactive users or addresses in network, creating daily/weekly/monthly reports with information about disk usage, file sizes etc… In a nutshell, the main functionality of these kinds of scripts is to avoid writing the same code over and over again and to automate tasks and ease the work required for certain regular tasks.
If you periodically do something in the same way, it should be automated.
This article demonstrates how to create a simple batch script which is deleting unnecessary log files on the server (to make more space on the disk), and set up a scheduler task to execute this script automatically (Windows Server 2012 in this article).
1. Create batch script:
Open a text editor such as Notepad and add commands below.
cd C:\alfresco-communitydel alfresco.log.2*exit
The cd command is a “change directory” command that is used the change the current working directory. In this case the directory would be changed to C:\alfresco-community where the logs are being saved. The command del is used to delete one or more files or directories from a file system. Since all logs in C:\alfresco-community directory are saved as “alfresco.log.2*”, this command is deleting all the files named as “alfresco.log.2*”. Wildcard or * character replaces all the characters that come after number 2 in this case. This means that this command can delete more than one file at a time, so the * character should be used cautiously.
To delete all the files in a folder called “logs” you can use the following delete commands
Once commands are written in Notepad, they should be saved with .bat extension.
Batch script is now created and it will be executed by double-clicking the file. Good practice is to test (execute) the new batch script before creating a task scheduler.
2. Create task scheduler
Task Scheduler allows us to create and run tasks, programs or scripts automatically at pre-defined times, when an event occurs or whenever the condition is met.
To create a basic task go to Start → Administrative Tools → Task Scheduler → Create Basic Task and type the Name of the task (“delete logs” in this case).
The next step is Trigger sections which enables selecting if a task is going to be triggered daily, weekly, monthly, or at a specific time. Once a task is set up to be triggered at a specific time, Action needs to be selected as well. The Action section gives an option to choose what action needs to be done in order to perform a task. We want to execute the new created batch script, so “Start a program” option needs to be selected.
Once “Start a program” is selected we will browse for the batch script as follows:
This is the last step in the wizard. After clicking “Next” the task will be created and can be visible in the task scheduler library together with other tasks.
Thank you for reading.