• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2657
  • Last Modified:

Millisecond Variable in Windows Shell

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
nsgguy245
Asked:
nsgguy245
  • 3
  • 2
2 Solutions
 
johnnash1180Commented:
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
 
BillDLCommented:
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
 
nsgguy245Author Commented:
Thanks both for the detail and the explanation!
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
BillDLCommented:
Thank you nsgguy245
Hey, I thought I asked you not to include me when accepting the answer ;-)
0
 
nsgguy245Author Commented:
I know, but I liked your explanation on how that worked. So thanks again. :)
0
 
BillDLCommented:
You're very welcome.
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now