|
Answer» Hello,
I'm trying to find a string in files (100101), and print the name of the file(s) found, and a count of total rows found that have the string "100101". I need to do something like this:
findstr /s /m "100101*" C:\MYDIR\*.* echo PRINT THE FILE NAME WHERE STRING IS FOUND, AND A COUNT OF TOTAL ROWS FOUND IN EACH FILE WITH STRING 100101*
Any help is greatly appreciated.
Thx You show two different strings: 100101 and 100101* - which is the string you are looking for?
This wouldn't be homework by any chance? Nope, not homework at all, this is work related. I'm looking to print the count of anything beginning with 100101*
ThxIs the asterisk part of the string or is it your "wild card"? In other words, you want to find any string that starts 100101, but exclude strings where those 6 characters are not at the beginning, but further along?
Correct,
it is a wildcard, not part of the string. I'd like to find any string that has 100101 only at the beginning of the row, and exclude those where the string is found later in the string.
Are the files all in the same folder, C:\MYDIR?Yes, they are all in that directory.Code: [Select]@echo off setlocal enabledelayedexpansion set folder="C:\MYDIR\" set string=100101 cd /d %folder% for /f "delims==" %%F in ('dir /b *.*') do ( set filename=%%~nxF set /a count=0 for /f "delims==" %%L in ('type "!filename!"') do ( set line=%%L if "!line:~0,6!"=="%string%" set /a count+=1 ) echo !filename! !count! ) AWESOME!!! Works like a charm. How can I spool this data to a csv file, say test.csv to the same C:MyDir folder?It appears redirecting to a file will work, but I need to loop through the values SOMEHOW to get them all. How Do I do that?
@echo off setlocal enabledelayedexpansion set folder="R:\Scott\DailyVendorDropoutAnalysis\DailyFiles\Vendor\Nov08\Nov12\" set string=100101 cd /d %folder% for /f "delims==" %%F in ('dir /b *.*') do ( set filename=%%~nxF set /a count=0 for /f "delims==" %%L in ('type "!filename!"') do ( set line=%%L if "!line:~0,6!"=="%string%" set /a count+=1 ) echo !filename! !count! > c:\myoutputtxt.txt )@echo off setlocal enabledelayedexpansion set folder="R:\Scott\DailyVendorDropoutAnalysis\DailyFiles\Vendor\Nov08\Nov12\" set string=100101 set outputfile="c:\myoutputtxt.csv" if exist %outputfile% del %outputfile% cd /d %folder% for /f "delims==" %%F in ('dir /b *.*') do ( set filename=%%~nxF set /a count=0 for /f "delims==" %%L in ('type "!filename!"') do ( set line=%%L if "!line:~0,6!"=="%string%" set /a count+=1 ) echo "!filename!" , "!count!">> %outputfile% )
Depending on how you want your csv file to look, you can noodle around with the quotes, comma, etc, in the line where the output happens.
Concerning redirection:
you use > to create a new file, (OVERWRITING any previous file with that name, so your previous code would only ever leave an output file with one line, containing the last value found)
and >> to append to a file, (if it exists - if not, the first time around creates it, then subsequently it appends - hence before we start we check if it exists and if it does we delete it. You may need to noodle around with that too - I don't know what you want to do with old files - MAYBE make a new output filename based on date/time or something, you know that better than me.)
Dias,
Thank you so very much!!!!!!!!!!You're very welcome. I edited the remarks in my last POST a bit. Or you could start a file with a header line using >
echo This is a header line File CREATED on %date% at %time% > %outputfile%
and then in the loop, append data lines with >>
echo "!filename!" , "!count!">> %outputfile%
|