Solved

Millisecond Variable in Windows Shell

Posted on 2009-05-12
6
2,384 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
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…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

705 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

17 Experts available now in Live!

Get 1:1 Help Now