Solved

Millisecond Variable in Windows Shell

Posted on 2009-05-12
6
2,433 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

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

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
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 …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

805 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