Computer Active (UK)

Automate repetitive tasks with a batch file

What you’ll need: Windows Notepad Time required: 20 minutes

-

Windows is a user-friendly interface for what is essentiall­y an operating system of text-based commands – as you’ll know if you’ve ever used Command Prompt. In this Workshop, we’ll show you how to create a batch file that contains commands that automate certain tasks on your PC – here we’ll empty the Recycle Bin and shut down your PC at a minute past midnight every night. Visit Microsoft’s site ( www.snipca. com/30197) for a full list of commands to automate other tasks on your PC.

Open Notepad. If it doesn’t appear on the Windows menu, type notepad into the search box and choose the top result. By default, everything you write in a batch file will be displayed in the Command Prompt window when the task is being carried out, so type @echo off 1 in the first line and press Return to stop the commands being displayed. Although we’re hiding the commands, it’s still useful to tell anyone who sees the batch file run what’s happening so they don’t worry that it’s a virus up to no good. On the next line, type title Going to bed… 2 . This displays ‘Going to bed’ in the Command Line window’s title bar when the batch file is executed.

On the next line, type echo Taking out the trash… 1 , then press Return. The ‘echo’ command displays whatever follows it in Command Prompt (so it will display ‘Taking out the trash’).

In this step we’ll empty the Recycle Bin, (which lives at C:\$recycle.bin), so if you tend to store anything there that you may want to retrieve later, this would be a good time to stop doing that. Windows will always ask you to confirm that you mean to empty the bin before it deletes any files. The need for confirmati­on would stall our batch file until we intervened manually, which misses the point of automating the process. So, type the command rd /s /q c:\$recycle.bin 1 in the next line (this deletes the bin’s contents without checking first). rd stands for remove directory.

To set your PC to switch off once the bin has been emptied, type Shutting down... 1 (or similar) on the next line (to display that message when the task is executed), then shutdown /s 2 on the next line. If you changed the ‘/s’ to ‘/l’ it would instead just log out the current user, but leave the computer running. Although this wouldn’t save any energy, it is still a sensible security measure as it would secure the machine when it’s unattended.

Finally, you need to set up your batch file to run automatica­lly each day, without any input from yourself. Type Schedule Tasks into the Windows search box and choose the top result. Click ‘Create basic task…’ in the sidebar and type Empty trash and shut down in the name box. Click Next, select Daily on the following page, then click Next again. On the third screen, click the date and select tomorrow’s date from the calendar, then set the time box to ‘00:01:00’ 1 for a minute after midnight.

Now we need to save the batch file. Once you get into the habit of setting up routines like this, you’ll probably think of a number of tasks you’d like to automate. So, to keep things tidy, create a new folder in your Documents folder called ‘batch files’, and save your work there. Notepad will want to use the extension ‘.txt’, but batch files need to use the extension ‘.bat’. Select the full contents of the filename box and type shutdown.bat 1 , then click Save, and close Notepad.

Click Next, then select ‘Start a program’ from the list of options, and click Next again. On the final screen, click Browse 1 and select the shutdown.bat file that you saved in the ‘batch files’ folder inside Documents in Step 5. Click Next, followed by Finish. You can now close Task Scheduler and, when a minute past midnight next rolls around, your batch file will run, the Recycle Bin will be emptied, and your computer will shut down.

 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??
 ??  ??

Newspapers in English

Newspapers from United Kingdom