Search code examples
batch-filefor-loopgrepcygwin

Looping Through a Directory and Extracting Data From The Files Created Today


I have some *.XML files located in a directory and its subdirectories. These files are increasing from time to time because of the periodic usage of a win batch script that I use for other tasks after extracting the XML files contents .

I know how to loop through all the files and extract the needed data, but what I don't know is how to loop through only the files created today, so that I can extract what I need from them.

Here is what I'm doing to extract the data from all the files:

1-By Using The "Grep" Tool, to Extract The Files' Contents

grep -r -hi --include="*.XML" "Pattern" > My.Data

.

2-By Looping in The .XML Files, and Combine Them into One File!

For /R %%X in (*.XML) Do @Type "%%X" >> My.Data

Yes I know It's a bit weird to use that second command while I have Grep! But actually, I need 99% of each file content, so both ways are almost the same for me .

As for my problem, I think it would be great if somebody can help me to modify one of the commands above, to loop in the xml files created today. But if it's not possible, or there is a better way in someone's mind, please don't hesitate to share it. And by the way, I have Cygwin installed on my system (win7-64)

Thanks so much guys in advance, I wish to get the right answer from here as usual. - Karim :)


Solution

  • I think the command you're looking for is forfiles. Type forfiles /? for full syntax. To grep only files that were modified today, do:

    forfiles /s /m *.xml /d +0 /c "cmd /c grep -hi ^"pattern^" @file >>\path\to\My.Data"

    Edit: As Karim pointed out, forfiles matches files based on last modified date. He's looking to match files based on created date.


    To get a file's creation date, use dir /tc and capture it with a for loop like this:

    @echo off
    setlocal
    
    :: get recursive list of XML files
    :: note: In US locale, a dir listing has 5 columns:
    :: MM/DD/YYYY  HH:MM  AM/PM  size  filename
    :: With "tokens=1,4*":
    ::   %%a captures date
    ::   %%b is throwaway (capturing file size)
    ::   %%c captures the filename with spaces
    
    for /f "tokens=1,4*" %%a in ('dir /s /tc *.XML') do (
    
        rem :: In US locale, %date% is in format "Day-of-week MM/DD/YYYY"
        rem :: If your locale doesn't list date last, see
        rem :: http://www.dostips.com/DtTipsStringManipulation.php
        rem :: for an explanation of string manipulation.
    
        if "%%a" equ "%date:* =%" (
    
            rem :: do stuff.
            grep "Pattern" "%%c" >> My.Data
    
        )
    )