donhorn20 Posted January 5, 2021 Posted January 5, 2021 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
Nine Posted January 5, 2021 Posted January 5, 2021 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 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Debug Messages Monitor UDF Screen Scraping Round Corner GUI UDF Multi-Threading Made Easy
donhorn20 Posted January 5, 2021 Author Posted January 5, 2021 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".
pseakins Posted January 6, 2021 Posted January 6, 2021 (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 January 6, 2021 by pseakins Phil Seakins
donhorn20 Posted January 6, 2021 Author Posted January 6, 2021 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.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now