Announcement

Collapse
No announcement yet.

.cmd replace mutliple characters

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • .cmd replace mutliple characters

    Hi,

    I'm using a script to do some periodic performance logging of a NetApp filer, the script works like a charm, but there is one thing I can't get fixed...

    I use this syntax in the script to manipulate the date and time to use it as the filename for the log.

    Code:
    set "DateTimeName=%DATE:/=-%_%TIME::=.%"
    set "DateTimeName=%DateTimeName:~3,-6%"
    copy "C:\temp\temp.log" "C:\temp\%datetimename%.log"
    The invalid characters "/" and ":" are replaced and the output is like this: "28-05-2008_11.59", so far so good, but with single-digit hours, there is a space before the digit, which can be used in a filename, but sometimes causes problems with further manipulation of the file. ("28-05-2008_ 9.59")

    I can replace the space with the following syntax:
    Code:
    ..._%TIME: =0%
    Which leads to a leading zero, before the hour-digit, but this does not replace the ":" so it can't be used as a filename...

    Question: Is it possible to replace both the space and the ":" of the time-variable, or is there another syntax to create a leading "0"?

    Thanks in advance

  • #2
    Re: .cmd replace mutliple characters

    In stead of echo.%time% you can use the command time/t. This will show the the leading 0 - but it does not show seconds!

    In a batch:
    Code:
    @echo off
    
    ::  _tf = "AM" or "PM" (only defined when using a 12h-clock)
    ::  _h = "hours"
    ::  _m = "minutes"
    
    For /f "tokens=1-3 delims=-: " %%m in ('time /t') do (
          (set _tf=%%o)&(set _h=%%m)&(set _m=%%n)
     )
    If Defined _tf (Set _h=%_tf%-%_h%)
    
    Echo.Timestamp: %_h%.%_m%

    \Rems
    Last edited by Rems; 28th May 2008, 14:17.

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: .cmd replace mutliple characters

      Probably there is a better way of doing this, but the following seam to work:
      Code:
      @echo off
      
      set MYTIME=%TIME: =0%
      set MYTIME=%MYTIME::=.%
      set MYTIME=%MYTIME:~0,-6%
      echo TIME: %MYTIME%
      
      set MYDATE=%DATE:/=-%
      set MYDATE=%MYDATE:~4,100%
      echo DATE: %MYDATE%
      
      
      set "DateTimeName=%MYDATE%_%MYTIME%"
      echo %DateTimeName%
      Guy Teverovsky
      "Smith & Wesson - the original point and click interface"

      Comment


      • #4
        Re: .cmd replace mutliple characters

        Thanks for the quick response!

        I think the solution provided by Rems is the better way of handling this, but I'm not that familiar with command-line scripting to fully understand the syntax of it.
        And I'm sure my collegues will understand even less of it

        The other alternative (from Guyt) is something I can completely understand and I have just altered my script a bit, now it runs really smooth!

        Next task is learn to understand the complete syntax of the "tokens" and "delims" commands...
        Last edited by diekstra; 28th May 2008, 16:20.

        Comment


        • #5
          Re: .cmd replace mutliple characters

          At line:
          set MYDATE=%MYDATE:~4,100%
          The number ~4 is cutting for my OS-language to many leading characters.
          Maybe we can instead use just the last 10 characters of the variable?:
          set MYDATE=%MYDATE:~-10%



          Originally posted by diekstra
          <...>
          Next task is learn to understand the complete syntax of the "tokens" and "delims" commands...
          With a For-Do statement you can also change the date format to yyyy-mm-dd, this wil make the files (date string) better sortable,
          http://forums.petri.com/showthread.p...highlight=date

          Code:
          :: http://www.ss64.com/ntsyntax/getdate.html
          
          @echo off
          
          for /f "tokens=2-4 skip=1 delims=(-./)" %%i in ('echo.^|date') do (
             for /f "tokens=1-4 delims=-./ " %%m in ('date /t') do (
                (set dow=%%m)&(set %%i=%%n)&(set %%j=%%o)&(set yy=%%p)
              )
           ) 
          
          echo DATE: %yy%-%mm%-%dd%


          \Rems


          EDIT
          yes, You can also do this by the use of substrings to expand the Date variable:
          e.g. (for Dutch OSs) echo.Date (jjjj - mm - dd): %date:~-4% - %date:~6,2% - %date:~3,2%
          But if would be language dependent doing it this way
          .
          Last edited by Rems; 28th May 2008, 17:17.

          This posting is provided "AS IS" with no warranties, and confers no rights.

          __________________

          ** Remember to give credit where credit's due **
          and leave Reputation Points for meaningful posts

          Comment


          • #6
            Re: .cmd replace mutliple characters

            Originally posted by Rems View Post
            At line:
            set MYDATE=%MYDATE:~4,100%
            The number ~4 is cutting for my OS-language to many leading characters.
            Maybe we can instead use just the last 10 characters of the variable?:
            It depends on your regional settings and the date/time formatting in use.
            Personally I'd use VBS to accomplish the task to remove the dependencies.

            This is what I have:
            Code:
            C:\>echo %date%
            Thu 29/05/2008
            Guy Teverovsky
            "Smith & Wesson - the original point and click interface"

            Comment


            • #7
              Re: .cmd replace mutliple characters

              http://gnuwin32.sourceforge.net/packages/sed.htm
              this is what I use
              ________
              SMOKE WEED EVERY DAY
              Last edited by DYasny; 6th March 2011, 18:55.
              Real stupidity always beats Artificial Intelligence (c) Terry Pratchett

              BA (BM), RHCE, MCSE, DCSE, Linux+, Network+

              Comment


              • #8
                Re: .cmd replace mutliple characters

                I use the whole bunch of those: http://unxutils.sourceforge.net/.
                Guy Teverovsky
                "Smith & Wesson - the original point and click interface"

                Comment

                Working...
                X