tags:

views:

48

answers:

1

Hi All,

I am trying to create a text file that contains a listing of all log files that contain a certain string in the first line. More specifically, SAS log files.

Currently I have a simple script that will search the entire system for "*.log" files and output the entire list to a text file.

Is there a way to only output the log files that contain a certain string?

Here is the current command:

dir /b /s /-p *.sas /o:n | findstr /E .sas >"%CD%"\win_file_list.txt

Every SAS log file contains the same string on the very first line. This string is:

1 The SAS System

So basically I want to search a file system for log files containing the string above, and output those file names to a text file.

Thanks in advance, Jason

A: 

The following transcript shows one way to do it. It's not the fastest but cmd scripts rarely are. If CygWin or GnuWin32 are an option, I'd probably go for one of those but this answer assumes that cmd.exe is all you have:

c:\src>type file1.log
1 The SAS System
xyz

c:\src>type file2.log
7 Not the SAS System
xyz

c:\src>type zz\file3.log
1 The SAS System
xyz

c:\src>type qq.cmd
    @setlocal enableextensions enabledelayedexpansion
    @echo off
    for /f %%a in ('dir /b /s *.log') do (
        call :procfile %%a
    )
    endlocal
    goto :eof
:procfile
    for /f "delims=" %%b in ('type "%1"') do (
        if "x%%b"=="x1 The SAS System" (
            echo %%a
        )
        goto :eof
    )

c:\src>qq.cmd
c:\src\file1.log
c:\src\zz\file3.log

The top-level for simply processes all files of a specified type. The function starts processing all lines but exits after the first one so as to avoid processing the entire file. This particular version handles subdirectories.


Keep in mind this won't handle file names with spaces in them (abominations though they are). If you need that, my advice is to definitely use a UNIX toolset like CygWin.

While you can often bend cmd.exe to your will, you'll almost certainly go mad in the process :-)

paxdiablo
Thanks a lot paxdiablo. I will be able to test it out tonight or tomorrow.
newbie_dev
Hi pax, I am having trouble getting this to work. This is the output I am getting:C:\Users\xx\>win_log_list.cmdFile Not Found
newbie_dev
@newbie, that's the error you get from `dir` when there are no files available matching the specification. (1) Did you change the `*.log` in my sample script to `*.sas` for your purposes? (2) Are there any `*.sas` files in the directory where you run the program?
paxdiablo
Well there are log files in sub directories of the directory where I am running the script. Will this not search recursively through all directories?Also, is there a way to get an output file with the list of files with the full path?
newbie_dev
No, the original version won't do recursive searches. I've modified the `dir` command to handle this with the subdirectory search `/s` flag but, if you need any more changes, I _do_ strongly suggest you switch to a UNIX-like toolset that has a lot more power.
paxdiablo