A backup folder is a folder that contains copies of important files and data that can be used to restore them in case of loss or damage. Creating a backup folder with the current date in the name is a useful way to organize and manage your backups. In this article, you will learn how to create a backup folder with the current date in the format of MM-DD-YYYY using a batch script.
Table of Contents
What is a Batch Script?
A batch script is a text file that contains a series of commands that can be executed by the Windows command interpreter, also known as the command prompt or cmd.exe. A batch script can perform various tasks, such as launching programs, copying files, deleting folders, and more. A batch script usually has a .bat or .cmd extension and can be run by double-clicking on it or typing its name in the command prompt.
How to Get the Current Date in Batch Script?
One of the challenges of creating a backup folder with the current date in batch script is that the date format may vary depending on the system settings and the regional preferences of the user. For example, some users may have their date displayed as DD/MM/YYYY, while others may have it as MM/DD/YYYY or YYYY/MM/DD. Therefore, we need a reliable way to get the current date in the desired format of MM-DD-YYYY regardless of the system settings.
There are several methods to get the current date in batch script, but one of the simplest and most robust ones is to use the WMIC command. WMIC stands for Windows Management Instrumentation Command-line, and it is a tool that allows you to access and manipulate various system information and settings. One of the features of WMIC is that it can return the current date and time in a standard format of YYYYMMDDHHMMSS, regardless of the regional settings. For example, if you type the following command in the command prompt:
wmic os get LocalDateTime
You will get an output like this:
LocalDateTime
20211118040251.500000+000
The first eight digits of the output represent the current date in the format of YYYYMMDD, which is exactly what we need. To extract the date from the output, we can use the FOR /F command, which allows us to parse and process the output of another command. For example, if we type the following command in the command prompt:
for /f "skip=1 tokens=1 delims=." %a in ('wmic os get LocalDateTime') do echo %a
We will get an output like this:
20211118040251
The FOR /F command skips the first line of the output, which is the header, and then assigns the first token of the remaining line, which is the date and time, to the variable %a. The delims option specifies that the dot (.) is the delimiter that separates the tokens. The echo command prints the value of the variable %a to the screen.
Now that we have the current date in the variable %a, we can use the substring operator to extract the month, day, and year from it. The substring operator uses the syntax %variable:~start,length%, where start is the zero-based position of the first character to extract, and length is the number of characters to extract. For example, if we type the following commands in the command prompt:
set month=%a:~4,2%
set day=%a:~6,2%
set year=%a:~0,4%
echo %month%-%day%-%year%
We will get an output like this:
11-18-2021
The set command assigns the extracted substrings to the variables month, day, and year, respectively. The echo command prints the formatted date to the screen.
How to Create a Backup Folder with the Current Date in Batch Script?
Now that we have the current date in the format of MM-DD-YYYY, we can use it to create a backup folder with that name. To do that, we can use the MD command, which creates a new folder with the specified name. For example, if we type the following command in the command prompt:
md backup-%month%-%day%-%year%
We will create a new folder named backup-11-18-2021 in the current directory. Alternatively, we can specify a different location for the backup folder by providing the full path. For example, if we type the following command in the command prompt:
md C:\Users\user\Documents\backup-%month%-%day%-%year%
We will create a new folder named backup-11-18-2021 in the C:\Users\user\Documents directory.
To automate the process of creating a backup folder with the current date, we can combine all the commands we have used so far into a single batch script. For example, we can create a text file named backup.bat with the following content:
@echo off
for /f "skip=1 tokens=1 delims=." %%a in ('wmic os get LocalDateTime') do set date=%%a
set month=%date:~4,2%
set day=%date:~6,2%
set year=%date:~0,4%
md C:\Users\user\Documents\backup-%month%-%day%-%year%
echo Backup folder created successfully.
The @echo off command suppresses the display of the commands on the screen. The double percent signs (%%) are used to indicate that the variable %a is used in a batch script, not in the command prompt. The rest of the commands are the same as before, except that we have used a different variable name (date) to avoid confusion with the DATE environment variable. The last command prints a confirmation message to the screen.
To run the batch script, we can either double-click on the backup.bat file or type its name in the command prompt. The script will create a backup folder with the current date in the specified location and display the confirmation message.
Frequently Asked Questions (FAQs)
Question: How can I copy files to the backup folder?
Answer: You can use the COPY or XCOPY commands to copy files to the backup folder. For example, if you want to copy all the files from the C:\Users\user\Pictures directory to the backup folder, you can add the following command to the end of the backup.bat script:
xcopy C:\Users\user\Pictures C:\Users\user\Documents\backup-%month%-%day%-%year% /s /e /i /y
The xcopy command copies files and folders, including subfolders. The /s option copies subdirectories, except empty ones. The /e option copies subdirectories, including empty ones. The /i option creates the destination folder if it does not exist. The /y option suppresses the prompt to confirm overwriting existing files.
Question: How can I schedule the backup script to run automatically?
Answer: You can use the Task Scheduler tool to schedule the backup script to run automatically at a specified time or frequency. To do that, follow these steps:
- Open the Task Scheduler by typing taskschd.msc in the Run dialog box or the Start menu search box.
- Click on Create Basic Task in the Actions pane on the right.
- Enter a name and a description for the task and click Next.
- Choose when you want the task to start and click Next.
- Specify the time or frequency of the task and click Next.
- Choose Start a program as the action and click Next.
- Browse to the location of the backup.bat file and select it as the program to start and click Next.
- Review the settings of the task and click Finish.
The task will be created and run according to the schedule you have specified.
Question: How can I modify the date format of the backup folder?
Answer: You can modify the date format of the backup folder by changing the order and the separators of the month, day, and year variables in the MD command. For example, if you want the date format to be YYYY-MM-DD, you can change the MD command to:
md C:\Users\user\Documents\backup-%year%-%month%-%day%
If you want the date format to be MMDDYYYY, you can change the MD command to:
md C:\Users\user\Documents\backup-%month%%day%%year%
Summary
In this article, you have learned how to create a backup folder with the current date in the format of MM-DD-YYYY using a batch script. You have also learned how to get the current date in batch script using the WMIC command, how to extract the month, day, and year from the date using the substring operator, and how to create a new folder using the MD command. Additionally, you have learned how to copy files to the backup folder using the XCOPY command, how to schedule the backup script to run automatically using the Task Scheduler tool, and how to modify the date format of the backup folder by changing the MD command.
Disclaimer: The information provided in this article is for educational purposes only and does not constitute professional advice. The author and the publisher are not liable for any errors or omissions, or for any consequences arising from the use of this information. Always consult a qualified IT professional before making any changes to your system or data.