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

Author Topic: Batch file to change to a specific date  (Read 38127 times)

0 Members and 1 Guest are viewing this topic.

The Artilleryman

    Topic Starter


    Starter

    • Experience: Beginner
    • OS: Unknown
    Batch file to change to a specific date
    « on: January 11, 2012, 05:04:44 AM »
    I need to write a batch file so I can set the date to start a program, but I then need to change back to the current date after the program has loaded. I've not written any batch files in a long time and can't work out how to do it.

    Squashman



      Specialist
    • Thanked: 134
    • Experience: Experienced
    • OS: Other
    Re: Batch file to change to a specific date
    « Reply #1 on: January 11, 2012, 07:53:01 AM »
    This is dependent on your regional and language settings.
    My date format is set to only output a 2 digit year yours is probably a 4 digit year.
    Code: [Select]
    H:\>echo %date%
    Wed 01/11/12

    So for a two digit year you could do this.
    Code: [Select]
    SET TODAY=%date:~4,2%-%date:~7,2%-%date:~10,2%
    REM setting date back to an older date
    DATE 12-11-11
    start "" "C:\path to my program\myprogram.exe"
    REM Changing date back to todays date
    DATE %TODAY%
    If you have a 4 digit year change the last DATE string to %date:~10,4%

    Salmon Trout

    • Guest
    Re: Batch file to change to a specific date
    « Reply #2 on: January 11, 2012, 09:59:42 AM »
    I need to write a batch file so I can set the date to start a program,

    Why? Has the trial period expired?

    The Artilleryman

      Topic Starter


      Starter

      • Experience: Beginner
      • OS: Unknown
      Re: Batch file to change to a specific date
      « Reply #3 on: January 11, 2012, 10:21:31 AM »
      It's a parts catalogue, new one issued every month, no longer got access to latest one so having to use an old one.

      The Artilleryman

        Topic Starter


        Starter

        • Experience: Beginner
        • OS: Unknown
        Re: Batch file to change to a specific date
        « Reply #4 on: January 11, 2012, 10:31:33 AM »
        This is dependent on your regional and language settings.
        My date format is set to only output a 2 digit year yours is probably a 4 digit year.
        Code: [Select]
        H:\>echo %date%
        Wed 01/11/12

        So for a two digit year you could do this.
        Code: [Select]
        SET TODAY=%date:~4,2%-%date:~7,2%-%date:~10,2%
        REM setting date back to an older date
        DATE 12-11-11
        start "" "C:\path to my program\myprogram.exe"
        REM Changing date back to todays date
        DATE %TODAY%
        If you have a 4 digit year change the last DATE string to %date:~10,4%



        I'm using XP and the date format is wrong, I get   today=1/-01-

        Squashman



          Specialist
        • Thanked: 134
        • Experience: Experienced
        • OS: Other
        Re: Batch file to change to a specific date
        « Reply #5 on: January 11, 2012, 10:39:46 AM »
        You need to show me how the date variable outputs from the cmd prompt just like I showed you in my first post.

        Raven19528



          Hopeful
        • Thanked: 30
          • Computer: Specs
          • Experience: Experienced
          • OS: Windows 7
          Re: Batch file to change to a specific date
          « Reply #6 on: January 11, 2012, 11:47:40 AM »
          It looks like it is likely UK date output, which expands DD/MM/YYYY.

          Let's modify for that and see if it works out better for the OP.
          "All things that are
          Are with more spirit chased than enjoy'd" -Shakespeare

          Salmon Trout

          • Guest
          Re: Batch file to change to a specific date
          « Reply #7 on: January 11, 2012, 12:17:40 PM »
          It looks like it is likely UK date output

          I think I once saw the Artilleryman at Horsell Common, near Woking.

          The Artilleryman

            Topic Starter


            Starter

            • Experience: Beginner
            • OS: Unknown
            Re: Batch file to change to a specific date
            « Reply #8 on: January 12, 2012, 10:17:27 AM »
            You need to show me how the date variable outputs from the cmd prompt just like I showed you in my first post.

            OK I've worked out the first line for saving today's date

            SET TODAY=%date:~0,2%-%date:~3,2%-%date:~6,4%
            this gives me  12-01-2012

            I'm using
            C:\myprog\myprog.exe

            which starts the prog OK, but after loading the prog the batch file stops and only completes after exiting the prog, what I realy need to do is reset the date once the prog has loaded.
             Line 3   start "" "C:\myprog\myprog.exe"   ignores the date change.

            Raven19528



              Hopeful
            • Thanked: 30
              • Computer: Specs
              • Experience: Experienced
              • OS: Windows 7
              Re: Batch file to change to a specific date
              « Reply #9 on: January 12, 2012, 11:07:13 AM »
              Here is an excerpt from the start /? text that I think specifies the problem:

              Code: [Select]
              Microsoft Windows [Version 6.1.7601]
              When executing an application that is a 32-bit GUI application, CMD.EXE
                  does not wait for the application to terminate before returning to
                  the command prompt.  This new behavior does NOT occur if executing
                  within a command script.

              The way around this is to "start" a different command process, which in turn starts the program, but never takes control away from your original batch file. Example:

              Code: [Select]
              SET TODAY=%date:~0,2%-%date:~3,2%-%date:~6,4%
              REM setting date back to an older date
              DATE 12-11-11
              (echo start "" "C:\path to my program\myprogram.exe"
              echo del ^%^0) >tempstart.bat
              start "" tempstart.bat
              REM Changing date back to todays date
              ping 1.1.1.1 -n 1 -w 2000>nul
              DATE %TODAY%

              This may or may not be a long enough delay (2 seconds), but it does institute a slight delay before changing the date back to today. If this isn't required, you can remove the ping line.
              "All things that are
              Are with more spirit chased than enjoy'd" -Shakespeare

              Squashman



                Specialist
              • Thanked: 134
              • Experience: Experienced
              • OS: Other
              Re: Batch file to change to a specific date
              « Reply #10 on: January 12, 2012, 11:45:47 AM »
              which starts the prog OK, but after loading the prog the batch file stops and only completes after exiting the prog, what I realy need to do is reset the date once the prog has loaded.
               Line 3   start "" "C:\myprog\myprog.exe"   ignores the date change.
              I don't throw out random code for my good health. I gave you the correct code for starting the program in my first post. I had no control over the date output as I explained in both of my previous posts because anyone can change the short date format in their regional settings.

              jjh281



                Starter

              • Flexibility is the 4th dimension of good planning
                • Computer: Specs
                • Experience: Experienced
                • OS: Windows XP
                Re: Batch file to change to a specific date
                « Reply #11 on: February 29, 2012, 01:33:44 PM »
                Hi all

                This is my elegant solution to solving the date issue

                Edit launch to read the Progam
                Just amend the dates 01/01/2012 to whatever date you require
                Amend c:\...... to the shortcut properties of the program you wish to run
                When you exit the program the date will rest to today!

                Just copy and paste into notepad and save as a .bat file

                ECHO OFF
                CLS
                :MENU
                ECHO.
                ECHO ....................................... ........
                ECHO PRESS 1 to select your task, or 2 to EXIT.
                ECHO ....................................... ........
                ECHO.
                ECHO 1 - Launch xxxxxxxxxxxx
                ECHO 2 - Exit
                ECHO.
                SET /P M=Type 1, 2, then press ENTER:
                IF %M%==1 GOTO PROGRAM
                IF %M%==2 GOTO RESETDATE

                :PROGRAM
                FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CURDATE=%DATE%
                COLOR 4f
                echo Setting System date to 01/01/2012
                echo.
                date 01/01/2012
                echo Launching Program........Please Wait
                echo.
                "c:\xxxxxxxxx.exe"
                echo Resetting System Date back to Todays Date....Please Wait
                date %CURDATE%



                Hope this helps

                Jules

                Salmon Trout

                • Guest
                Re: Batch file to change to a specific date
                « Reply #12 on: February 29, 2012, 01:43:05 PM »
                Code: [Select]
                SET /P M=Type 1, 2, then press ENTER:
                What will happen if the user types 3 or just presses ENTER?

                None of the scripts in this thread should be run sufficiently close to midnight that the line with the start command finishes the next day.



                jjh281



                  Starter

                • Flexibility is the 4th dimension of good planning
                  • Computer: Specs
                  • Experience: Experienced
                  • OS: Windows XP
                  Re: Batch file to change to a specific date
                  « Reply #13 on: February 29, 2012, 01:50:00 PM »
                  Nothing!  You could delete the first section and just run the program batch file but remember running a date change could corrupt other files you save and will certainly impact your internet browser home page  so be careful  - hence to 1 or 2 check to make sure.

                  Julian


                  Salmon Trout

                  • Guest
                  Re: Batch file to change to a specific date
                  « Reply #14 on: February 29, 2012, 01:53:40 PM »
                  Nothing!

                  If the user types 3 and presses Enter, neither of the following IF tests will be satisfied, so the script will fall through to :PROGRAM, and if they just press ENTER, then %M% will be set to an empty string, and the script will halt with an error at the first IF test.



                  jjh281



                    Starter

                  • Flexibility is the 4th dimension of good planning
                    • Computer: Specs
                    • Experience: Experienced
                    • OS: Windows XP
                    Re: Batch file to change to a specific date
                    « Reply #15 on: February 29, 2012, 02:04:00 PM »
                    I dont see this as an issue as pressing 3 or enter will just renew the options screen to ask again.

                    jjh281



                      Starter

                    • Flexibility is the 4th dimension of good planning
                      • Computer: Specs
                      • Experience: Experienced
                      • OS: Windows XP
                      Re: Batch file to change to a specific date
                      « Reply #16 on: February 29, 2012, 02:07:58 PM »
                      Only trying to help those who seek a solution - I use this system everyday to run OEM car discs which have date related expiry locks - they work perfectly!

                      Salmon Trout

                      • Guest
                      Re: Batch file to change to a specific date
                      « Reply #17 on: February 29, 2012, 02:41:23 PM »
                      I dont see this as an issue as pressing 3 or enter will just renew the options screen to ask again.

                      It won't.

                      Raven19528



                        Hopeful
                      • Thanked: 30
                        • Computer: Specs
                        • Experience: Experienced
                        • OS: Windows 7
                        Re: Batch file to change to a specific date
                        « Reply #18 on: March 05, 2012, 06:59:23 PM »

                        ECHO OFF
                        CLS
                        :MENU
                        ECHO.
                        ECHO ....................................... ........
                        ECHO PRESS 1 to select your task, or 2 to EXIT.
                        ECHO ....................................... ........
                        ECHO.
                        ECHO 1 - Launch xxxxxxxxxxxx
                        ECHO 2 - Exit
                        ECHO.
                        SET /P M=Type 1, 2, then press ENTER:
                        IF %M%==1 GOTO PROGRAM
                        IF %M%==2 GOTO RESETDATE

                        All basic but working. Why don't you add a goto Menu line at the end here? That would save you the trouble of whether the user inputs anything besides 1 or 2. Also, using quotes (or any characters really) will allow those if tests to work if the user just hits enter too fast. You seem like you may ba little new, and this is good for anyone that wants to learn batch as well.

                        Example:
                        Code: [Select]
                        set /p m=
                        if %m%==1 (echo yes)
                        if %m%==2 (echo no)
                        If a batch file calls for user input (via set /p) and the user inputs nothing, then the variable will be empty. That presents a problem as the computer doesn't do well with comparing nothing with something. However if you were to use something to surround the variable, then it will be comparing something with something else, and it can handle that.
                        Code: [Select]
                        set /p m=
                        if a%m%a==a1a (echo yes)
                        if a%m%a==a2a (echo no)
                        These two scripts will do exactly the same thing when 1 or 2 is entered, but the second script won't crash the program if nothing is entered.
                         


                        FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CURDATE=%DATE%
                        What is this supposed to do? It looks like you are way overcomplicating this. Why not just a set CURDATE=%DATE%?

                        Salmon Trout is very good at this, so it would be wise not to argue if he is telling you something won't work. The correct response might be to ask why, as he is usually quite willing to help those who are willing to learn.
                        "All things that are
                        Are with more spirit chased than enjoy'd" -Shakespeare

                        Salmon Trout

                        • Guest
                        Re: Batch file to change to a specific date
                        « Reply #19 on: March 06, 2012, 12:36:53 AM »
                        Only the code I show here in blue actually does anything in this line

                        FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET CURDATE=%DATE%

                        I think the OP is not really interested in finding out why their "code" doesn't work. Otherwise, he wouldn't argue with the advice given.




                        Salmon Trout

                        • Guest
                        Re: Batch file to change to a specific date
                        « Reply #20 on: March 06, 2012, 02:42:21 AM »
                        Quote
                        Code: [Select]
                        set /p m=
                        if a%m%a==a1a (echo yes)
                        if a%m%a==a2a (echo no)

                        These two scripts will do exactly the same thing when 1 or 2 is entered, but the second script won't crash the program if nothing is entered.
                         

                        A very common, readable way of doing this is to use quote marks like this:

                        if "%a%"=="1" echo yes
                        if "%a%"=="2" echo no



                        Raven19528



                          Hopeful
                        • Thanked: 30
                          • Computer: Specs
                          • Experience: Experienced
                          • OS: Windows 7
                          Re: Batch file to change to a specific date
                          « Reply #21 on: March 06, 2012, 05:54:22 PM »
                          The only thing I have found is that sometimes quotes will get weird in the if checks. Only if quotes are in the input though. Try this,

                          Code: [Select]
                          if "She said, "Hello all," with confidence."=="She said, "Hello all," with confidence." echo yes
                          You will get the following error:
                          Code: [Select]
                          all was unexpected at this time.
                          At the same time, changing it to a's surrounding does the same thing.

                          Code: [Select]
                          if aShe said, "Hello all," with confidence.a==aShe said, "Hello all," with confidence.a echo yes
                          But with a different error word:
                          Code: [Select]
                          said was unexpected at this time.
                          I'm not sure the nuances on this, or how one goes about making sure that they use encapsulators that will always work, but it is something to be aware of when building something that may have a non-standard input.
                          "All things that are
                          Are with more spirit chased than enjoy'd" -Shakespeare

                          Salmon Trout

                          • Guest
                          Re: Batch file to change to a specific date
                          « Reply #22 on: March 07, 2012, 12:16:02 AM »
                          The only thing I have found is that sometimes quotes will get weird in the if checks.

                          I guess, but there aren't any quotes in 1 2 or 3, and to be honest I doubt if the OP will get much from an extended discussion of the topic.