Link to home
Start Free TrialLog in
Avatar of -Dman100-
-Dman100-Flag for United States of America

asked on

date format

I am using a post processing command to copy one file to another file with a date stamp at the end of the file name.  I found an example of adding the date stamp,  but it adds the date stamp in the wrong format.  Currently, the date stamp is in the format:

YYYYDDMM

But, I need the date format to be in:

YYYYMMDD

Here is the command I'm using:

copy c:\Users\UserName\Desktop\TemplateFile.txt c:\Users\UserName\Desktop\FileName_%date:~-4,4%%date:~-7,2%%date:~-10,2%.txt

I just need to switch the month and day in the date stamp.

Thanks for any help.
Regards.
SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
yyyymmdd

select convert(varchar, getdate(), 112)
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of -Dman100-

ASKER

I tried the following, but it failed?

copy c:\Users\UserName\Desktop\Template.txt
c:\Users\UserName\Desktop\FileName_%date:~-4,4%%date:~-10,2%%date:~-7,2%
.txt

It looks like I have everything correct, but the command failed?
Avatar of Turkey1974
Turkey1974

This is the format you requested:

copy c:\Users\UserName\Desktop\TemplateFile.txt c:\Users\UserName\Desktop\FileName_%date:~-4,4%%date:~-10,2%%date:~-7,2%.txt
(just switch the -7 and -10)

As for why?  The negative number refers to the number of characters from right to left of the result of %date% to begin and the second number is how many characters you want to extract from %date% beginning at the chosen character to start with (the negative number).

Prove it, try the following at a command prompt: (use of echo will allow you to test at a command prompt)
echo %date%

This will return:
Mon 03/12/2012

Now try the following:
echo %date:~-4,4%  Result:  2012
echo %date:~-5,5%  Result:  /2012
echo %date:~-5,3%  Result:  /20
echo %date:~-10,5%  Result:  03/12
echo %date:~-10,6%%date:~-2,2%  Result:  03/12/12
echo %date:~-10,2%-%date:~-7,2%-%date:~-2,2%  Result:  03-12-12

Explore other combinations to produce the desired output.
Can I ask why I got an "assisted" and half the marks when I gave you the answer?

Quite likely that you have spaces in the filename now as it will be 2012 312.txt or the like.  You will need either " " around the filename or use one of the techniques which works reliably as per my link.

Steve
dragon-it

>> "Can I ask why I got an "assisted" and half the marks when I gave you the answer?"

You gave a nice write-up along with a really useful link in http:#37710156 however, you didn't actually give the 'answer' he was seeking. I think he just wanted something he could copy & paste that worked straight off as provided in http:#37710240.
Well short of actually putting the spoon in his mouth for him:

You can just amend the %date:~-4,4%%date:~-7,2%%date:~-10,2% bits at a simple level:

%date:~-4,4% "YYYY"
%date:~-7,2% "DD"
%date:~-10,2% "MM"

%date:~-4,4%%date:~-10,2%%date:~-7,2%

But then with no feedback from asker don't expect anything else.
Steve....
You know as well as I do 'the bigger the spoon, the wider the smile'!
:)