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 9480 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

                          Geek-9pm


                            Mastermind
                          • Geek After Dark
                          • Thanked: 1026
                            • Gekk9pm bnlog
                          • Certifications: List
                          • Computer: Specs
                          • Experience: Expert
                          • OS: Windows 10
                          Re: Batch file to check when it was lat run?
                          « Reply #15 on: April 27, 2009, 01:46:05 PM »
                          May I but in ?  OK, I will anyway.
                          Before you get too far, look this over.
                          Quote
                          MS-DOS System Date Cannot Have a Year Past 2099
                          Article ID: 58495 - Last Review: May 10, 2003 - Revision: 2.0
                          http://support.microsoft.com/kb/58495
                          It apples to Microsoft MS-DOS 6.22 Standard Edition, so if you really use DOS and not the XP command prompt, you can have a problem.
                           Maybe. I don't know.  :P

                          devcom



                            Apprentice

                            Thanked: 37
                            Re: Batch file to check when it was lat run?
                            « Reply #16 on: April 27, 2009, 02:41:09 PM »
                            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


                            if your date looks like yyyy/mm/dd then change every delims=- to delims=/
                            Download: Choice.exe

                            Reno



                              Hopeful
                            • Thanked: 32
                              Re: Batch file to check when it was lat run?
                              « Reply #17 on: April 28, 2009, 12:15:07 AM »
                              you could do all of this in vbscript

                              Code: [Select]
                              'lastrun.vbs - check for vbs lastrun and rename a folder if less than 7d
                              f=".\testfolder"
                              with createobject("scripting.filesystemobject")
                              if not .folderexists(f) then wsh.echo f, "not exist":wsh.quit 1

                              lastrun=now
                              if .fileexists(".\dummy.txt") then lastrun=.getfile(".\dummy.txt").datelastmodified
                              wsh.echo "Last run:",lastrun

                              if datediff("d",now,lastrun)<7 then
                              d=.getbasename(f) & "_" & right(year(date)*10000+month(date)*100+day(date),6)
                              wsh.echo "Less than 7 days, rename to", d
                              .getfolder(f).name = d
                              end if

                              set dummy=.opentextfile(".\dummy.txt",2,true)
                              dummy.writeline now
                              dummy.close
                              end with
                              to run, type at command prompt "lastrun.vbs"
                              use cscript engine: cscript//nologo lastrun.vbs
                              use wscript engine: wscript//nologo lastrun.vbs

                              or if you insist on batch code, i've the pure batch code which is twice the size of vbs code.
                              or you could have the best of both world with the above code by devcom

                              TheSpecial1

                                Topic Starter


                                Greenhorn

                                Re: Batch file to check when it was lat run?
                                « Reply #18 on: April 28, 2009, 03:32:31 AM »
                                Thanks everyone for all your help.

                                I'm gonna go away ant try these out.

                                no doubt, i'll be back if i get confused  ???

                                TheSpecial1

                                  Topic Starter


                                  Greenhorn

                                  Re: Batch file to check when it was lat run?
                                  « Reply #19 on: April 28, 2009, 05:19:57 AM »
                                  Hi - back again  ::)

                                  I am almost there, however there is one small problem.

                                  The script successfully renames the test folder, however when i run it again it should do the comparison to see when it was last run. It was run less than 7 days ago the 'rename will not be performed' message should display.

                                  The script for some reason is not doing this. When i run it for the second time I get the 'duplicate file name exists or location cannot be found' error instead.

                                  I think it is because of the "if not" statement that compares the 2 dates to see whether 7 days have elapsed.
                                  The 2 variables are in different formats and therefore the statement fall over.
                                  i.e.
                                  Date  = 28/04/09
                                  Date7 - 05052009

                                  Am I correct in this? and how to I get the date.txt  in the same format as the date7 variable?

                                  thanks




                                  Code: [Select]


                                  @echo off

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


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


                                  set /p date=<date.txt

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


                                  if not %date:+=% GEQ %date7:+=% (
                                  echo Rename will not be performed.
                                  pause >nul
                                  exit
                                  )

                                  pause
                                  ren %org% %org%_%yymmdd%
                                  cscript //nologo evaluate.vbs date >date.txt

                                  echo. DONE!
                                  pause


                                  devcom



                                    Apprentice

                                    Thanked: 37
                                    Re: Batch file to check when it was lat run?
                                    « Reply #20 on: April 28, 2009, 05:52:04 AM »
                                    forgot to told this to you ;)

                                    you also need to change this:
                                    Code: [Select]
                                    if not %date:+=% GEQ %date7:+=% (
                                    to

                                    Code: [Select]
                                    if not %date:/=% GEQ %date7:/=% (
                                    so it can remove it

                                    and i don't know why Date & Date7 are in different format... they should be the same as it uses the same command ;)
                                    Download: Choice.exe

                                    TheSpecial1

                                      Topic Starter


                                      Greenhorn

                                      Re: Batch file to check when it was lat run?
                                      « Reply #21 on: April 28, 2009, 07:04:34 AM »
                                      Hi,

                                      I'm almost there - so the batch file runs and successfully renames the test folder to test_yyyymmdd

                                      If I create another test folder and run the batch file again - it should check if the batch file has been run in the last 7 days and then decide wether to run or not.

                                      So in the case of me creating the second test folder, the batch file should not run as i have previosuly run it on the same day.

                                      I have tried this and it is not working  ???

                                      I get the following error -  "A duplicate file name exists, or the file cannot be found"

                                      What am I doing wrong?   :'(

                                      Code: [Select]
                                      @echo off

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

                                      for /f "tokens=1-3 delims=/" %%A in ('cscript //nologo evaluate.vbs date') do set current=%%A%%B%%C
                                      if not exist date.txt echo.%current% >date.txt

                                      set /p date=<date.txt

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

                                      if not %date:/=% GEQ %date7:/=% (
                                      echo Rename will not be performed.
                                      pause >nul
                                      exit
                                      )

                                      ren %org% %org%_%yymmdd%
                                      echo.%current% >date.txt

                                      echo. DONE!
                                      pause

                                      devcom



                                        Apprentice

                                        Thanked: 37
                                        Re: Batch file to check when it was lat run?
                                        « Reply #22 on: April 28, 2009, 08:18:06 AM »
                                        final code i think ;)

                                        edit: or no...

                                        edit: if i understand you good add this in front of file:

                                        Code: [Select]
                                        for /f "tokens=*" %%a in ('dir /b ^|findstr "%org%"') do (
                                        if not "%%a" equ "%org%"(
                                        echo Error: %%a
                                        pause >nul
                                        exit
                                        )
                                        )
                                        « Last Edit: April 28, 2009, 08:36:49 AM by devcom »
                                        Download: Choice.exe