Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

2 Answers 2

up vote 2 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: http://ss64.com/nt/syntax-replace.html

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.