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 16319 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?

                                Joanlong



                                  Rookie

                                  Re: 2 part question about date in a batch file
                                  « Reply #15 on: November 29, 2009, 06:40:43 PM »
                                  then , Miss ,  can you show a complete batch solution to do both part 1 and 2?

                                  Part 2 is such a minor problem, we will leave as an exercise  for the original Poster.

                                  For example,  Central Time is five hours behind ( depending summer or winter ).

                                  gh0std0g74



                                    Apprentice

                                    Thanked: 37
                                    Re: 2 part question about date in a batch file
                                    « Reply #16 on: November 29, 2009, 06:54:47 PM »
                                    Part 2 is such a minor problem, we will leave as an exercise  for the original Poster.

                                    For example,  Central Time is five hours behind ( depending summer or winter ).
                                    can you at least show pseudocode of how you are going to determine if you are in "Central Time" whatever you call it, or do addition or subtraction to this "five hours"? Or how to determine whether OP is in summer or winter ? Please, miss genius, I doubt OP knows how to do all these PROGRAMMATICALLY. Since you know how, please show us, or is it you are just talk and no show?

                                    Salmon Trout

                                    • Guest
                                    Re: 2 part question about date in a batch file
                                    « Reply #17 on: November 30, 2009, 03:28:26 AM »
                                    You tell him, Ghostdog!

                                    gh0std0g74



                                      Apprentice

                                      Thanked: 37
                                      Re: 2 part question about date in a batch file
                                      « Reply #18 on: November 30, 2009, 04:12:28 AM »
                                      You tell him, Ghostdog!

                                      lol , no i am not in the mood. Besides, i am not the one who knows how to do it in batch :)
                                      he(or she) says its a minor issue and seem to know how to do it, so i will wait for his(her) batch solution.

                                      Salmon Trout

                                      • Guest
                                      Re: 2 part question about date in a batch file
                                      « Reply #19 on: November 30, 2009, 04:46:09 AM »
                                      lol , no i am not in the mood. Besides, i am not the one who knows how to do it in batch :)
                                      he(or she) says its a minor issue and seem to know how to do it, so i will wait for his(her) batch solution.

                                      No - "You tell him!" is a British colloquialism meaning "Bravo for putting him in his place!". Said with the emphasis on "tell".

                                      BC_Programmer


                                        Mastermind
                                      • Typing is no substitute for thinking.
                                      • Thanked: 1140
                                        • Yes
                                        • Yes
                                        • BC-Programming.com
                                      • Certifications: List
                                      • Computer: Specs
                                      • Experience: Beginner
                                      • OS: Windows 11
                                      Re: 2 part question about date in a batch file
                                      « Reply #20 on: November 30, 2009, 05:05:59 AM »
                                      performing Date manipulation in Batch has many flaws:

                                      First: a string has to be parsed- usually from date /t or %date%. Why is this a problem? Because exactly what and how it is parsed depends entirely on regional options, none of which can even be inferred. for example- how do you know wether "Wed 12/01/2009" is January 12th (DD/MM/YYYY) or December 1st? (MM/DD/YYYY)? Logically- we can see that it is the first option, since January 12th 2009 was a Monday. But there is no way to acquire this information programmatically via batch code.

                                      Visual Basic Script uses the OLE automation Variant Date manipulation Functions to perform it's Date manipulation. These routines "know" what the systems regional settings are and know how to parse dates. Date is, a different variant Subtype (and in full blown VB can be used as a explicit type as well) and for good reason. Handling Dates as strings is playing with fire; you simply cannot predict for every possible arrangement of time and date information. If the regional options are set to display as "DD/HH/MM/YYYY/SS" (day/Hour/Minute/Year/Second) then the user should *censored* well be able to input their PREFERRED entry style into scripts that accept date entry; that's what the regional settings are for. There should be no requirement to "enter in this specific format" because "this specific format" should be the bloody regional settings format as set by the USER for purposes of DATE ENTRY.

                                      Additionally, I know why Salmon Trout, at least, would be so animate about this type of thing. the UK uses a different date format by default, and every time one of us USians decides that the default North American format is "good enough" for a batch it's like a little slap in the face, especially when tools such as VBScript are so widely available that solve the issue entirely. And really- it doesn't matter the context- there is NO excuse for ignoring the OS preferences as set by the user.

                                      A analogous situation could be seen with batch when it was only a "pure DOS" type solution, without command extensions. It could NOT do arithmetic at  all.

                                      Did people write batch files to perform arithmetic and parse date strings? No. Because it was bloody hard and not worth the time; when people needed to do math like this they ALWAYS resorted to another solution. This should be the case for performing date manipulation with Batch. If there will be any sort of deployment of a batch file across a company or especially in separate countries, the "good enough" string parsing solutions in batch will not be acceptable, and something else will need to be devised. How much batch code will be necessary simply to properly parse a date string using regional options? None- the problem is intractable in batch code because there is no way to acquire the regional format and there is surely no way to properly use that string (if it is even acquirable) to parse out the components of a date /t output.

                                      The best compromise  solution would be to write a VBScript to perform generic date manipulations and ignore performing such operations in batch at all.
                                      I was trying to dereference Null Pointers before it was cool.

                                      gh0std0g74



                                        Apprentice

                                        Thanked: 37
                                        Re: 2 part question about date in a batch file
                                        « Reply #21 on: November 30, 2009, 05:32:17 AM »
                                        No - "You tell him!" is a British colloquialism meaning "Bravo for putting him in his place!". Said with the emphasis on "tell".

                                        sorry, english is not my native language. what is put him in his place??

                                        Salmon Trout

                                        • Guest
                                        Re: 2 part question about date in a batch file
                                        « Reply #22 on: November 30, 2009, 05:40:53 AM »
                                        sorry, english is not my native language. what is put him in his place??

                                        If someone says something stupid, and you reply in a way that shows how stupid that person is, that is "putting that person in his place".


                                        BC_Programmer


                                          Mastermind
                                        • Typing is no substitute for thinking.
                                        • Thanked: 1140
                                          • Yes
                                          • Yes
                                          • BC-Programming.com
                                        • Certifications: List
                                        • Computer: Specs
                                        • Experience: Beginner
                                        • OS: Windows 11
                                        Re: 2 part question about date in a batch file
                                        « Reply #23 on: November 30, 2009, 05:45:57 AM »
                                        sorry, english is not my native language.

                                        really?

                                        Could have fooled me!  :)
                                        I was trying to dereference Null Pointers before it was cool.

                                        Salmon Trout

                                        • Guest
                                        Re: 2 part question about date in a batch file
                                        « Reply #24 on: November 30, 2009, 05:48:29 AM »

                                        gh0std0g74



                                          Apprentice

                                          Thanked: 37
                                          Re: 2 part question about date in a batch file
                                          « Reply #25 on: November 30, 2009, 06:19:28 AM »
                                          really?

                                          Could have fooled me!  :)
                                          lol, yes, english is not my native language. Only after years of writing/practicing with it that i can write half as good as you(or anyone else) :)

                                          gh0std0g74



                                            Apprentice

                                            Thanked: 37
                                            Re: 2 part question about date in a batch file
                                            « Reply #26 on: November 30, 2009, 06:21:19 AM »
                                            If someone says something stupid, and you reply in a way that shows how stupid that person is, that is "putting that person in his place".
                                            i see. lol i get it now. I thought you are telling me to write the code and show "him". 

                                            Sidewinder



                                              Guru

                                              Thanked: 139
                                            • Experience: Familiar
                                            • OS: Windows 10
                                            Re: 2 part question about date in a batch file
                                            « Reply #27 on: November 30, 2009, 07:14:29 AM »
                                            VBScript may not be the best tool in this situation as there is no native UTC support. JScript on the otherhand has a few UTC methods, one of which may be helpful. JScript, along with it's companion VBScript gets installed when Windows in installed (post Win95). By creating a Windows Script File (WSF), it is possible to use the features of both languages and let the PC do all the work with a minimum of code.

                                            Code: [Select]
                                            <job id="UTCDateandTime">
                                               <script language="JScript">
                                                  function ConvertDateToUTCString(dtmDate)
                                                    {
                                                      var d;                                        //Declare variables.
                                                      d = new Date(dtmDate);                        //Create Date object.
                                                      return(d.toUTCString());                      //Convert to UTC string.
                                                    }
                                               </script>

                                               <script language="VBScript">
                                                  strUTC = ConvertDateToUTCString(Now())
                                                  WScript.Echo strUTC
                                               </script>
                                            </job>

                                            Save the script with a .WSF extension and run from the command prompt:
                                            cscript scriptname.wsf

                                            By using the VBScript Now() function you can guarantee the JScript function receives the current local date and time. No need for The World Clock – Time Zone Converter.

                                            Currently the script echos the variable containing the UTC string to the console. One of the VBScript mavens on this board should be able to slice and dice the UTC string into an acceptable file label for the OP's zip file.

                                            Good luck. 8)
                                            The true sign of intelligence is not knowledge but imagination.

                                            -- Albert Einstein

                                            Salmon Trout

                                            • Guest
                                            Re: 2 part question about date in a batch file
                                            « Reply #28 on: November 30, 2009, 07:38:55 AM »
                                            Sidewinder, I needed to insert a line thus

                                            <job>

                                            immediately after the first line to avoid this error


                                            Code: [Select]
                                            (15, 4) Windows Script Host: The end tag does not have a corresponding start tag : job

                                            Salmon Trout

                                            • Guest
                                            Re: 2 part question about date in a batch file
                                            « Reply #29 on: November 30, 2009, 07:47:50 AM »
                                            If the user has registry read permission they can use vbs

                                            Code: [Select]
                                            od = now()
                                            set oShell = CreateObject("WScript.Shell")
                                            atb = "HKEY_LOCAL_MACHINE\System\CurrentControlSet\" &_
                                            "Control\TimeZoneInformation\ActiveTimeBias"
                                            offsetMin = oShell.RegRead(atb)
                                            StrUTC = dateadd("n", offsetMin, od)
                                            Wscript.Echo(StrUTC) 

                                            Salmon Trout

                                            • Guest
                                            Re: 2 part question about date in a batch file
                                            « Reply #30 on: November 30, 2009, 07:51:39 AM »
                                            Code: [Select]

                                            S:\Test>cscript utc1.wsf
                                            Mon, 30 Nov 2009 14:50:24 UTC

                                            S:\Test>cscript utc1.vbs
                                            30/11/2009 14:50:28


                                            Sidewinder



                                              Guru

                                              Thanked: 139
                                            • Experience: Familiar
                                            • OS: Windows 10
                                            Re: 2 part question about date in a batch file
                                            « Reply #31 on: November 30, 2009, 08:14:26 AM »
                                            Sidewinder, I needed to insert a line thus

                                            <job>

                                            immediately after the first line to avoid this error


                                            Code: [Select]
                                            (15, 4) Windows Script Host: The end tag does not have a corresponding start tag : job

                                            Not sure why you had to insert a job statement immediately after the first line. The error is flagged on line 15, and in fact my posted script had both and opening <Job> and closing </job> statements.

                                            Personally, I prefer not to post scripts that utilize the registry. One small typo can have serious repercussions,  8)
                                            The true sign of intelligence is not knowledge but imagination.

                                            -- Albert Einstein

                                            Salmon Trout

                                            • Guest
                                            Re: 2 part question about date in a batch file
                                            « Reply #32 on: November 30, 2009, 08:21:28 AM »
                                            Quote from: Sidewinder
                                            Not sure why you had to insert a job statement immediately after the first line. The error is flagged on line 15, and in fact my posted script had both and opening <Job> and closing </job> statements.

                                            When I pasted it into UltraEdit, I had missed the opening < at the very start of the first line. Mea culpa.


                                            JohnBergt

                                              Topic Starter


                                              Greenhorn

                                              Re: 2 part question about date in a batch file
                                              « Reply #33 on: November 30, 2009, 08:57:26 AM »
                                              Wow, I didn't realize I was hitting such a sensitive subject.

                                              The purpose for the time conversion is for doing a back up, like I said in my initial post. There are certain files which have a very specific naming format and this is why I have the DDMMMYY.WXT format. The date is based on UTC date, this is why i asked for the time/date conversion to UTC. I am not averse to using a VB script as opposed to a batch file, i just don't know how to write it. I am open to suggestions.

                                              this is my batch file as it stands today, this whole thing runs in a hosted environment, so I don't know about access to registries, etc. :

                                              @echo off
                                              cls

                                              setlocal enabledelayedexpansion


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

                                              for /f "tokens=%date:~4,2%" %%1 in ("%alphamonth%") do (
                                                  set month=%%1
                                              )

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


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

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

                                              set time=%hour%%min%


                                              E:
                                              CD \FCS_FPS\OCA\OPS_BACKUP\TEMP\
                                              BUTIL -STARTBU @E:\FCS_FPS\OCA\OPS_BACKUP\STARTLST.FIL
                                              XCOPY E:\FCS_FPS\OCA\OPS\FLTCTRL\DATA\*.DAT E:\FCS_FPS\OCA\OPS_BACKUP\TEMP\ /EXCLUDE:E:\FCS_FPS\OCA\OPS_BACKUP\EXCLUDE.LST
                                              COPY E:\FCS_FPS\OCA\OPS\FLTCTRL\DATA\%today%*.WXT E:\FCS_FPS\OCA\OPS_BACKUP\TEMP\
                                              BUTIL -ENDBU @E:\FCS_FPS\OCA\OPS_BACKUP\STARTLST.FIL



                                              E:\FCS_FPS\OCA\OPS_BACKUP\7ZIP\7ZA A E:\FCS_FPS\OCA\OPS_BACKUP\ZIPS\%today%_%time%.ZIP E:\FCS_FPS\OCA\OPS_BACKUP\TEMP\*.DAT
                                              E:\FCS_FPS\OCA\OPS_BACKUP\7ZIP\7ZA A E:\FCS_FPS\OCA\OPS_BACKUP\ZIPS\%today%_%time%.ZIP E:\FCS_FPS\OCA\OPS_BACKUP\TEMP\*.WXT

                                              DEL E:\FCS_FPS\OCA\OPS_BACKUP\TEMP\*.DAT
                                              DEL E:\FCS_FPS\OCA\OPS_BACKUP\TEMP\*.WXT

                                              COPY E:\FCS_FPS\OCA\OPS_BACKUP\ZIPS\%today%_%time%.ZIP \\EMGHWP079\E$\FCS_FPS\OCA\OPS_BACKUP\ZIPS\

                                              Joanlong



                                                Rookie

                                                Re: 2 part question about date in a batch file
                                                « Reply #34 on: November 30, 2009, 09:17:43 AM »

                                                This is my batch file as it stands today, this whole thing runs in a hosted environment, so I don't know about access to registries, etc. :

                                                @echo off
                                                cls

                                                setlocal enabledelayedexpansion


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

                                                for /f "tokens=%date:~4,2%" %%1 in ("%alphamonth%") do (
                                                    set month=%%1
                                                )

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


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

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

                                                set time=%hour%%min%


                                                E:
                                                CD \FCS_FPS\OCA\OPS_BACKUP\TEMP\
                                                BUTIL -STARTBU @E:\FCS_FPS\OCA\OPS_BACKUP\STARTLST.FIL
                                                XCOPY E:\FCS_FPS\OCA\OPS\FLTCTRL\DATA\*.DAT E:\FCS_FPS\OCA\OPS_BACKUP\TEMP\ /EXCLUDE:E:\FCS_FPS\OCA\OPS_BACKUP\EXCLUDE.LST
                                                COPY E:\FCS_FPS\OCA\OPS\FLTCTRL\DATA\%today%*.WXT E:\FCS_FPS\OCA\OPS_BACKUP\TEMP\
                                                BUTIL -ENDBU @E:\FCS_FPS\OCA\OPS_BACKUP\STARTLST.FIL



                                                E:\FCS_FPS\OCA\OPS_BACKUP\7ZIP\7ZA A E:\FCS_FPS\OCA\OPS_BACKUP\ZIPS\%today%_%time%.ZIP E:\FCS_FPS\OCA\OPS_BACKUP\TEMP\*.DAT
                                                E:\FCS_FPS\OCA\OPS_BACKUP\7ZIP\7ZA A E:\FCS_FPS\OCA\OPS_BACKUP\ZIPS\%today%_%time%.ZIP E:\FCS_FPS\OCA\OPS_BACKUP\TEMP\*.WXT

                                                DEL E:\FCS_FPS\OCA\OPS_BACKUP\TEMP\*.DAT
                                                DEL E:\FCS_FPS\OCA\OPS_BACKUP\TEMP\*.WXT

                                                COPY E:\FCS_FPS\OCA\OPS_BACKUP\ZIPS\%today%_%time%.ZIP \\EMGHWP079\E$\FCS_FPS\OCA\OPS_BACKUP\ZIPS\

                                                Here is the Output when the above code is execuded:

                                                The system cannot find the path specified.
                                                'BUTIL' is not recognized as an internal or external command,
                                                operable program or batch file.
                                                Can't read file: E:\FCS_FPS\OCA\OPS_BACKUP\EXCLUDE.LST

                                                0 File(s) copied
                                                The system cannot find the path specified.
                                                'BUTIL' is not recognized as an internal or external command,
                                                operable program or batch file.
                                                The system cannot find the path specified.

                                                C:\batch>

                                                Would you show the output when your code is run on your machine?

                                                I suggest you look at post # 9.

                                                Or study anything Sidewinder writes.

                                                You may safely ignore everyone else.

                                                Salmon Trout

                                                • Guest
                                                Re: 2 part question about date in a batch file
                                                « Reply #35 on: November 30, 2009, 09:24:24 AM »
                                                Joanlong, aka Billrich, the resident troll.

                                                gh0std0g74



                                                  Apprentice

                                                  Thanked: 37
                                                  Re: 2 part question about date in a batch file
                                                  « Reply #36 on: November 30, 2009, 09:38:11 AM »
                                                  You may safely ignore everyone else.
                                                  lol , that means including you right?

                                                  Joanlong



                                                    Rookie

                                                    Re: 2 part question about date in a batch file
                                                    « Reply #37 on: November 30, 2009, 09:38:33 AM »
                                                    Joanlong the resident . . . .l.


                                                    The Standard Operating Procedure for the ComputerHope Board:

                                                    When ComputerHope members are unable to answer your questions:

                                                    ComputerHope members call everybody Bad Names.

                                                    « Last Edit: November 30, 2009, 09:57:15 AM by Joanlong »

                                                    Salmon Trout

                                                    • Guest
                                                    Re: 2 part question about date in a batch file
                                                    « Reply #38 on: November 30, 2009, 09:54:46 AM »
                                                    The Standard Operating Procedure for the ComputerHope Board:

                                                    When ComputerHope members are unable to answer your questions:

                                                    ComputerHope members call everybody Bad Names.

                                                    Name calling is the  refuge of the ComputerHope Cowards.

                                                    ...and "ComputerHope Cowards" is a name.

                                                    T.C.



                                                      Beginner

                                                      Thanked: 13
                                                      Re: 2 part question about date in a batch file
                                                      « Reply #39 on: November 30, 2009, 11:48:17 AM »
                                                      JohnBergt - The script you posted in Reply 33 will fail if the month variable contains 08 or 09 (August or September) and, if the time is between 11.59 am and 1.00 pm, the hour will be returned as 24.  Please see my reply #5 posted Nov 26 for amended script.




                                                      Joanlong



                                                        Rookie

                                                        Re: 2 part question about date in a batch file
                                                        « Reply #40 on: November 30, 2009, 01:39:47 PM »

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

                                                        Sidewinder (Post #27 in this Thread ) gave the perfect answer and we failed to test the code ( Or thank Sidewinder ):


                                                        C:\batch>cscript sidewind.wsf

                                                        Microsoft (R) Windows Script Host Version 5.7
                                                        Copyright (C) Microsoft Corporation. All rights reserved.

                                                         :D
                                                        Mon, 30 Nov 2009 20:34:15 UTC
                                                         :D

                                                           Thank Sidewinder for their post.
                                                           

                                                        C:\batch>time /t
                                                        02:34 PM

                                                        C:\batch>


                                                        C:\batch>type sidewind.wsf

                                                        Code: [Select]
                                                        <job id="UTCDateandTime">
                                                           <script language="JScript">
                                                              function ConvertDateToUTCString(dtmDate)
                                                                {
                                                                  var d;                                        //Declare variables.
                                                                  d = new Date(dtmDate);                        //Create Date object.
                                                                  return(d.toUTCString());                      //Convert to UTC string.

                                                                }
                                                           </script>

                                                           <script language="VBScript">
                                                              strUTC = ConvertDateToUTCString(Now())
                                                              WScript.Echo strUTC
                                                           </script>
                                                        </job>


                                                        rem Save the script with a .WSF extension and run from the command prompt:
                                                        rem  cscript sidewind.wsf
                                                        C:\batch>


                                                        p.s.
                                                           Thank Sidewinder for their post. 
                                                        Sidewinder is far ahead of the rest of us


                                                        http://en.wikipedia.org/wiki/Windows_Script_Host
                                                        « Last Edit: November 30, 2009, 02:08:34 PM by Joanlong »

                                                        Sidewinder



                                                          Guru

                                                          Thanked: 139
                                                        • Experience: Familiar
                                                        • OS: Windows 10
                                                        Re: 2 part question about date in a batch file
                                                        « Reply #41 on: November 30, 2009, 03:21:07 PM »
                                                        JohnBergt ,

                                                        Your batch file should be changed:
                                                        Code: [Select]
                                                        @echo off
                                                        cls

                                                        for /f %%a in ('cscript //nologo scriptname.wsf') do (
                                                          set zipFile=%%a
                                                          )

                                                        setlocal enabledelayedexpansion


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

                                                        for /f "tokens=%date:~4,2%" %%1 in ("%alphamonth%") do (
                                                            set month=%%1
                                                        )

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


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

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

                                                        set time=%hour%%min%

                                                        E:
                                                        CD \FCS_FPS\OCA\OPS_BACKUP\TEMP\
                                                        BUTIL -STARTBU @E:\FCS_FPS\OCA\OPS_BACKUP\STARTLST.FIL
                                                        XCOPY E:\FCS_FPS\OCA\OPS\FLTCTRL\DATA\*.DAT E:\FCS_FPS\OCA\OPS_BACKUP\TEMP\ /EXCLUDE:E:\FCS_FPS\OCA\OPS_BACKUP\EXCLUDE.LST
                                                        COPY E:\FCS_FPS\OCA\OPS\FLTCTRL\DATA\%today%*.WXT E:\FCS_FPS\OCA\OPS_BACKUP\TEMP\
                                                        BUTIL -ENDBU @E:\FCS_FPS\OCA\OPS_BACKUP\STARTLST.FIL



                                                        E:\FCS_FPS\OCA\OPS_BACKUP\7ZIP\7ZA A E:\FCS_FPS\OCA\OPS_BACKUP\ZIPS\%zipFile% E:\FCS_FPS\OCA\OPS_BACKUP\TEMP\*.DAT
                                                        E:\FCS_FPS\OCA\OPS_BACKUP\7ZIP\7ZA A E:\FCS_FPS\OCA\OPS_BACKUP\ZIPS\%zipFile% E:\FCS_FPS\OCA\OPS_BACKUP\TEMP\*.WXT

                                                        DEL E:\FCS_FPS\OCA\OPS_BACKUP\TEMP\*.DAT
                                                        DEL E:\FCS_FPS\OCA\OPS_BACKUP\TEMP\*.WXT

                                                        COPY E:\FCS_FPS\OCA\OPS_BACKUP\ZIPS\%zipFile% \\EMGHWP079\E$\FCS_FPS\OCA\OPS_BACKUP\ZIPS\

                                                        The Windows Script File should be changed to:
                                                        Code: [Select]
                                                        <job id="UTCTime">
                                                           <script language="JScript">
                                                              function ConvertDateToUTCString(dtmDate)
                                                                {
                                                                  var d;                                        //Declare variables.
                                                                  d = new Date(dtmDate);                        //Create Date object.
                                                                  return(d.toUTCString());                      //Convert to UTC string.
                                                                }
                                                           </script>

                                                           <script language="VBScript">
                                                              strUTC = ConvertDateToUTCString(Now())
                                                              arrUTC = Split(strUTC, " ")
                                                             
                                                              strYR = arrUTC(3)
                                                              strMon = UCase(arrUTC(2))
                                                              strDay = arrUTC(1)
                                                              If Len(strDay) = 1 Then strDay = "0" & strDay
                                                             
                                                              arrTime = Split(arrUTC(4), ":")
                                                              strHH = arrTime(0)
                                                              strMM = arrTime(1)
                                                           
                                                              zipLabel = strYR & strMon & strDay & "-" & strHH & strMM & ".zip"
                                                              WScript.Echo zipLabel
                                                           </script>
                                                        </job>

                                                        Save the script file with a WSF extension. Note: the scriptname referenced in the batch file must match the named you save the script as.

                                                        The label of the zip file can be referenced as %zipFile%.

                                                        I left your original batch logic in place while adding the reference to the script file because I couldn't come up with a clean way to resolve %today%*.WXT

                                                        Good luck.  8)

                                                        This is truly an ugly run unit, but since we already used JScript and VBScript in the same WSF file, it's probably not a great leap to wrap everything around some batch code.;D
                                                        « Last Edit: November 30, 2009, 04:43:14 PM by Sidewinder »
                                                        The true sign of intelligence is not knowledge but imagination.

                                                        -- Albert Einstein