[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Millisecond Variable in Windows Shell

Posted on 2009-05-12
6
Medium Priority
?
2,631 Views
Last Modified: 2012-05-06
I need to write a script that changes a filename name to a name with the date and time appended to it. I have most of it already, but cannot figure out how to include milliseconds as part of the time. This is what I have so far:

Rename "%%V" .%%V%%L%%J%%K_%%M%%N%%~xV

0
Comment
Question by:nsgguy245
[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
  • 2
6 Comments
 
LVL 7

Accepted Solution

by:
johnnash1180 earned 800 total points
ID: 24372047
You can get the millisecond part of current time from "%time:~-2,2%" value.

Similarly, You can get the date value by "%date:~4%" value.

You can replace the "/" by "_" by using "%date:/=_%" value.

So, the batch file will be as follows...

John Nash
rem to assign file name as date_time format
set filename=%date:~4%_%time%
 
rem to replace / char by _ char
set filename=%filename:/=_%
 
rem to remove blank space
set filename=%filename: =%
 
rem to replace . char [separates millisecond in time value] by _ char
set filename=%filename:.=_%
 
rem Now filename contains the time stamp value.

Open in new window

0
 
LVL 38

Assisted Solution

by:BillDL
BillDL earned 200 total points
ID: 24373518
nsgguy245

That's a nicely described method from John above, and I'm sure it provides exactly what you are looking for.  I am just going to add a brief further explanaton about the theory used by John that may help you use the same method with other projects that aren't necessarily time or date variables.

The :~ can be used to modify the value of the variable, as John showed on the first 2 lines of his answer.  Used like %Variable:~x,y% what this does is set the starting point of the value held in the variable, and then tell it how many characters to grab from it.

So, say you had my Experts-Exchange user name (BillDL) as your variable set as NAME, and you modified and echoed it back with %NAME:~4,2%
Counting the leftmost character as Zero, it is told to go to the 4th character "D" and then include that and the next character, ie. include only 2 characters.  It would echo back DL.

You just have to remember that the first character is the Zero marker, not 1.

%NAME:~-2,2% (note the minus sign) tells it to count backwards 2 characters from the last (rightmost) character and then, going forwards, include 2 characters from that point.  Output is the same as the above ie. DL

%NAME:~0,4% gives the output Bill

This method allows you to skip unwanted characters such as the / in the date format that aren't allowed in file names and such.  John's method above is nice and neat, but the alternative long-handed approach where you can use fragments of the variable (assuming the date format to be MM/DD/YYYY (05/13/2009) is like this:

echo It is Day %DATE:~3,2% of Month %DATE:~0,2% in the year %DATE:~6,4% AD

or something like this to create underscores between the fragments (MM_DD_YYYY) for use in a file name:

set TODAY=%DATE:~0,2%_%DATE:~3,2%_%DATE:~6,4%
ren "FileName.txt" "%TODAY%.txt"

The same theory can be applied to ANY text string set as a variable, including the %TIME% output as John showed above.

As I said, John's way of skipping the / and : characters is much neater.  I am just explaining the WAY it works, so PLEASE DO NOT include me when accepting John's comment as your answer.
0
 

Author Comment

by:nsgguy245
ID: 24373858
Thanks both for the detail and the explanation!
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 
LVL 38

Expert Comment

by:BillDL
ID: 24373949
Thank you nsgguy245
Hey, I thought I asked you not to include me when accepting the answer ;-)
0
 

Author Comment

by:nsgguy245
ID: 24374117
I know, but I liked your explanation on how that worked. So thanks again. :)
0
 
LVL 38

Expert Comment

by:BillDL
ID: 24375752
You're very welcome.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

649 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