silverkid
silverkid

Reputation: 9573

how to set date to current date using dos batch file command

how to set date to current date using dos batch file command.

Upvotes: 3

Views: 85442

Answers (6)

Manni Dee
Manni Dee

Reputation: 9

If you need to include the time when making the file you can use this

C:\scripts\startup.bat >> automaton_logs_%date:/=%_%time:~0,2%%time:~3,2%%time:~6,2%.txt 2>&1

This command runs a script and redirects both standard out and error to a file with the timestamp of the execution when configured in a shortcut on the desktop

to see the file name it would use you can do

C:\Users\Administrator>echo automaton_logs_%date:/=%_%time:~0,2%%time:~3,2%%time:~6,2%.txt

automaton_logs_16062023_111951.txt

Upvotes: -1

Mister Robato
Mister Robato

Reputation: 176

This should work if you have the "Set time automatically" option on:

net stop w32time    
net start w32time
w32tm /resync /nowait

Or you can save the current date to a text file and then pass the current date to the date command:

date /t>current_date.txt

OR

echo %date%>current_date.txt

Then pass the current date to the date command:

date<current_date.txt

Upvotes: 1

B-Art
B-Art

Reputation: 121

You must take in account that US date is a bit different from europe date. Giving Date displayes:

>date
The current date is: 2019-09-12
Enter the new date: (yy-mm-dd)
But US date will be something like: mm-dd-yy

Better have a look at: https://www.robvanderwoude.com/batexamples.php

Upvotes: -2

hallie
hallie

Reputation: 2845

When you type "date" on the dos command it will show you the date specified in your bios, and ask you to set the current date (you can set this if the date in your bios is not the current). The system has no ability to know if it's date is correct.

If you have a server with correct date, you can use "net time" so the the client can synchronize with the server.

NET TIME <SERVERNAME> /SET

or

NET TIME \\SERVERNAME /SET /YES

Upvotes: 2

Carlos Guti&#233;rrez
Carlos Guti&#233;rrez

Reputation: 14282

If you need to use the current date in a batch file, the variable %date% has the current date:

echo %date%
23/02/2010

It uses the format of the regional setting of your computer. In my computer it's dd/mm/yyyy.

Since the / can't be part of a file name, they must be replaced with a safe character or nothing:

echo %date:/=-%
23-02-2010

echo %date:/=%
23022010

If you want to create a backup copy of a file, you can do something like:

copy file.txt file-%date:/=%.txt
dir /b file*.*
file-23022010.txt
file.txt

Or first set it to a variable and then use it:

set currdate=%date:/=%
copy file.txt file-%currdate%.txt

Upvotes: 9

J&#248;rn Schou-Rode
J&#248;rn Schou-Rode

Reputation: 38336

The date command is what you are looking for. This works on my Windows XP box:

date 15-02-2010

Notice the formatting dd-MM-yyyy, which seems to be required here, probably because of my regional settings being set to Denmark. The documentation states that the format is MM-dd-yy, but on my computer, the day and month fields gets flipped if the date is written in that format.

Upvotes: 3

Related Questions