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

0 Members and 1 Guest are viewing this topic.

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