Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

copying file with current date

Posted on 2014-01-21
6
Medium Priority
?
400 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 59

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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
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 59

Accepted Solution

by:
Bill Prew earned 375 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

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…
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 …
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

916 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