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

Author Topic: File Date search  (Read 3518 times)

0 Members and 1 Guest are viewing this topic.

destruct

    Topic Starter


    Greenhorn

    File Date search
    « on: November 30, 2009, 10:42:50 AM »
    I have a very simple MS DOS batch file in use which along the way renames files with their date of creation, in the format yyyymmdd.

    These files are created only one per day, so there is no duplication of names.

    I would like to add an "IF EXIST" statement which searches among these files for one which is one day old.

    thanks for any inputs.

    Joanlong



      Rookie

      Re: File Date search
      « Reply #1 on: November 30, 2009, 10:58:01 AM »

      Salmon Trout

      • Guest
      Re: File Date search
      « Reply #2 on: November 30, 2009, 11:07:13 AM »
      Visual Basic Script/batch hybrid. Frowned on by some scripting purists, but I believe it will do the job, provided vb scripting is enabled on the system(s) intended.

      One day all scripts will be commented like this  ;)

      Code: [Select]
      @echo off

      REM create vb script on the fly
      echo Wscript.echo eval(WScript.Arguments(0))>evaluate.vbs

      REM Regarding the 3 cscript calls below:
      REM date-1 means "yesterday". Can change to get older dates.
      REM E.g. date-36500 gives a date in 1909
      REM date   means "today"
      REM date+1 means "tomorrow".  So you can see possibilities.

      REM get YYYY (always 4 figures)
      for /f "delims==" %%A in ('cscript //nologo evaluate.vbs "year(date-1) "') do set ydY=%%A

      REM get m (1 to 9) or mm (10-12)
      for /f "delims==" %%A in ('cscript //nologo evaluate.vbs "month(date-1)"') do set ydM=%%A

      rem get d or dd (1 to 9) or dd (10 and up)
      for /f "delims==" %%A in ('cscript //nologo evaluate.vbs "day(date-1)  "') do set ydD=%%A

      REM done with vb script
      del evaluate.vbs

      REM if month is single figure (1-9) prefix with 0
      if %ydM% LEQ 9 set ydM=0%ydM%

      REM if day is single figure (1-9) prefix with 0
      if %ydD% LEQ 9 set ydD=0%ydD%

      REM string them all together
      set yd-date=%ydY%%ydM%%ydD%

      REM show result
      echo Yesterday was %yd-date%

      REM is there a file with that name?
      if exist "%yd-date%" (
         echo the file exists
         ) else (
         echo the file does not exist
         )
        
      « Last Edit: November 30, 2009, 11:38:49 AM by Salmon Trout »

      destruct

        Topic Starter


        Greenhorn

        Re: File Date search
        « Reply #3 on: November 30, 2009, 02:29:23 PM »
        Thank, but first answer is on wrong track.  I asked how to search for a file, not rename one.  I have the renaming bit covered.

        Second answer, I will try and let you know.

        I was, however,  hoping for something simple along lines of: http://www.computerhope.com/forum/index.phpPHPSESSID=8f658f031aa85f07167fc915a1af04c3&topic=67178.0 , which of course I would have to finish out with MS DOS batch language.  That is of course if batch  languages can be mixed and matched.

        There is quite a bit more to my batch file than indicated in original question, so MS DOS is already being being used.

        thanks


        gh0std0g74



          Apprentice

          Thanked: 37
          Re: File Date search
          « Reply #4 on: November 30, 2009, 05:29:55 PM »
          since you are using batch, i am not going to suggest you use entirely vbscript, but you can save a vbscript that does the 1 day old check and call it in your batch
          Code: [Select]
          Set objFSO = CreateObject("Scripting.FileSystemObject")
          Set objArgs = WScript.Arguments
          strNumDays=objArgs(0)
          strFolder = objArgs(1)
          Set objFolder = objFSO.GetFolder(strFolder)
          For Each efile in objFolder.Files
           If DateDiff("d",eFile.DateLastModified,Now) >= strNumDays Then
             WScript.Echo "file found that is " & strNumDays & " day(s) old: " & efile
             WScript.Echo eFile.DateLastModified   
             ' objFSO.DeleteFile(eFile) 'deletion
           End If
          Next


          save it as find1dayold.vbs and in your batch, use it like
          Code: [Select]
          @echo off
          .....
          cscript /nologo find1dayold.vbs 1 "c:\Folder_to_check"
          .....
          if you need return values, use a for loop to get the values

          destruct

            Topic Starter


            Greenhorn

            Re: File Date search
            « Reply #5 on: December 03, 2009, 06:46:49 PM »
            Sorry to be late getting back.

            I know there are variables I need to plug in to that script, but with no experience with this type, I don't have a clue as to what or where

            thanks for bearing with me

            destruct

              Topic Starter


              Greenhorn

              Re: File Date search
              « Reply #6 on: December 03, 2009, 06:59:27 PM »
              As a clarification, my previous post was meant to apply to that from gh0std0g74's answer

              answer  prior to that from Salmon Trout does work. and I do appreciate the comments along the way in the script.

              thank you


              destruct

                Topic Starter


                Greenhorn

                Re: File Date search
                « Reply #7 on: December 03, 2009, 08:43:45 PM »
                How about this, which is pretty simple, and requires no additional VBS script be called:

                @echo off
                echo wscript.echo ^(Date^(^)- 1^)>yesterday.vbs
                for /f %%a in ('cscript //nologo yesterday.vbs') do set ydate1=%%a
                del yesterday.vbs
                set ydate1=%ydate1:/=%
                echo The current date is:
                date /t
                set m=%ydate1:~2,2%
                set d=%ydate1:~4,4%
                set y=%ydate1:~0,2%
                set ydate2=%y%%m%%d%
                echo Yesterday was %ydate2%
                if exist PATH\%ydate2% (<COMMAND> <PATH\specified files>) else (exit batch)

                gh0std0g74



                  Apprentice

                  Thanked: 37
                  Re: File Date search
                  « Reply #8 on: December 04, 2009, 01:58:27 AM »
                  How about this, which is pretty simple, and requires no additional VBS script be called:
                  Code: [Select]
                  ...
                  for /f %%a in ('cscript //nologo yesterday.vbs') do set ydate1=%%a
                  what do you mean. you are calling yesterday.vbs which is a vbscript.


                  Salmon Trout

                  • Guest
                  Re: File Date search
                  « Reply #9 on: December 04, 2009, 02:06:04 AM »
                  I think he means it's being created on-the-fly in the same folder as the batch and not residing somewhere else. This is deprecated by many, I understand, and in many cases may be unsuitable e.g. in environments where the script engines are disabled.