Solved

copying file with current date

Posted on 2014-01-21
6
387 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
Comment Utility
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
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
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 your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 33

Expert Comment

by:knightEknight
Comment Utility
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
Comment Utility
it's came like this:

kumar---.srf
0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 125 total points
Comment Utility
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 run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now