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

Author Topic: output files based on two FIND criterias with .bat file.  (Read 8820 times)

0 Members and 1 Guest are viewing this topic.

downat420

    Topic Starter


    Rookie

    • Experience: Beginner
    • OS: Unknown
    output files based on two FIND criterias with .bat file.
    « on: October 28, 2010, 10:35:26 AM »
    I wrote a .bat script that will search a folder for files with file names that were modified on todays date and then output the results to a .txt file.  I want to change it to only return files that were not modified on todays date.  I tried to use /V in the second FIND statements, but it appears to apply the /V to both FIND statements.

    Code: [Select]
    for /f "tokens=2,3,4 delims=/ " %%a in ('DATE /T') do set date=%%a/%%b/%%c
    dir  "c:\temp"  /s  /ta  /a-d  | find "%test_script.txt% %script_test.txt%" | find "%date%" > c:\temp\list.txt

    This is my /V attempt that does not do what I want.
    Code: [Select]
    for /f "tokens=2,3,4 delims=/ " %%a in ('DATE /T') do set date=%%a/%%b/%%c
    dir  "c:\temp"  /s  /ta  /a-d  | find "%test_script.txt% %script_test.txt%" | find "%date%" /V > c:\temp\list.txt


    If you know how to modify this script to meet my needs let me know.

    Thank you

    Salmon Trout

    • Guest
    Re: output files based on two FIND criterias with .bat file.
    « Reply #1 on: October 28, 2010, 10:56:00 AM »
    Try puting the /V switch after FIND and before the string. e.g find /v "%date%"

    By the way, (1) did you really want to set a variable (%date%) with the same name as a system variable? (2) The /ta switch is time last accessed which is not necessarily the time a file was last modified. Maybe you need /tw (time last written) switch.

    Why don't you just use FOR to find the modified date directly? 




    downat420

      Topic Starter


      Rookie

      • Experience: Beginner
      • OS: Unknown
      Re: output files based on two FIND criterias with .bat file.
      « Reply #2 on: October 28, 2010, 02:12:31 PM »
      The /V is in the wrong place in my post.  I am using it in directly after the find.

      1. I can change the variable name.  Nice tip.

      2. I should change the /ta to /tw.

      I do not know how to use FOR to find the date directly.  Feel free to modify what I have and help me out.

      Thank you for your help.

      Salmon Trout

      • Guest
      Re: output files based on two FIND criterias with .bat file.
      « Reply #3 on: October 28, 2010, 02:15:45 PM »
      I will have a try with some ideas I have... could you do one thing for me?

      Open a command prompt and type

      echo %date%

      and copy and paste here what you get?

      I need to see your local date format


      downat420

        Topic Starter


        Rookie

        • Experience: Beginner
        • OS: Unknown
        Re: output files based on two FIND criterias with .bat file.
        « Reply #4 on: October 28, 2010, 03:01:22 PM »
        Thu 10/28/2010

        That is the date format. 

        The first line of my script returns "10/28/2010".


        Thank you for your help.

        Salmon Trout

        • Guest
        Re: output files based on two FIND criterias with .bat file.
        « Reply #5 on: October 28, 2010, 03:43:34 PM »
        What sort of output do you get from this?


        Code: [Select]

        @echo off
        for /f "delims=" %%A in ('dir /s /b /tw c:\temp') do (
        echo %%~tA %%~dpnxA
        )


        downat420

          Topic Starter


          Rookie

          • Experience: Beginner
          • OS: Unknown
          Re: output files based on two FIND criterias with .bat file.
          « Reply #6 on: October 28, 2010, 08:31:01 PM »
          I have 3 test files.  Your script returns all of them.  These are the only three files in c:\temp.


          10/28/2010 10:20 PM c:\temp\test_1.txt
          10/28/2010 10:21 PM c:\temp\test_2.txt
          10/11/2010 02:18 PM c:\temp\Test_3.txt
          Microsoft Windows [Version 6.1.7600]
          Copyright (c) 2009 Microsoft Corporation.  All rights reserved.

          Salmon Trout

          • Guest
          Re: output files based on two FIND criterias with .bat file.
          « Reply #7 on: October 29, 2010, 12:59:58 AM »
          try this

          Code: [Select]
          @echo off
          setlocal enabledelayedexpansion
          set TodayDate=%date%
          echo Today is %TodayDate%

          REM create at least one file with todays date for test
          echo hello>c:\temp\TodaysFile.txt

          for /f "delims=" %%A in ('dir /s /b /tw /a-d c:\temp') do (
          set FileDateTime=%%~tA
          set FileDrivePathnameExt=%%~dpnxA
          for /f "tokens=1-2 delims= " %%D in ("!FileDateTime!") do set DatePart=%%D
          if "!DatePart!"=="%TodayDate%" (
          echo Keep   !FileDrivePathnameExt!
          ) else (
          echo Remove !FileDrivePathNameExt!
          REM IN next line remove REM to actually remove the file
          REM del "!FileDrivePathnameExt"
          )
          )

          downat420

            Topic Starter


            Rookie

            • Experience: Beginner
            • OS: Unknown
            Re: output files based on two FIND criterias with .bat file.
            « Reply #8 on: October 29, 2010, 07:00:14 AM »
            As you wrote it, it returns all files.  I think the problem is the TodayDate string.  I modified it to use my date string and now it tells me which files to keep.

            I do not want to remove any files as they are already overwritten daily.  I just need a quick check to tell me which files were not updated today, indication of an error.  I want to search for them by name as I did in my script. 

            The folders will have other files that I want to exclude from the scope of this script, so I must search for them by name, and identify which ones I named in the FIND do not have a modified date of today.  I think what you have is really close though.




            Salmon Trout

            • Guest
            Re: output files based on two FIND criterias with .bat file.
            « Reply #9 on: October 29, 2010, 07:02:46 AM »
            (Update) I realised that you want to output a list of files modified before today's date to a text file.

            Code: [Select]
            @echo off
            setlocal enabledelayedexpansion
            set TodayDate=%date%
            if exist output.txt del output.txt
            echo Files modified before date: %TodayDate%>output.txt
            echo.>>output.txt
            set filesfound=0
            for /f "delims=" %%A in ('dir /s /b /tw /a-d c:\temp') do (
            set FileDateTime=%%~tA
            set FileDrivePathnameExt=%%~dpnxA
            for /f "tokens=1-2 delims= " %%D in ("!FileDateTime!") do set DatePart=%%D
            if not "!DatePart!"=="%TodayDate%" (
                                 echo Modified: !DatePart! File: "!FileDrivePathnameExt">>output.txt
                                 set /a filesfound+=1
                                 )
            )
            echo Files found: %filesfound%


            downat420

              Topic Starter


              Rookie

              • Experience: Beginner
              • OS: Unknown
              Re: output files based on two FIND criterias with .bat file.
              « Reply #10 on: October 29, 2010, 09:05:03 AM »
              The output your latest version returns is below.  The only thing I need is to only apply the search to the named files like I was doing in my script.  And output the filename and date modified.  This is almost what I need.

              Thank you for your help so far.



              Files modified before date: Fri 10/29/2010

              Modified: 10/25/2010 File: "FileDrivePathnameExt"
              Modified: 10/22/2010 File: "FileDrivePathnameExt"
              Modified: 10/25/2010 File: "FileDrivePathnameExt"
              Modified: 10/25/2010 File: "FileDrivePathnameExt"
              Modified: 10/29/2010 File: "FileDrivePathnameExt"
              Modified: 10/28/2010 File: "FileDrivePathnameExt"
              Modified: 10/25/2010 File: "FileDrivePathnameExt"



              Salmon Trout

              • Guest
              Re: output files based on two FIND criterias with .bat file.
              « Reply #11 on: October 29, 2010, 09:59:23 AM »
              Code: [Select]
              @echo off
              setlocal enabledelayedexpansion
              set TodayDate=%date%
              if exist output.txt del output.txt
              echo Files modified before date: %TodayDate%>output.txt
              echo.>>output.txt
              set filesfound=0
              for /f "delims=" %%A in ('dir /s /b /tw /a-d c:\temp ^| find "%test_script.txt% %script_test.txt%" ') do (
              set FileDateTime=%%~tA
              set FileDrivePathnameExt=%%~dpnxA
              for /f "tokens=1-2 delims= " %%D in ("!FileDateTime!") do set DatePart=%%D
              if not "!DatePart!"=="%TodayDate%" (
                                   REM this was an error
                                   REM Replaced exclamation mark at the end of !FileDrivePathnameExt!
                                   echo Modified: !DatePart! File: "!FileDrivePathnameExt!">>output.txt
                                   set /a filesfound+=1
                                   )
              )
              echo Files found: %filesfound%

              downat420

                Topic Starter


                Rookie

                • Experience: Beginner
                • OS: Unknown
                Re: output files based on two FIND criterias with .bat file.
                « Reply #12 on: October 29, 2010, 10:40:04 AM »
                that does not return anything.  i have 3 test files.  one has a modified date of "10/29/2010".  Could it be that your date string reads Fri 10/29/2010.

                thank you for your continued help.

                Salmon Trout

                • Guest
                Re: output files based on two FIND criterias with .bat file.
                « Reply #13 on: October 29, 2010, 10:43:24 AM »
                Quote
                "%test_script.txt% %script_test.txt%"

                What do these variables hold?


                downat420

                  Topic Starter


                  Rookie

                  • Experience: Beginner
                  • OS: Unknown
                  Re: output files based on two FIND criterias with .bat file.
                  « Reply #14 on: October 29, 2010, 11:46:27 AM »
                  Those are file names.  I need to check just the file names I include in those strings for modified dates <> today and return them.

                  In my test c:\temp folder I have three files

                  script_test.txt          modified:  10/28/2010
                  test_script.txt          modified:  10/29/2010
                  not_included.txt      does not matter because it is not named in the script.

                  The goal would be to return:
                  script_test.txt         10/28/2010

                  This would tell me quickly that this file was not updated so I could troubleshoot the process responsible for updating that file.  I have to check a ton of folders each morning.  My ultimate goal is the clean up the process, but in the meantime I need this to take less of my day.  New jobs have their quirks.

                  Thank you.