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

Author Topic: Batch file to check when it was lat run?  (Read 9478 times)

0 Members and 1 Guest are viewing this topic.

TheSpecial1

    Topic Starter


    Greenhorn

    Batch file to check when it was lat run?
    « on: April 26, 2009, 04:50:26 AM »
    Hi,

    I am a newbie to batch files.

    I am wanting to write a batch file to rename a directory to originalname_yymmdd.

    However before the folder rename is done, i need the batch file to check when it was last run. If it was run less than 7 days then i don't want it to perform the rename.

    I need to do this without using scheduled tasks.

    Any help provided would be greatly appreciated.

    Thanks

    gh0std0g74



      Apprentice

      Thanked: 37
      Re: Batch file to check when it was lat run?
      « Reply #1 on: April 26, 2009, 04:52:41 AM »
      then use a file that will store something to indicate its last run. eg a date.

      Helpmeh



        Guru

      • Roar.
      • Thanked: 123
        • Yes
        • Yes
      • Computer: Specs
      • Experience: Familiar
      • OS: Windows 8
      Re: Batch file to check when it was lat run?
      « Reply #2 on: April 26, 2009, 06:07:45 AM »
      1. I am wanting to write a batch file to rename a directory to originalname_yymmdd.
      Code: [Select]
      for /f "tokens=1-3 delims=/ " %%A in ("%date%") do set yymmdd=%C%B%A
      set org=ORIGINALNAME
      ren %org% %org%%yymmdd%
      2. However before the folder rename is done, i need the batch file to check when it was last run. If it was run less than 7 days then i don't want it to perform the rename.
      Code: [Select]
      set /p old=<date
      set /a old2=old+7
      if not %yymmdd% GEQ %old2% (
      echo Rename will not be performed.
      pause > nul
      exit
      )

      Total code:
      Code: [Select]
      @echo off
      for /f "tokens=1-3 delims=/ " %%A in ("%date%") do set yymmdd=%C%B%A
      set org=ORIGINALNAME
      set /p old=<date
      set /a old2=%old%+7
      if not %yymmdd% GEQ %old2% (
      echo Rename will not be performed.
      pause > nul
      exit
      )
      ren %org% %org%%yymmdd%
      echo %yymmdd% > date
      Note that you have to put the current date in yymmdd format in date before you run it for the first time.
      Where's MagicSpeed?
      Quote from: 'matt'
      He's playing a game called IRL. Great graphics, *censored* gameplay.

      devcom



        Apprentice

        Thanked: 37
        Re: Batch file to check when it was lat run?
        « Reply #3 on: April 26, 2009, 06:39:17 AM »
        @helpmeh
        is that question or response ?

        @TheSpecial1
        Code: [Select]
        @echo off
        if not exist date.txt echo First run! > date.txt
        echo Last run:
        type date.txt

        echo.%date% %time% >date.txt
        pause
        Download: Choice.exe

        Helpmeh



          Guru

        • Roar.
        • Thanked: 123
          • Yes
          • Yes
        • Computer: Specs
        • Experience: Familiar
        • OS: Windows 8
        Re: Batch file to check when it was lat run?
        « Reply #4 on: April 26, 2009, 06:42:47 AM »
        @helpmeh
        is that question or response ?

        @TheSpecial1
        Code: [Select]
        @echo off
        if not exist date.txt echo First run! > date.txt
        echo Last run:
        type date.txt

        echo.%date% %time% >date.txt
        pause
        That was a response. I was responding to each of the OP "questions" individially, then put them together.
        Where's MagicSpeed?
        Quote from: 'matt'
        He's playing a game called IRL. Great graphics, *censored* gameplay.

        devcom



          Apprentice

          Thanked: 37
          Re: Batch file to check when it was lat run?
          « Reply #5 on: April 26, 2009, 08:02:35 AM »
          Oh ok  ;) Sorry then  ;)
          Download: Choice.exe

          Helpmeh



            Guru

          • Roar.
          • Thanked: 123
            • Yes
            • Yes
          • Computer: Specs
          • Experience: Familiar
          • OS: Windows 8
          Re: Batch file to check when it was lat run?
          « Reply #6 on: April 26, 2009, 09:27:17 AM »
          Oh ok  ;) Sorry then  ;)
          Not your fault. You're just here to help out (and possibly to be helped as well lol).
          Where's MagicSpeed?
          Quote from: 'matt'
          He's playing a game called IRL. Great graphics, *censored* gameplay.

          TheSpecial1

            Topic Starter


            Greenhorn

            Re: Batch file to check when it was lat run?
            « Reply #7 on: April 27, 2009, 03:10:52 AM »
            Total code:
            Code: [Select]
            @echo off
            for /f "tokens=1-3 delims=/ " %%A in ("%date%") do set yymmdd=%C%B%A
            set org=ORIGINALNAME
            set /p old=<date
            set /a old2=%old%+7
            if not %yymmdd% GEQ %old2% (
            echo Rename will not be performed.
            pause > nul
            exit
            )
            ren %org% %org%%yymmdd%
            echo %yymmdd% > date
            Note that you have to put the current date in yymmdd format in date before you run it for the first time.

            Hi thanks for everyones help.

            I have tried to run the code you provided in a batch file, however it is renaming the yymmdd to BA.
            Therefore if i try this code on a folder named 'test' then the batch file will rename the folder to 'testba'.
            What am I doing wrong here?

            TheSpecial1

              Topic Starter


              Greenhorn

              Re: Batch file to check when it was lat run?
              « Reply #8 on: April 27, 2009, 03:35:24 AM »
              I have sorted it!

              thanks

              TheSpecial1

                Topic Starter


                Greenhorn

                Re: Batch file to check when it was lat run?
                « Reply #9 on: April 27, 2009, 04:33:09 AM »
                Hi,

                This is how far i have got with the code (see below). However I have changed the system date on my PC to more than 7 days later and the code will not run. It is will not go past 'rename will not be performed'.

                I think it's because its going into a new month (May)?

                Can someone please help?


                Code: [Select]

                @echo off

                for /f "tokens=1-3 delims=/ " %%A in ("%date%") do set yymmdd=%%C%%B%%A
                set org=test

                if not exist date.txt echo.%date% >date.txt

                set /p old=<date.txt
                set /a old2=%old%+7
                if not %date% GEQ %old2% (
                echo Rename will not be performed.
                pause > nul
                exit
                )
                ren %org% %org%%yymmdd%
                echo %date% > date.txt

                pause


                gh0std0g74



                  Apprentice

                  Thanked: 37
                  Re: Batch file to check when it was lat run?
                  « Reply #10 on: April 27, 2009, 07:02:49 AM »
                  you can't really do date calculation like that with batch.

                  TheSpecial1

                    Topic Starter


                    Greenhorn

                    Re: Batch file to check when it was lat run?
                    « Reply #11 on: April 27, 2009, 07:15:01 AM »
                    Can you suggest any possible ways round this, any other ways to achieve the same result?

                    devcom



                      Apprentice

                      Thanked: 37
                      Re: Batch file to check when it was lat run?
                      « Reply #12 on: April 27, 2009, 07:18:50 AM »
                      Quote from: Dias de verano
                      Code:
                      Wscript.echo eval(WScript.Arguments(0))

                      Save this script as Evaluate.vbs

                      Now, (cumbersome) either call it by

                      cscript //nologo "Path to\evaluate.vbs" "valid VBS expression" [quotes not always needed but advised]
                      http://www.computerhope.com/forum/index.php?topic=77361.0

                      so...

                      you need to use for loop:

                      Code: [Select]
                      for /f "tokens=1-3 delims=/" %%a in ('cscript //nologo evaluate.vbs date+7') do (echo %%a %%b %%c)
                      Download: Choice.exe

                      TheSpecial1

                        Topic Starter


                        Greenhorn

                        Re: Batch file to check when it was lat run?
                        « Reply #13 on: April 27, 2009, 08:38:39 AM »
                        Hi

                        I am a bit confused with this.

                        How can i implement this within my code?

                        thanks

                        devcom



                          Apprentice

                          Thanked: 37
                          Re: Batch file to check when it was lat run?
                          « Reply #14 on: April 27, 2009, 10:45:57 AM »
                          try this:

                          Code: [Select]
                          @echo off

                          for /f "tokens=1-3 delims=-" %%A in ('cscript //nologo evaluate.vbs date') do set yymmdd=%%A%%B%%C
                          set org=test

                          if not exist date.txt cscript //nologo evaluate.vbs date >date.txt

                          set /p date=<date.txt
                          for /f "tokens=* delims=-" %%A in ('cscript //nologo evaluate.vbs date+7') do set date7=%%A

                          if not %date:-=% GEQ %date7:-=% (
                          echo Rename will not be performed.
                          pause >nul
                          exit
                          )
                          ren %org%.txt %org%_%yymmdd%.txt
                          cscript //nologo evaluate.vbs date >date.txt

                          echo. DONE!
                          pause
                          Download: Choice.exe