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 37945 times)

0 Members and 2 Guests 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.