Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Rename file with date and time stamp using BAT file

30,192 views
Skip to first unread message

Brett

unread,
Dec 17, 2009, 3:50:15 AM12/17/09
to
Hello,

I have a batch file I use to rename files with the date and time
stamp:

RENAME W:\TEST.TXT TEST_%date:~-4,4%%date:~-7,2%%date:~0,2%_%time:~0,2%
%time:~3,2%%time:~6,2%.TXT

This will work correctly if the time is after 10AM, if the time is
between 01:00 and 09:59 it will fail. Can anyone help me out and let
me know why?

For example if I run the BATCH file at 10:01 I will see
TEST_20091217_100112.TXT, if I run it at 09:58 the file command fails
with error "The syntax of the command is incorrect."

Thanks,

Brett

foxidrive

unread,
Dec 17, 2009, 6:23:53 AM12/17/09
to
On Thu, 17 Dec 2009 00:50:15 -0800 (PST), Brett <brettma...@gmail.com>
wrote:

>I have a batch file I use to rename files with the date and time
>stamp:
>
>RENAME W:\TEST.TXT TEST_%date:~-4,4%%date:~-7,2%%date:~0,2%_%time:~0,2%
>%time:~3,2%%time:~6,2%.TXT
>
>This will work correctly if the time is after 10AM, if the time is
>between 01:00 and 09:59 it will fail. Can anyone help me out and let
>me know why?

The time will have a leading space character beween 1 and 10am and your
rename command doesn't support longfilename elements.

>For example if I run the BATCH file at 10:01 I will see
>TEST_20091217_100112.TXT, if I run it at 09:58 the file command fails
>with error "The syntax of the command is incorrect."

This should be more robust:

set d=%date:~-4,4%%date:~-7,2%%date:~0,2%
set d=%d: =_%
set t=%time:~0,2%%time:~3,2%%time:~6,2%
set t=%t: =0%

RENAME "W:\TEST.TXT" "TEST_%d%_%t%.TXT"

Brett

unread,
Dec 17, 2009, 7:43:31 AM12/17/09
to
On 17 Dec, 11:23, foxidrive <got...@woohoo.invalid> wrote:
> On Thu, 17 Dec 2009 00:50:15 -0800 (PST), Brett <brettmannin...@gmail.com>

Thank you foxidrive, this is perfect.

Dr J R Stockton

unread,
Dec 18, 2009, 2:03:42 PM12/18/09
to
In alt.msdos.batch message <89c154fe-f115-4adb-b81e-4ec4b10b7aad@l13g200
0yqb.googlegroups.com>, Thu, 17 Dec 2009 00:50:15, Brett
<brettma...@gmail.com> posted:

>
>This will work correctly if the time is after 10AM, if the time is
>between 01:00 and 09:59 it will fail. Can anyone help me out and let
>me know why?

Set your computer to show ISO 8601 date & time format, and all such
problems will be behind you.

Read <http://en.wikipedia.org/wiki/ISO_8601>.

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm estrdate.htm js-dates.htm pas-time.htm critdate.htm etc.

Todd Vargo

unread,
Dec 20, 2009, 10:24:25 AM12/20/09
to
Dr J R Stockton wrote:
> In alt.msdos.batch message <89c154fe-f115-4adb-b81e-4ec4b10b7aad@l13g200
> 0yqb.googlegroups.com>, Thu, 17 Dec 2009 00:50:15, Brett
> <brettma...@gmail.com> posted:
> >
> >This will work correctly if the time is after 10AM, if the time is
> >between 01:00 and 09:59 it will fail. Can anyone help me out and let
> >me know why?
>
> Set your computer to show ISO 8601 date & time format, and all such
> problems will be behind you.
>
> Read <http://en.wikipedia.org/wiki/ISO_8601>.

Details to, "Set your computer to show ISO 8601 date & time format", would
be worthwhile to include since the standard alone does not explain it. OTOH,
many people do not have necessary permission required to modify date/time
format, so perhaps it is time you consider an alternate method that does not
require system settings modifications.

--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

Dr J R Stockton

unread,
Dec 21, 2009, 3:35:05 PM12/21/09
to
In alt.msdos.batch message <hglg97$245v$1...@adenine.netfront.net>, Sun, 20
Dec 2009 10:24:25, Todd Vargo <tlv...@sbcglobal.netz> posted:

Evidently your memory and wits are ageing further.

I have, and offer, as you should recall, NOWMINUS, via sig line 3, for
those rightfully asking in this newsgroup, and for some of those asking
in a.m.b.nt (but maybe not for 64-bitters).

One can also, in at least Win98 & later, do it in Windows Scripting
Host, with either JavaScript or VBScript; my batch pages, via sig,
refer.

I suppose one can do it in PowerShell, too.

Nevertheless, what I wrote is strictly accurate (provided one does not
import batch files written strictly for those who use FFF date format.

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk DOS 3.3 6.20 ; WinXP.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>
My DOS <URL:http://www.merlyn.demon.co.uk/batfiles.htm> - also batprogs.htm.

Todd Vargo

unread,
Dec 22, 2009, 6:08:07 PM12/22/09
to
Dr J R Stockton wrote:
> Todd Vargo posted:

We both know there are many methods to collect date/time elements, and we
both know you thrive to antagonize, but your time would have been better
spent had your post related to what I mentioned.


/\/\
/ / /
_/,/ /
_/` (/"/////,
( '```--.___
/' _), ,- '-.
/, / \ (\ \,
\_()/ \) )' =_ )) |
| | .// _/)
( ( \_ // /
\ >_,\ (/)= /
| | | \ #\| /
|=| |=|\ ( (
(=> ( >( >),)
| | |=| \ ( (
/ / / / ) |/
\ /_( /_( , || )/.,_
). /\\_(\,/,_//-_/ /_(_( /
,\. .- '-'-'-,)\/.'))


eme...@gmail.com

unread,
Nov 13, 2013, 9:13:26 AM11/13/13
to
Thank you very much it's work for me!

manuel...@gmail.com

unread,
Feb 20, 2014, 5:09:55 AM2/20/14
to
Thanks!!! works great!

chitrala....@gmail.com

unread,
Jan 4, 2019, 11:14:33 AM1/4/19
to
Thank You Foxidrive, This really helped and working fine

pyotr filipivich

unread,
Jan 4, 2019, 10:36:18 PM1/4/19
to
chitrala....@gmail.com on Fri, 4 Jan 2019 08:14:32 -0800 (PST)
typed in alt.msdos.batch the following:
And simpler than the version I found:

rem - make date & time data available
for /f "tokens=1-4 delims=/ " %%a in ('date /t') do (set weekday=%%a&
set month=%%b& set day=%%c& set year=%%d)
for /f %%a in ('time /t') do set time=%%a
for /f "tokens=1-3 delims=: " %%a in ('time /t') do (set hours=%%a&
set minutes=%%b& set TheEnd=%%c)

which I then use:

set Hack=%month%%day%_%hours%%minutes%-Load
set DateHack=%year%%month%%day%
set Hack1=%hours%%minutes%

and

set LogFile=%Hack%AXL_LOG.rtf

and finally

robocopy . r: /s /XO /np /xx /if *.dat *.idx *.bat /tee
/Log+:%LogFile%

So that a copy of what is done is logged into file
0104_1925-LoadAXL_log.rtf
Which works for me.

Haven't tried the rename by batchfile, yet.

--
pyotr filipivich
Next month's Panel: Graft - Boon or blessing?
0 new messages