Solved

Millisecond Variable in Windows Shell

Posted on 2009-05-12
6
2,465 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
  • 3
  • 2
6 Comments
 
LVL 7

Accepted Solution

by:
johnnash1180 earned 200 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 50 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

820 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