1.

Solve : output files based on two FIND criterias with .bat file.?

Answer»

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 youTry 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? 



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

Thu 10/28/2010

That is the date format. 

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


Thank you for your help.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
)

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.
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"
)
)
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.



(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%

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"


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%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. Quote

"%test_script.txt% %script_test.txt%"

What do these variables hold?

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.


Discussion

No Comment Found