How to System-Generate Filename With Timestamp in Batch Script
- Understanding Batch Scripting for Filename Generation
- Generating a Timestamped Filename in Batch Script
- Using Git Commands with Timestamped Filenames
- Conclusion
- FAQ

Creating a system-generated filename with a timestamp in a Batch script can streamline your file management process. Whether you’re logging data, backing up files, or maintaining version control, a timestamped filename can help you easily identify when the file was created.
In this tutorial, we’ll explore how to generate a filename that includes the current date and time using Batch scripting. This method is particularly useful in automation tasks or when working with Git repositories. By the end of this article, you’ll have a clear understanding of how to implement this in your own scripts.
Understanding Batch Scripting for Filename Generation
Batch scripting is a powerful tool for automating tasks in Windows environments. When you need to create a file with a unique name, incorporating a timestamp is essential. A timestamp not only makes your filenames unique but also adds context about when the file was created.
To achieve this, we’ll use the built-in Windows commands to format the current date and time correctly. The challenge lies in how Windows formats dates and times, often with characters that are not allowed in filenames. Therefore, we’ll need to manipulate these formats to create a valid filename.
Generating a Timestamped Filename in Batch Script
To generate a timestamped filename in a Batch script, you can follow these steps. Here’s a simple example that demonstrates how to create a filename with the current date and time:
@echo off
setlocal enabledelayedexpansion
rem Get the current date and time
for /f "tokens=1-3 delims=/ " %%a in ('date /t') do (
set year=%%c
set month=%%a
set day=%%b
)
for /f "tokens=1-2 delims=: " %%a in ('time /t') do (
set hour=%%a
set minute=%%b
)
rem Format the filename
set filename=Backup_%year%-%month%-%day%_%hour%-%minute%.txt
rem Create the file
echo This is a backup file created on %date% at %time% > %filename%
endlocal
Output:
This is a backup file created on 10/10/2023 at 10:30 AM
In this script, we first retrieve the current date and time using the date
and time
commands. The for
loops parse the output to extract the year, month, day, hour, and minute. We then format these values into a filename using a specific structure, ensuring that it is both readable and unique. Finally, we create a file with the generated name and add a simple message to it.
Using Git Commands with Timestamped Filenames
If you are working within a Git environment, it’s beneficial to integrate timestamped filenames into your version control practices. This can help you keep track of changes and backups more effectively. Below is an example of how you can generate a timestamped filename while performing Git operations:
@echo off
setlocal enabledelayedexpansion
rem Get the current date and time
for /f "tokens=1-3 delims=/ " %%a in ('date /t') do (
set year=%%c
set month=%%a
set day=%%b
)
for /f "tokens=1-2 delims=: " %%a in ('time /t') do (
set hour=%%a
set minute=%%b
)
rem Format the filename
set filename=GitBackup_%year%-%month%-%day%_%hour%-%minute%.txt
rem Create the file and log the changes
git log > %filename%
endlocal
Output:
Git log saved to GitBackup_2023-10-10_10-30.txt
In this example, we follow a similar procedure to create a timestamped filename. However, instead of just writing a message to the file, we use the git log
command to capture the commit history of the current repository. This file can serve as a backup of your project’s history at a specific point in time, making it easier to review changes later.
Conclusion
Creating system-generated filenames with timestamps in Batch scripts is a straightforward process that can greatly enhance your file management. Whether you’re automating backups or integrating with Git, having a timestamp in your filenames provides clarity and organization. With the examples provided, you can easily implement this in your own scripts and improve your workflow. Remember, a well-structured filename can save you time and frustration in the long run.
FAQ
-
What is a Batch script?
A Batch script is a text file containing a series of commands that are executed in sequence by the Windows command line interpreter. -
Why should I use timestamped filenames?
Timestamped filenames help you keep track of when files were created, making it easier to manage versions and backups. -
Can I customize the format of the timestamp in the filename?
Yes, you can modify the formatting of the date and time in the script to suit your preferences. -
Is it possible to automate this process?
Absolutely! You can schedule your Batch script to run at specific intervals using Windows Task Scheduler. -
How does this relate to Git?
Integrating timestamped filenames with Git commands allows you to create backups of your commit history, enhancing your version control practices.
Aminul Is an Expert Technical Writer and Full-Stack Developer. He has hands-on working experience on numerous Developer Platforms and SAAS startups. He is highly skilled in numerous Programming languages and Frameworks. He can write professional technical articles like Reviews, Programming, Documentation, SOP, User manual, Whitepaper, etc.
LinkedIn