Solved

copying file with current date

Posted on 2014-01-21
6
396 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
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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

Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

828 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