Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm writing a script to RAR-compress my files and name the result with the time stamp.

I use the TIME environment variable. I also do

set _time=%TIME::=.%` 

to use only allowed characters in the filename.

How do I get rid of the milliseconds part of the %TIME%, e.g. 16:35:42.07

share|improve this question
up vote 3 down vote accepted

Just cut off the characters you don't need afterwards:

 set _time=%_time:~0,-3%

Or get the full, desired result directly like this:

set _time=%TIME:~0,2%-%TIME:~3,2%-%TIME:~6,2%
share|improve this answer

The easy way would be if the milliseconds field is always two digits: %_TIME:~0,-3%
Translates to trim off the first 0 characters, then keep up to (size of string - 3) characters. Which would lop off the .07 in your example. In fact, this should always work: I just realised you're using the built in %time% variable (teaches me to read), which should always return with a two digit 'ms' value.

If your milliseconds could be a different number of digits (e.g. 0.1 instead of 0.10), some kind of string search command would be needed. And here it is:

Ok, this seems hackish.. but it's Windows command line, so hey. This will trim off the dot and everything after it, so it will work no matter how many digits of precision your ms value is (e.g. 0.1 instead of 0.10).

SetLocal EnableDelayedExpansion

set _time=%time%_EndOfStringMarker
set __DotToEnd=%_time:*.=%
set _time=!_time:%__DotToEnd%=!
set _time=%_time:.=%

EndLocal && set _time=%_time%

Make sure there is only one dot in your time when this is used.

Attempting to explain now. Of course, you can just use it and save yourself the headache of trying to understand.

  • SetLocal defines a local scope. Anything changed between now and the EndLocal will not last past the EndLocal. You cannot have a SetLocal within a SetLocal.
  • EnableDelayedExpansion allows the use of !s, among other things. It lets me use a variable within a variable.
  • I add an _EndOfStringMarker so if the ms value (in your example 07) occurs anywhere else, e.g. the seconds value, that will not be replaced.
  • I trim off from before the dot, including the dot, leaving me with everything after the dot. The command line doesn't let me do it the other way around.
  • I replace everything after the dot in the original string with nothing, leaving me with the time ending in a dot.
  • I replace the dot with nothing.
  • I end the local area, to tidy things up a bit. To keep the new time value outside this local scope, I have to set it to itself on the same line. It's yet another quirk of the command line.

Source for the dodgy trim to end:

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .