Solved

date format

Posted on 2012-03-12
10
429 Views
Last Modified: 2012-06-21
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.
0
Comment
Question by:-Dman100-
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 43

Assisted Solution

by:Steve Knight
Steve Knight earned 250 total points
ID: 37710156
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 this highlights a general issue here in that it relies on the user who runs the command's date format, e.g. dd/mm/yyyy mm/dd/yyyy dd-mm-yyyy yyyy-mm-dd etc. and therefore is easily wrong.

Look at my article here for betters ways to do it:
http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html

You will see in my article above an entry "yyyymmdd.cmd" at one of the last comments right down the bottom which gives you a prebuilt script that will get you yyyymmdd regardless of the user's date format.

also be aware user is quite likely to have 1/ 3/2012 rather than 01/03/2012 so there won't be leading zeroes if you want them using just %date% either.

Steve
0
 
LVL 15

Expert Comment

by:Ess Kay
ID: 37710215
0
 
LVL 15

Expert Comment

by:Ess Kay
ID: 37710223
yyyymmdd

select convert(varchar, getdate(), 112)
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 11

Accepted Solution

by:
paultomasi earned 250 total points
ID: 37710240
-Dman100-

You need:

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

Open in new window

0
 

Author Comment

by:-Dman100-
ID: 37710345
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?
0
 
LVL 1

Expert Comment

by:Turkey1974
ID: 37710438
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.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37710553
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
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37711419
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.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 37711540
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.
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 37711946
Steve....
You know as well as I do 'the bigger the spoon, the wider the smile'!
:)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question