IT CAMPER
asked on
Batch or script to scan dated file for certain text
I have a daily log file that gets created in the format yymmdd.skd. I would like to scan all files in the folder that contain these skd files. The scan would check only files with the date of the current day or newer. So if today is 120416.skd, then it would check for files 120416.skd and 120417.skd, etc, but not older files than the current day.
The scan would check each file for the string ,,CM, (comma comma CM comma).
It needs to output the number of times it finds the string to a text file, just the total count. So the text file could say the following....
Found XX CM1 entries on schedule 120416.skd
If it does not find any entries of ,,CM, then it needs to make it obvious.
DID NOT FIND any CM1 entries on schedule 120416.skd.
Since it will be scanning multiple files each day, the text output would likely contain multiple file results.
The scan would check each file for the string ,,CM, (comma comma CM comma).
It needs to output the number of times it finds the string to a text file, just the total count. So the text file could say the following....
Found XX CM1 entries on schedule 120416.skd
If it does not find any entries of ,,CM, then it needs to make it obvious.
DID NOT FIND any CM1 entries on schedule 120416.skd.
Since it will be scanning multiple files each day, the text output would likely contain multiple file results.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Robert....attached is an actual skd file to test with (had to rename to .txt to attach), that contains zero ,,CM,.
When I run the script it creates a blank scan.log file. Not sure what it is doing or where it is missing anything.
Paul...the date format needs to be yymmdd
120418.txt
When I run the script it creates a blank scan.log file. Not sure what it is doing or where it is missing anything.
Paul...the date format needs to be yymmdd
120418.txt
murryc
echo %date%
Please let us know what that returns.
EDIT: (Oops! Just noticed seems like Qlemo might have it... Please confirm anyway)
Paul...the date format needs to be yymmddYeah, but to get the current date I would need to know your system's date format. You can find it with the following command in a DOS box:
echo %date%
Please let us know what that returns.
EDIT: (Oops! Just noticed seems like Qlemo might have it... Please confirm anyway)
I like the other guys' solutions because I used to do a lot of shell scripting on unix and didn't know windows shell scripting had become so versatile.
The big plus point of a .vbs like this for me is total flexibility in the sense that if you need to change something like search for a RegExp or show some other field in the lines where you find the search text it's easily extendable.
I can only guess that the script as I provided it doesn't work because I defined a variable to designate the folder where the .skd files are: C_FOLDER = "c:\temp\logging"
So you would need to point that at the right location. But maybe it's possible to change it to just "." for the current directory if that's where they are.
The big plus point of a .vbs like this for me is total flexibility in the sense that if you need to change something like search for a RegExp or show some other field in the lines where you find the search text it's easily extendable.
I can only guess that the script as I provided it doesn't work because I defined a variable to designate the folder where the .skd files are: C_FOLDER = "c:\temp\logging"
So you would need to point that at the right location. But maybe it's possible to change it to just "." for the current directory if that's where they are.
murryc
It would have been preferable for you to have posted a file that contains some ',,CM,'s.
I Just tried my previous code. Can you try this one instead please.
It would have been preferable for you to have posted a file that contains some ',,CM,'s.
I Just tried my previous code. Can you try this one instead please.
@echo off
for /f "skip=1 tokens=1-3" %%a in ('WMIC Path Win32_LocalTime Get Day^, Month^, Year') do (
for /f "tokens=*" %%f in ('xcopy *.skd "%temp%" /d:%%b-%%a-%%c /l ^| find /v "File(s)"') do (
for /f "tokens=3 delims=:" %%g in ('find /c "for" "%%f"') do (
if %%g equ 0 (
echo DID NOT FIND any CM1 entries on schedule %%~nxf
) else (
echo Found %%g CM1 entries on schedule %%~nxf
) ) ) )
Here's my version of a VBS that will write the details to a CSV file with
DATE,FILENAME,SEARCHCOUNT
Regards,
Rob
DATE,FILENAME,SEARCHCOUNT
Regards,
Rob
strFolder = "C:\Temp\Scripts"
strOutputFile = "C:\Temp\Scripts\FileCount.csv"
strSearchString = ",,CM,"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1
Const ForAppending = 8
Set objOutput = objFSO.OpenTextFile(strOutputFile, ForAppending, True)
objOutput.WriteLine """Date"",""File Name"",""CM Count"""
For Each objFile In objFSO.GetFolder(strFolder).Files
If Right(LCase(objFile.Name), 4) = ".skd" Then
Set objSKD = objFSO.OpenTextFile(objFile.Path, ForReading, False)
strContents = objSKD.ReadAll
objSKD.Close
intCount = 0
intPos = InStr(strContents, strSearchString)
While intPos > 0
intCount = intCount + 1
strContents = Mid(strContents, intPos + 1)'Len(strSearchString))
intPos = InStr(strContents, strSearchString)
Wend
If intCount > 0 Then
'WScript.Echo intCount & " entries were found in " & objFile.Name
objOutput.WriteLine Now & "," & objFile.Name & "," & intCount
Else
'WScript.Echo "No entries were found in " & objFile.Name
objOutput.WriteLine Now & "," & objFile.Name & "," & intCount
End If
End If
Next
objOutput.Close
ASKER
Paul,
not sure where your batch looks for ,,CM1,. I ran it against the SKD and added a ,,CM1, (which is just changing any of the ,,E**, areas to ,,CM1,, but your batch did not return anything.
Rob, I knew you would show up sometime :)
Your script nailed the scan and the output. Is there a way for it NOT to scan any skd files that exist prior to the current day? These skd files stay in the folder for years and so scanning backwards is useless, but scanning current and forward is crucial.
Thoughts?
not sure where your batch looks for ,,CM1,. I ran it against the SKD and added a ,,CM1, (which is just changing any of the ,,E**, areas to ,,CM1,, but your batch did not return anything.
Rob, I knew you would show up sometime :)
Your script nailed the scan and the output. Is there a way for it NOT to scan any skd files that exist prior to the current day? These skd files stay in the folder for years and so scanning backwards is useless, but scanning current and forward is crucial.
Thoughts?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
May I remind you of http:#a37857240 ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window