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

create file name with date in YYYYMMDD format

4,987 views
Skip to first unread message

Beliavsky

unread,
Oct 2, 2008, 8:52:25 AM10/2/08
to
In a batch file, how can I issue a command such as

copy foo.txt foo_YYYYMMDD.txt

where YYYYMMDD is the current date.

I am using Windows XP.

crfer...@gmail.com

unread,
Oct 2, 2008, 10:14:40 AM10/2/08
to

I happen to be working on a batch file that includes the logic you
need. Go figure :-)

===================
:: Get current date
for /f "tokens=2,3,4 delims=/ " %%a in ('date /t') do set CurrDate=%%c%
%a%%b

copy foo.txt foo_%CurrDate%.txt
===================

billious

unread,
Oct 2, 2008, 10:58:42 AM10/2/08
to

"Beliavsky" <beli...@aol.com> wrote in message
news:e419d4b4-6b8f-4a09...@h2g2000hsg.googlegroups.com...

#1 FAQ - read Timo's FAQ - posted weekly, last posted Sep 22 @1159am (and
hence overdue - I've never figured out whether the date/time shown as "sent"
is UTC, local or local-with-daylight-saving-adjustment or even sender's or
receiver's view...)

The major issue is the date-format being used by the user running your file.
The format returned by date/t is set up by the USER settings, so it's not
consistent. Each user can set up a date format to optionally include dayname
or leading zeroes, have 2 or 4 digit year numbers, have different
element-sequences or have different separators.

Without knowing precisely the format you are using and whether or not it's
for your own use or for use in a commercial - even international -
environment, it's dangerous to make a simple suggestion like one that
assumes separator, element-sequence and element-presence.

Best to look at the FAQ for a guide - it covers most situations.


Message has been deleted

Timo Salmi

unread,
Oct 2, 2008, 3:26:15 PM10/2/08
to
billious <billio...@hotmail.com> wrote:
> #1 FAQ - read Timo's FAQ - posted weekly, last posted Sep 22 @1159am (and
> hence overdue

Ah, our crontab process must have been down. Anyway (for the OP)

1} How to get today's date elements into environment variables?
http://www.netikka.net/tsneti/info/tscmd001.htm

Also in the FAQ e.g.
6} How does one get yesterday's date?
31} How many days ago was 31.12.2003? What date was it 100 days ago?

All the best, Timo

--
Prof. Timo Salmi mailto:t...@uwasa.fi ftp & http://garbo.uwasa.fi/
Hpage: http://www.uwasa.fi/laskentatoimi/english/personnel/salmitimo/
Department of Accounting and Finance, University of Vaasa, Finland
Useful CMD script tricks http://www.netikka.net/tsneti/info/tscmd.htm

contrex

unread,
Oct 2, 2008, 4:39:46 PM10/2/08
to

REM get MM, DD and YYYY independent of system date format settings
echo Wscript.echo ^(DatePart^("YYYY", Date^)^)>DateYear.vbs
echo Wscript.echo ^(DatePart^("M", Date^)^)>DateMonth.vbs
echo Wscript.echo ^(DatePart^("D", Date^)^)>DateDay.vbs
for /f %%A in ('DateYear.vbs //nologo') do set /a yyyy=%%A
for /f %%A in ('DateMonth.vbs //nologo') do set /a mm=%%A
for /f %%A in ('DateDay.vbs //nologo') do set /a dd=%%A
del DateYear.vbs
del DateMonth.vbs
del DateDay.vbs
REM add leading zero to single digit numbers
if %mm% LSS 10 set mm=0%mm%
if %dd% LSS 10 set dd=0%dd%

ten.n...@virgin.net

unread,
Oct 3, 2008, 12:44:30 PM10/3/08
to

If you insist on using DatePart, then you could produce your output all in
one go!
::----- START -----
>%temp%\_$.vbs (Echo:DateString=DatePart^("yyyy",Date^) ^& Right^(^
"0" ^& DatePart^("m",Date^),2^) ^&Right^("0" ^&DatePart^("d",Date^),2^)
Echo:Wscript.Echo DateString)
For /f %%# In ('cscript //nologo %Temp%\_$.vbs') Do (Set D_=%%#&&Del
%temp%\_$.vbs)
::------ END ------
The usual two spaces have preceeded each new line in order to highlight
line wrap!

contrex

unread,
Oct 3, 2008, 3:27:05 PM10/3/08
to
On 3 Oct, 17:44, "ten.nig...@virgin.net" <ten.nig...@virgin.net>
wrote:

> If you insist on using DatePart, then you could produce your output all in
> one go!

Yours is more concise, sure, but I reckon mine is easier for somebody
to fool around with Lego-fashion if they are more familiar with batch
than with wsh.

foxidrive

unread,
Oct 4, 2008, 3:14:12 AM10/4/08
to
On Fri, 3 Oct 2008 12:27:05 -0700 (PDT), contrex <mike.j...@gmail.com>
wrote:

Yeah, sometimes dense code is difficult to decipher.

Here's another option:

:: DateTime - WMIC
@echo off
:: info based on usenet post by ten.n...@virgin.net
:: 18 Aug 2008 22:29:38 +0100,
:: xp and higher

(
Wmic Path Win32_LocalTime Get day,hour,minute,month,second,year /value
)>%temp%.\wmicinfo.txt
for /f "delims=" %%a in ('type %temp%.\wmicinfo.txt') do set %%a
del %temp%.\wmicinfo.txt
set Day=00%day%
set Hour=00%hour%
set Minute=00%minute%
set Month=00%month%
set Second=00%second%
set Day=%day:~-2%
set Hour=%hour:~-2%
set Minute=%minute:~-2%
set Month=%month:~-2%
set Second=%second:~-2%
set yr=%year:~2%

