logo

Batch files

What are batch files

Batch files are script files in DOS, OS/2, and Microsoft Windows. They consist of a series of commands that are executed by the command-line interpreter, stored in a plain text file. The term "batch" is derived from the batch processing, meaning non-interactive execution. The file extension for batch files is .bat.

Where to use batch files

Batch files are typically used in Windows operating systems. They're suitable for repetitive tasks in personal computing but are especially powerful in network environments where the same tasks need to be executed on multiple machines.

When to use batch files

Automation of routine tasks

If you have a sequence of commands that you often execute manually, you can put them in a batch file and run them with a single command. This can include tasks like copying files, deleting temporary files, or starting multiple applications at once.

System maintenance and administration

System administrators can use batch files to change settings, run diagnostics, manage files, and more, across multiple systems at once.

Deployment of software and configuration

Batch files can automate the process of installing and configuring software across various workstations in a network.

Scheduled tasks

You can use Task Scheduler in conjunction with batch files to run tasks at specific times or under specific conditions.

How to create and use batch files

  1. Open a text editor like Notepad. Write the commands you want to be executed, line by line, then save the file with the .bat extension.
  2. Use any command that you can run from the command prompt.

You can schedule batch files to run at specific times using Windows Task Scheduler.

Caution

Batch files are powerful tools and can make significant changes to a system. It's important to have a clear understanding of the commands you're using, especially if you are running batch files on production systems or networked computers, as incorrect commands can cause data loss or other serious issues.

Group 1: General utilities

Intro

Backup Files and Folders

You can create a batch file to back up important documents or entire folders to a specific location, such as an external hard drive or a network share.


@echo off
xcopy "C:\path\to\source\folder" "D:\path\to\backup\folder" /D /E /I /Y
echo Backup complete!
pause

Clean Temporary Files

You can automatically delete temporary files, browser cache, and other non-essential files to free up disk space.


@echo off
del /s /f /q "C:\Windows\Temp\*.*"
del /s /f /q "C:\Users\%username%\AppData\Local\Temp\*.*"
echo Temp files deleted!
pause

Shutdown or Restart the Computer

You can create a batch file to shutdown or restart the computer after a certain time or immediately.


@echo off
shutdown -s -t 60
echo Computer will shut down in 60 seconds!
pause

Start Multiple Applications at Once

Open multiple applications or websites simultaneously with a single click.


@echo off
start "" "C:\Program Files\YourApp1\app1.exe"
start "" "C:\Program Files\YourApp2\app2.exe"
start "" "https://www.jb11.org"
echo Applications started!
pause

Automate Network Pings

Regularly check the connection to important servers or websites by pinging them.


@echo off
:begin
ping google.com -n 4
timeout /t 60
goto begin

Disk Cleanup

Automate the Windows Disk Cleanup tool to run regularly.


@echo off
cleanmgr /sagerun:1

Quick IP Configuration View

Quickly view your IP configuration by running a batch file.


@echo off
ipconfig /all
pause

Check Website Availability

You can create a batch file to ping a website and notify you if it's down.


@echo off
ping -n 1 https://www.jb11.org > NUL
if %errorlevel% == 0 (
  echo The website is up!
) else (
  echo The website is down!
)
pause

Open Websites Regularly Checked

Create a batch file to open websites you regularly check in your default browser.


@echo off
start "" "http://www.example.com/search?query=your+name"
start "" "http://www.news-site.com/topic/your-field"
echo Websites opened!
pause

Group 2: Text wrangling

Here are some batch files that might be useful for students working on corpus or computational linguistic projects.

Automated Data Collection from Text Files

You can combine text files from various locations into a single file, which might be helpful for corpus creation.


@echo off
copy C:\path\to\text\files\*.txt C:\path\to\combined\combined.txt
echo Files combined!
pause

Automate Downloads

If you use wget or a similar tool, you can automate the download of files.


@echo off
wget -i C:\path\to\list\of\urls.txt -P C:\path\to\downloads
echo Downloads complete!
pause

Organize Downloads into Folders by File Type

Move downloaded files into specific folders based on their file type.


@echo off
move "C:\path\to\downloads\*.pdf" "C:\path\to\PDFs"
move "C:\path\to\downloads\*.csv" "C:\path\to\CSVs"
echo Files organized!
pause

Automate Folder Cleaning

Delete files older than a certain number of days in a specific folder (e.g., 30 days).


@echo off
forfiles -p "C:\path\to\folder" -s -m *.* -d -30 -c "cmd /c del @path"
echo Old files deleted!
pause

Convert Text Files to a Specific Encoding

Convert ASCII text files to UTF-8 to reduce encoding issues.


@echo off
for %%i in (C:\path\to\files\*.txt) do (
  iconv -f ASCII -t UTF-8 "%%i" -o "%%i"
)
echo Encoding conversion complete!
pause

Batch Renaming of Files

Rename files with a specific pattern, which might be helpful when organizing your datasets.


@echo off
for %%A in (C:\path\to\files\oldprefix*.txt) do (
  set "name=%%~nA"
  rename "%%A" "newprefix%%~xname:~10%%~xA"
)
echo Files renamed!
pause

Create a Log File of System Activities

Keep a log of system activities, which might be helpful in tracking data processing tasks.


@echo off
echo %date% %time% >> C:\path\to\log.txt
echo Task performed >> C:\path\to\log.txt
pause

Sync Files and Folders

If you need to keep certain folders synchronized with a network location or external drive, you can use a command-line tool like Robocopy.


@echo off
robocopy "C:\path\to\source" "\\server\share\destination" /MIR

Misc.

Start a Local Server

If you're developing and need a local server running, you can start it automatically.


@echo off
cd "C:\Path\To\Server"
start yourservercommand

Backup Files

Perform a daily backup of important files when you start your computer.


@echo off
xcopy "C:\path\to\important\files" "D:\path\to\backup" /D /E /I /Y

Open Websites

Automatically open websites you check daily.


@echo off
start "" "http://www.website.com"

Custom Pop-up Message

You can use the msg command to create a pop-up message in a batch file that will display when the computer starts up.


@echo off
msg * "Good morning, How can I get rid of this pop-up?"

To make this script run at startup, follow these steps: (1) Save the above script as a .bat file, e.g., morning_message.bat. (2) Press Win + R, type shell:startup, and hit Enter. (3) Create a shortcut to the .bat file in the startup folder that opens.

Custom Greetings or Notifications

You can display a custom message or reminder every time your computer starts.


@echo off
echo Good morning, Professor! Don't forget to drink coffee.
pause

To run a batch file at startup, you would generally place a shortcut to the batch file in the startup folder (C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp), or use a task scheduler to set it to run on logon.