How do I get current date/time on the Windows command line in a suitable format for usage in a file/folder name?

Cover Image for How do I get current date/time on the Windows command line in a suitable format for usage in a file/folder name?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

How to Get the Current Date and Time in the Windows Command Line

Are you looking for a quick and easy way to get the current date and time in the Windows command line? Well, you've come to the right place! In this guide, we will walk you through a simple solution to get the current date and time in a suitable format for usage in a file or folder name, without worrying about regional settings. Let's dive in!

The Problem at Hand

You want to create a .bat file that zips up a directory into an archive with the current date and time as part of the name. However, the challenge here is to find a way to retrieve the date and time in a format that fits your needs, like yyyy-mm-dd or any other simple format.

The Clunky Solution

One approach that can solve this problem involves using a series of commands to manipulate the date and time string. Here's an example:

rem Get the datetime in a format that can go into a filename.
set _my_datetime=%date%_%time%
set _my_datetime=%_my_datetime: =_%
set _my_datetime=%_my_datetime::=%
set _my_datetime=%_my_datetime:/=_%
set _my_datetime=%_my_datetime:.=_

rem Now use the timestamp by including it in a new ZIP file name.
"d:\Program Files\7-Zip\7z.exe" a -r Code_%_my_datetime%.zip Code

While this solution works, it may not be the most elegant or concise approach. Plus, it doesn't provide the date format you desire.

A Simpler Solution

Luckily, there's a simpler solution that gives us more flexibility with the date format. Instead of relying on the traditional Windows command line, we can leverage PowerShell to accomplish our goal. Here's how you can do it:

@echo off

for /f "usebackq" %%i in (`powershell.exe -Command "(Get-Date).ToString('yyyy-MM-dd_HHmmss')"`) do set "datetime=%%i"

echo %datetime%

In this updated solution, we utilize PowerShell's ability to format dates exactly the way we want. The command (Get-Date).ToString('yyyy-MM-dd_HHmmss') gives us the desired format yyyy-MM-dd_HHmmss (e.g., 2022-12-31_235959). We then capture this value in the datetime variable using the for /f loop.

By using PowerShell, we eliminate the need for complex string manipulations and achieve the desired format effortlessly.

Taking It a Step Further

If you're looking to enhance your script even more, you can integrate this simple solution into your existing batch file. Combine it with the zipping command to create a file or folder name with the current date and time:

@echo off

for /f "usebackq" %%i in (`powershell.exe -Command "(Get-Date).ToString('yyyy-MM-dd_HHmmss')"`) do set "datetime=%%i"

"d:\Program Files\7-Zip\7z.exe" a -r Code_%datetime%.zip Code

Feel free to modify this example to fit your specific needs. You can change the file path, folder name, or add additional commands before or after the zipping process.

Share and Engage!

Now that you have learned a simple yet effective solution to retrieve the current date and time in the Windows command line, why not share this knowledge with others who might find it useful? Spread the word by sharing this blog post with your friends and colleagues.

If you found this guide helpful or have any further questions, feel free to leave a comment below. We'd love to hear from you and help you out!


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my

Matheus Mello
Matheus Mello