echo Hour=%hour%
echo Minute=%minute%
echo Second=%second%
echo Day=%day%
echo Month=%month%
echo Year=%year%
echo Yr=%yr%
pause


ten.n...@virgin.net

unread,
Oct 5, 2008, 6:00:50 PM10/5/08
to

Or reduced for the purposes of this exercise:

::----- START -----
@Echo off&Setlocal
Set "_=%temp%\_$.tmp"
Wmic /output:%_% Path Win32_LocalTime Get Day,Month,Year /value
For /f "tokens=1-2 delims==" %%A In (
'Find "="^<%_%^&del %_%') Do (If %%B Lss 10 (Set %%A=0%%B) Else (Set
%%A=%%B))
Echo:Copy foo.txt foo_%Year%%Month%%Day%.txt&Pause
::------ END ------

Dr J R Stockton

unread,
Oct 6, 2008, 10:40:00 AM10/6/08
to
On Oct 2, 3:58 pm, "billious" <billious_1...@hotmail.com> wrote:

> #1 FAQ - read Timo's FAQ - posted weekly, last posted Sep 22 @1159am (and
> hence overdue - I've never figured out whether the date/time shown as "sent"
> is UTC, local or local-with-daylight-saving-adjustment or even sender's or
> receiver's view...)

Read the actual headers of a few articles, using your regular system,
and including one of your own posted at a known local clock time, and
you'll soon work it out. Especially if you know where you are and the
relevant relationships between local times and UTC. Omphalocentric
systems stamp their local time, with offset.

Timo probably uses a CRON job to post while he's asleep. Many use
PST, even in Summer. Read the beginning of "- And he built a crooked
house -".

NOWMINUS, via sig line 3, programs directory, will do most Gregorian
date-setting jobs (not in Vista-64?).

--
(c) John Stockton, near London, UK. Posting with Google.
Mail: J.R.""""""""@physics.org or (better) via Home Page at
Web: <URL:http://www.merlyn.demon.co.uk/>
FAQish topics, acronyms, links, etc.; Date, Delphi, JavaScript, ....|

mik3...@gmail.com

unread,
Oct 7, 2008, 4:23:20 AM10/7/08
to

if you have Services for Unix from Microsoft

date "+%Y%m%d"

Bob

unread,
Oct 9, 2008, 12:22:30 AM10/9/08
to
Dr J R Stockton wrote:

(snip)


between local times and UTC. Omphalocentric
> systems stamp their local time, with offset.

What the 'hey' is Omphalocentric?
Coined a word have you Sir Stockton?


Timo Salmi

unread,
Oct 9, 2008, 2:02:32 AM10/9/08
to
Bob <no_e...@for.me> wrote:
> What the 'hey' is Omphalocentric?

Actually "omphaloenteric". And yes, it is a genuine word. A medical
dictionary will tell you more about it.

foxidrive

unread,
Oct 9, 2008, 2:59:36 AM10/9/08
to

This is just a guess but John might be preparing to purchase one of these
Oompa-Loompa outfits.
http://www.amazon.com/Charlie-Chocolate-Factory-Adults-Costume/dp/B000TMM7KU


Zaphod Beeblebrox

unread,
Oct 9, 2008, 8:10:40 AM10/9/08
to

"Timo Salmi" <t...@uwasa.fi> wrote in message
news:48ed9e78$0$25381$9b53...@news.fv.fi...

> Bob <no_e...@for.me> wrote:
>> What the 'hey' is Omphalocentric?

Omphalocentric = Overly introspective and inclined to navel-gazing

>
> Actually "omphaloenteric". And yes, it is a genuine word. A medical
> dictionary will tell you more about it.
>

Omphaloenteric = Relating to the umbilicus and the intestine

Either way, how does that have anything to do with how systems handle
local time, daylight saving time and offsets? Color me confused...

--
Zaphod

Zaphod's just zis guy, ya know?


Dr J R Stockton

unread,
Oct 9, 2008, 5:40:04 PM10/9/08
to
In alt.msdos.batch.nt message <48ed9e78$0$25381$9b53...@news.fv.fi>,
Thu, 9 Oct 2008 09:02:32, Timo Salmi <t...@uwasa.fi> posted:

>Bob <no_e...@for.me> wrote:
>> What the 'hey' is Omphalocentric?
>
>Actually "omphaloenteric". And yes, it is a genuine word. A medical
>dictionary will tell you more about it.

No : actually omphalocentric. I use it more, but not completely, in the
literal sense. Moderate-sized dictionaries don't necessarily have it,
but the Web does.

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.

Usenet News services are currently unreliable; I may not see all articles here.

irina.art...@gmail.com

unread,
Sep 5, 2015, 10:34:45 PM9/5/15
to
Thank you for your help!

I modified the answer to make it exactly in YYYYMMDD format:

for /f "tokens=1,2,3,4 delims=/ " %%a in ('date /t') do set CurrDate=%%d%%c%%a%%b

echo %CurrDate%





Kerr Mudd-John

unread,
Sep 7, 2015, 5:03:23 AM9/7/15
to
On Sun, 06 Sep 2015 03:34:44 +0100, <irina.art...@gmail.com> wrote:

> Thank you for your help!-MM-DD
>
> I modified the answer to make it exactly in YYYYMMDD format:
>
> for /f "tokens=1,2,3,4 delims=/ " %%a in ('date /t') do set
> CurrDate=%%d%%c%%a%%b
>
> echo %CurrDate%
>
Note that this is only a local (and perhaps temporary) solution; It will
break if you change or have a different date format. I already set mine to
YYYY-MM-DD, so juggling the tokens would mess it up.

But as you're using GG, you're probably replying to an ancient post anyway.


--
Bah, and indeed, Humbug
0 new messages