Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: 2 part question about date in a batch file  (Read 16566 times)

0 Members and 1 Guest are viewing this topic.

JohnBergt

    Topic Starter


    Greenhorn

    2 part question about date in a batch file
    « on: November 25, 2009, 09:04:59 AM »
    Part 1:

    I am using the following to create a zip file with a specific name:

    @echo off
    for /f "tokens=2-4" %%i in ('echo %date%') do (
     set today=%%i
    )
    for /f "tokens=1-3 delims=/" %%a in ('echo %today%') do (
     set month=%%a
     set day=%%b
     set year=%%c
    )

    for /f "tokens=1-4 delims=.: " %%i in ("%time%") do (
     set hour=%%i
     set minute=%%j
    )

    if %hour% LSS 10 set hour=0%hour%


    I use %year%%month%%day%_%hour%%minute%.ZIP as the file name parameter, which gives me 20091125_0905.ZIP, for example.  I want to change this so the output is in the DDMMMYY_HHmm.ZIP format, so the output would be 25NOV09_0905.ZIP.  How do I need to reformat above, or use in it's place? I am using this in a bacth file to back up some files.

    Part 2:

    I would like to output both the date and time in UTC, instead of local time.

    MikeTaylor



      Rookie

      Re: 2 part question about date in a batch file
      « Reply #1 on: November 25, 2009, 02:47:29 PM »
      rem We did not show the different format for file name, yet?

      C:\batch>cat showdate.bat

      Code: [Select]
      @echo off
      setlocal enabledelayedexpansion
      for /f "tokens=2-4" %%i in ('echo %date%') do (
      set today=%%i
      echo !today!
      echo %%i
      )
      for /f "tokens=1-3 delims=/" %%a in ('echo !today!') do (
       set month=%%a
       set day=%%b
       set year=%%c
      echo month=!month!
      echo day=!day!
      echo year=!year!
      )

      for /f "tokens=1-4 delims=.: " %%i in ("%time%") do (
       set hour=%%i
       set minute=%%j

      echo hour=!hour!
      echo minute=!minute!

      echo !year!!month!!day!_!hour!!minute!.ZIP
      echo  !day!!month!!year!_!hour!!minute!.ZIP
      )


      rem set hour=9

      rem echo hour=!hour!

      rem if !hour! LSS 10 set  hour=0!hour!

      rem echo hour=!hour!


      rem I use %year%%month%%day%_%hour%%minute%.ZIP as the file name parameter,
      rem which gives me 20091125_0905.ZIP, for example.
      rem I want to change this so the output is in the DDMMMYY_HHmm.ZIP format,
      rem so the output would be 25NOV09_0905.ZIP.  How do I need to reformat above,
      rem or use in it's place? I am using this in a bacth file to back up some files.



      OUTPUT:

      C:\batch>showdate.bat
      11/25/2009
      11/25/2009
      month=11
      day=25
      year=2009
      hour=15
      minute=39
      20091125_1539.ZIP
       25112009_1539.ZIP

      C:\batch>
      Bill Richardson

      T.C.



        Beginner

        Thanked: 13
        Re: 2 part question about date in a batch file
        « Reply #2 on: November 25, 2009, 03:29:29 PM »
        Quote from: JohnBergt
        Part 2:

        I would like to output both the date and time in UTC, instead of local time.

        I doubt if this is achievable using only batch scripting, VBS might be better for this part, perhaps one of the VBS gurus will drop in.

        Meantime here's another version of batching to create the filename in the format you requested using the 24-hour clock (aka Military Time).   The underlined command lines should be removed when you are finished testing.   Your date format is assumed to be 'day mm/dd/yyyy' and the time format as 'hh:mm:ss.ms AM (or PM)',  When the hour is less than 10 it's assumed it is shown as one digit.

        Code: [Select]
        Script removed.

        Edit: See amended script below....
        « Last Edit: November 26, 2009, 11:40:53 PM by T.C. »

        macdad-



          Expert

          Thanked: 40
          Re: 2 part question about date in a batch file
          « Reply #3 on: November 25, 2009, 03:32:55 PM »
          As a suggestion you could try extracting it with the Set command  ;)
          Code: [Select]
          @echo off
          set nummonth=%Date:~4,2%
          set day=%Date:~7,2%
          set year=%Date:~12,2%
          set hour=%Time:~2%
          set min=%Time:~3,2%

          if %hour% LSS 10 set hour=0%hour%

          for /f "tokens=%nummonth%" %%a in ('echo JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC') do (
           set wordmonth=%%a
          )

          Then for the ZIP paramater you can just put:
          Code: [Select]
          %day%%wordmonth%%year%_%hour%%min%.ZIP
          For part two, you would have to use a 3rd party program or perhaps Cscript.

          I'll get back with the Cscript instructions.

          Hope this helps
          ,Nick(macdad-)
          If you dont know DOS, you dont know Windows...

          Thats why Bill Gates created the Windows NT Family.

          JohnBergt

            Topic Starter


            Greenhorn

            Re: 2 part question about date in a batch file
            « Reply #4 on: November 25, 2009, 05:08:29 PM »
            Thanks T.C. and macdad. T.C.'s method worked well. Macdad, your method left some extra spaces and inserted a : into the file name, so that was invalid. Thanks to everyone for answering.

            T.C.



              Beginner

              Thanked: 13
              Re: 2 part question about date in a batch file
              « Reply #5 on: November 26, 2009, 01:30:28 AM »
              T.C.'s method worked well.

              You spoke to soon John, there is a glaring error in my script.

              In the command line
              Code: [Select]
              for /f "tokens=%date:~4,2%" %%1 in ("%alphamonth%") do (
                  set month=%%1
              )
              if the month is 08 or 09 (Aug or Sept) tokens will fail probably due to 08 and 09 being invalid octal numbers.   Amended script posted below, sorry about the error.

              Code: [Select]
              @echo off
              cls

              setlocal enabledelayedexpansion

              set date=fri 09.26.2009
              :: --------------------

              set alphamonth=JAN FEB MAR APL MAY JUN JUL AUG SEP OCT NOV DEC

              if %date:~4,2% lss 10 (set monthnbr=%date:~5,1%
                 ) else (
                 set monthnbr=%date:~4,2%
              )

              for /f "tokens=%monthnbr%" %%1 in ("%alphamonth%") do (
                  set month=%%1
              )

              set today=%date:~7,2%%month%%date:~-2%

              set time=1:12:13.14 am
              :: -------------------

              for /f "tokens=1-4 delims=: " %%1 in ("%time%") do (
                  set hour=%%1
                  set min=%%2
                  if /i %%4 equ pm if %%1 lss 12 set /a Hour +=12
              )

              if %hour% lss 10 set hour=0%hour%

              set time=%hour%%min%

              echo.&echo.
              echo                       Today=%today%  Time=%time%
              echo.&echo.
              echo                       Filename=%today%_%time%.zip


               

              Script edited to allow for correct display of times between 12.00 (noon) and 1.00 pm
              « Last Edit: November 26, 2009, 11:35:43 PM by T.C. »

              gh0std0g74



                Apprentice

                Thanked: 37
                Re: 2 part question about date in a batch file
                « Reply #6 on: November 26, 2009, 02:53:27 AM »
                date manipulation has been discussed countless times. Don't use batch for date manipulations. try learning and using vbscript if you are a native guy, or get a real programming language, Perl/Python are the ones you need.

                macdad-



                  Expert

                  Thanked: 40
                  Re: 2 part question about date in a batch file
                  « Reply #7 on: November 26, 2009, 06:35:41 AM »
                  date manipulation has been discussed countless times. Don't use batch for date manipulations. try learning and using vbscript if you are a native guy, or get a real programming language, Perl/Python are the ones you need.

                  I believe this vbscript should do the job.
                  If you dont know DOS, you dont know Windows...

                  Thats why Bill Gates created the Windows NT Family.

                  T.C.



                    Beginner

                    Thanked: 13
                    Re: 2 part question about date in a batch file
                    « Reply #8 on: November 26, 2009, 12:04:51 PM »
                    Don't use batch for date manipulations. try learning and using vbscript

                    Sorry, I cannot agree with that.  While it is a good idea to learn VBScript etc., when a member asks for a solution to a problem using batch scripting he/she should be assisted to achieve that solution in the scripting language of his/her choice not told to go off and learn another language with which he/she may have no experience.  Certainly batch scripting is very limited in some areas, and date manipulation is one of them, but if the request is for assistance in a batch script that assistance should be forthcoming where it is possible.  Then, and only then, should the suggestion be made to expand into VBS etc...

                    Joanlong



                      Rookie

                      Re: 2 part question about date in a batch file
                      « Reply #9 on: November 26, 2009, 02:58:15 PM »
                      Batch Works.

                      Code: [Select]
                      @echo off
                       
                      setlocal enabledelayedexpansion
                       

                      date /t >  sdate.txt
                      set /p sdate=<sdate.txt
                      echo sdate=!sdate!
                       
                       
                       
                      set alphamonth=JAN FEB MAR APL MAY JUN JUL AUG SEP OCT NOV DEC
                       
                      set tokens=%sdate:~4,2%
                       
                      echo tokens=!tokens!
                       
                       
                       
                      for /f "tokens=%date:~4,2%" %%a in ("%alphamonth%") do (
                          set month=%%a
                          echo month=!month!
                      )
                       
                      set today=%date:~7,2%%month%%date:~-2%
                       

                      time /t > stime.txt
                      set /p stime=<stime.txt
                       
                      echo stime=!stime!
                       

                      for /f "tokens=1-4 delims=: " %%a in ("%stime%") do (
                          set hour=%%a
                          set min=%%b
                          set pmm=%%c
                      echo hour=!hour!
                      echo min=!min!
                      echo  PM=!pmm!
                       
                          if  !pmm! equ PM set /a hour=!hour! + 12
                       
                      echo hour = !hour!
                      )
                       
                      if %hour% lss 10 set hour=0%hour%
                       
                      set time=%hour%%min%
                       
                      echo.&echo.
                      echo                       Today=%today%  Time=%time%
                      echo.&echo.
                      echo                       Filename=%today%_%time%.zip
                       


                      Output:
                       

                      C:\>fdate.bat

                       sdate=Wed 11/25/2009
                      tokens=11
                      month=NOV
                      stime=11:40 PM
                      hour=11
                      min=40
                       PM=PM
                      hour = 23
                       

                                            Today=25NOV09  Time=2340
                       

                                            Filename=25NOV09_2340.zip

                      Output for 11/26/2009:


                      C:\test>joan.bat
                       sdate=Thu 11/26/2009
                      tokens=11
                      month=NOV
                      stime=04:15 PM
                      hour=04
                      min=15
                       PM=PM
                      hour = 16


                                            Today=26NOV09  Time=1615


                                            Filename=26NOV09_1615.zip
                      C:\test>
                       
                      « Last Edit: November 29, 2009, 01:36:56 PM by Joanlong »

                      gh0std0g74



                        Apprentice

                        Thanked: 37
                        Re: 2 part question about date in a batch file
                        « Reply #10 on: November 26, 2009, 06:10:37 PM »
                        Sorry, I cannot agree with that. 
                        you do not agree because i said "Don't use batch for date manipulation"? or you don't agree that i asked OP to learn and use something else that is better at doing this job?

                        Quote
                        While it is a good idea to learn VBScript etc., when a member asks for a solution to a problem using batch scripting he/she should be assisted to achieve that solution in the scripting language of his/her choice not told to go off and learn another language with which he/she may have no experience. 
                        you must understand that this is a forum, and we are allowed to suggest BETTER and EASIER alternatives to SOLVE the problem. What really matters is OP's choice of solution, not yours or mine.
                        No experience in learning something he did not know?? If OP has experience in batch, he wouldn't have asked  here already. so what's your point?

                        Quote
                        Certainly batch scripting is very limited in some areas, and date manipulation is one of them, but if the request is for assistance in a batch script that assistance should be forthcoming where it is possible.  Then, and only then, should the suggestion be made to expand into VBS etc...
                        as far as i already know, you have provided a batch (that may not work in every machine due to individual date settings). I/macdad have provided suggestion using vbscript and all that's left is up to OP.  A challenge for you, try producing a batch to convert local to UTC for OP then, if you really meant what you say.
                        « Last Edit: November 26, 2009, 06:34:21 PM by gh0std0g74 »

                        T.C.



                          Beginner

                          Thanked: 13
                          Re: 2 part question about date in a batch file
                          « Reply #11 on: November 26, 2009, 08:24:33 PM »
                          Quote from: GD
                          A challenge for you, try producing a batch to convert local to UTC for OP then, if you really meant what you say.

                          GD - You obviously did not read my first response to JohnBergt, did I not state
                          Quote
                          Quote from: JohnBergt
                          Part 2:

                          I would like to output both the date and time in UTC, instead of local time.

                          I doubt if this is achievable using only batch scripting, VBS might be better for this part, perhaps one of the VBS gurus will drop in.

                          One of the VBS gurus did drop in but unfortunately didn't leave a solution.

                          Quote from: GD
                          as far as i already know, you have provided a batch (that may not work in every machine due to individual date settings).

                          Quite correct, it will not work for date settings other than the ones I stipulated in my first response:
                          Quote
                          Your date format is assumed to be 'day mm/dd/yyyy' and the time format as 'hh:mm:ss.ms AM (or PM)',  When the hour is less than 10 it's assumed it is shown as one digit.

                          How can I make that clearer?  I have no wish to become involved in a drawn out argument about the merits of Batch Scripting vs VBS, I also believe VBS is superior in many ways than Batch but still believe that if a solution to a problem is asked for in batch scripting and that solution can be arrived at then it should be given.

                          Kind regards to all.

                          T.C.

                          « Last Edit: November 26, 2009, 11:31:02 PM by T.C. »

                          gh0std0g74



                            Apprentice

                            Thanked: 37
                            Re: 2 part question about date in a batch file
                            « Reply #12 on: November 26, 2009, 08:47:28 PM »
                            GD - You obviously did not read my first response to JohnBergt, did I not state
                            don't worry, i did read your post before posting my last response.
                            Quote
                            but still believe that if a solution to a problem is asked for in batch scripting and that solution can be arrived at then it should be given.
                            so is there a  clean and understandable batch(cmd.exe) solution given yet to take into account, different regional date settings on different machines, and the ability to convert to UTC time easily?? Your reasoning that batch solution should be given is flawed. most of the time, the ones posting questions here either did not know there are better alternatives, or they just have the mentality that says batch does everything. What's wrong with posting better alternatives that do the job in half the time it takes to do it in batch? you are beginning to sound that the other "hated" person around here. I suggest you focus more on providing solution to the OP and stop meddling with trivial things like this.

                            Joanlong



                              Rookie

                              Re: 2 part question about date in a batch file
                              « Reply #13 on: November 29, 2009, 01:31:25 PM »

                              I would like to output both the date and time in UTC, instead of local time.

                              http://www.timeanddate.com/worldclock/converted.html?month=11&day=29&year=2009&hour=0&min=0&sec=0&p1=0&p2=184

                              The World Clock – Time Zone Converter – results

                              At the specified time, local time in Oklahoma City was 6 hours behind UTC
                              Location   Local time   Time zone
                              UTC   Sunday, November 29, 2009 at 00:00:00   
                              Oklahoma City (U.S.A. - Oklahoma)   Saturday, November 28, 2009 at 6:00:00 PM   UTC-6 hours CST


                              It appears Batch is the best solution for the first part of your question.  See Post # 9

                              UTC is a minor problem

                              gh0std0g74



                                Apprentice

                                Thanked: 37
                                Re: 2 part question about date in a batch file
                                « Reply #14 on: November 29, 2009, 05:36:34 PM »
                                then , Miss genius,  can you show a complete batch solution to do both part 1 and 2?