Jump to content
donhorn20

Any way to search and compare filename to text within same file?

Recommended Posts

Hey guys, I have a scenario that I am trying to wrap my head around to if it's even possible to code for. Let me lay out the scenario first before I get into to far.

I have multiple files with different file names. There is a particular string within each text file that references the filename itself, just without the extension. I am trying to find all text files that possibly have a different string then the filename within the file.

Example of a good file:

Text1.pat (string within file is labeled as %File Name:Text1)

Text2.pat (string within file is labeled as %File Name:Text2)

Example of a bad file that I am trying to find all instances of:

Text3.pat (string within file is labeled as %File Name:Text7)

 

I can get the list of files from an array, but am stuck on how to pass that array through a possible check so that it compares the filename to a string within that file but strips off the extension. Any help or guidance would be great.

So far I just have the basic search going for the files in question (taken from help file)

#include <File.au3>
#include <MsgBoxConstants.au3>
#include <WinAPIFiles.au3>
#include <FileConstants.au3>

Test()

Func Test()
; List all the files and folders in the desktop directory.
    Local $aFileList = _FileListToArray(@DesktopDir & "\test", "*.pat", 1, True)
    If @error = 1 Then
        MsgBox($MB_SYSTEMMODAL, "", "Path was invalid.")
        Exit
    EndIf
    If @error = 4 Then
        MsgBox($MB_SYSTEMMODAL, "", "No file(s) were found.")
        Exit
    EndIf
    ; Display the results returned by _FileListToArray.
    _ArrayDisplay($aFileList, "$aFileList")
EndFunc

 

Share this post


Link to post
Share on other sites

Untested, but you will get the idea :

#include <File.au3>
#include <MsgBoxConstants.au3>
#include <FileConstants.au3>

Test()

Func Test()
  Local $sDrive, $sDir, $sFileName, $sExtension
  Local $aFileList = _FileListToArray(@DesktopDir & "\test", "*.pat", 1, True)
  If @error = 1 Then Exit MsgBox($MB_SYSTEMMODAL, "", "Path was invalid.")
  If @error = 4 Then Exit MsgBox($MB_SYSTEMMODAL, "", "No file(s) were found.")
  For $i = 1 to $aFileList[0]
    $sString = FileRead($aFileList[$i])
    _PathSplit($aFileList[$i], $sDrive, $sDir, $sFileName, $sExtension)
    If StringInStr($sString, "%File Name:" & $sFileName) Then ContinueLoop
    ConsoleWrite ("Error on " & $aFileList[$i] & @CRLF)
  Next
EndFunc   ;==>Test

 

Share this post


Link to post
Share on other sites

So I see what you did there with the _PathSplit and then passing that through the If StingInStr statement, but it seems to just list all my files and not display the mismatched ones

Its almost like its not evaluating the sFileName within the StringInStr section.

The consolewrite spits back 

Test1.pat

Test2.pat

Test3.pat

and so on, even though Test1.pat should not of returned a result because its filename "Test1" matches the section within that file "%File Name:Test1".  

 

Share this post


Link to post
Share on other sites
Posted (edited)
25 minutes ago, donhorn20 said:

and so on, even though Test1.pat should not of returned a result because its filename "Test1" matches the section within that file "%File Name:Test1".  

 @Nine's code should work. The file content does not conform to your specifications. Try dumping the file with Hxd or any other hex dumper and make sure that it actually contains "%File Name:Test1".

My guess is the "%" is not really there or the spacing is different.

Edited by pseakins

Phil Seakins

Share this post


Link to post
Share on other sites

Thanks @pseakins and @Nine for all the help. @Nine code was spot on as stated. The issue was I had a typo in my test file. The files do contain the "%" but there was a space issue. Once I corrected this minor oversight on my side (thanks to @pseakins making me take a deeper look at my file), it all worked. Thanks again for the speedy help. Much appreciated.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...