Solved

copying file with current date

Posted on 2014-01-21
6
395 Views
Last Modified: 2014-01-21
Support,

Whenever I'm trying to copy the file with current date in the batch scripting it's copied like this..

Copy "\\<servername>\d$\kumar.srf" "\\<servername>\d$\kumar%date:~-10,2%%date:~-7,2%%date:~-4,4%.srf"
 
 1 file(s) copied


it's copied like this:

kumar--------.srf
0
Comment
Question by:iamsateesh
6 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 39798037
Open a command prompt and run the following:

C:\>  echo %date:~-10,2%%date:~-7,2%%date:~-4,4%

Open in new window

What is the output of the above command?  (the command begins with "echo")
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39798042
Use a better way of getting the date perhaps.

What do you get from

echo %date%

set d=%date:/=%

 will remove / from the date into variable d

set d=%~d,4%
echo use %d% in your copy now.

that will remove first 4 chars if date is Mon 21/01/2014 etc.

But better is to use more reliable date that doesn't matter what the user has set for date format.  Try this:

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html

Steve
0
 
LVL 53

Expert Comment

by:Bill Prew
ID: 39798064
While it is true that the specific format of the %DATE% variable can vary depending on Windows settings, you seemed to indicate that you are getting "--------" as the result of "%date:~-10,2%%date:~-7,2%%date:~-4,4%" and that doesn't make sense.

Are you sure you aren't changing the DATE environment variable someplace in your code to have a lot of dashes in it?

~bp
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 33

Expert Comment

by:knightEknight
ID: 39798065
Something to try:

copy  "\\<servername>\d$\kumar.srf"  "\\<servername>\d$\kumar%date:~4,2%%date:~7,2%%date:~10,4%.srf"

Open in new window

This just switches out the negative values for positive values that correspond to the same position(s) in the date string.
0
 

Author Comment

by:iamsateesh
ID: 39798166
it's came like this:

kumar---.srf
0
 
LVL 53

Accepted Solution

by:
Bill Prew earned 125 total points
ID: 39798175
And of course, with more and more systems having powershell on them, you can leverage it to keep things simple and do:

for /f %%A in ('powershell get-date -format "{yyyyMMdd}"') do set Stamp=%%A
Copy "\\<servername>\d$\kumar.srf" "\\<servername>\d$\kumar%Stamp%.srf"

Open in new window

~bp
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

803 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