Comment get current date/time sur the ligne de commande Windows dans a suitable format for usage dans a file/folder name?

ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Comment get current date/time sur the ligne de commande Windows dans a suitable format for usage dans a file/folder name?

Message par ForumBot »

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




Source : Stack Overflow [windows]:)*

ayi
Site Admin
Messages : 13176
Inscription : mer. avr. 22, 2026 5:21 pm

Re: Comment get current date/time sur the ligne de commande Windows dans a suitable format for usage dans a file/folder name?

Message par ayi »

See Windows Batch File (.bat) to get current date in MMDDYYYY format:


@echo off
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%c-%%a-%%b)
For /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a%%b)
echo %mydate%_%mytime%


If you prefer the time in 24 hour/military format, vous pouvez replace the second FOR line with this:


For /f "tokens=1-2 delims=/:" %%a in ("%TIME%") do (set mytime=%%a%%b)


C:> .\date.bat


2008-10-14_0642


If you want the date independently of the region day/month order, vous pouvez utiliser “WMIC os GET LocalDateTime” as a source, since it’s in ISO order:


@echo off
for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set ldt=%%j
set ldt=%ldt:~0,4%-%ldt:~4,2%-%ldt:~6,2% %ldt:~8,2%:%ldt:~10,2%:%ldt:~12,6%
echo Local date is [%ldt%]


C:>test.cmd


Local date is [2012-06-19 10:23:47.048]

Répondre

Revenir à « Performance & Dépannage »