KickStarter15 Posted March 6, 2018 Share Posted March 6, 2018 (edited) Hi Experts, I know this is weird, but my below code msgbox will pop-up multiple times even if the FileFindFirstFile() function has no more file(s) found from our server path. But when I test it locally in my computer, it will pop-up only once and no issue found. Here's the code: #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> Opt("TrayIconHide", 1) GUISetState(@SW_HIDE) While 1 Sleep(1000) $LogPath = "\\Z:Server\Programs\PublicDirectory\Logs\*" Local $hSearch = FileFindFirstFile($LogPath) $sFileName = FileFindNextFile($hSearch) If FileExists($LogPath & ".log") Then MsgBox(64, "Warning: " & _RemoveFileExt($sFileName),"Unauthorized file found: " & @CRLF & " " & _RemoveFileExt($sFileName)) FileDelete("\\Z:Server\Programs\PublicDirectory\Logs\" & $sFileName) EndIf WEnd Func _RemoveFileExt($string) Return StringLeft($string,StringInStr($string,".",Default,-1)-1) EndFunc This code will loop to our heads PC checking our server path waiting for any files to detect, from my sample, I use ".log". Once that path will have .log saved by our production, it will automatically pop-up the message to our head director that someone saved a file in our server without permission. So basically that file will be deleted after the message. The problem is, MsgBox() will pop-up multiple times even if no more file is found from the path. Any advise is helpful and appreciated. EDIT: missed to add, this happened when the code was compiled to .exe. Thanks! KS15 Edited March 6, 2018 by KickStarter15 Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare. Link to comment Share on other sites More sharing options...
Bilgus Posted March 6, 2018 Share Posted March 6, 2018 (edited) If $hSearch <> -1 and FileExists($LogPath & ".log") Then MsgBox(64, "Warning: " & _RemoveFileExt($sFileName),"Unauthorized file found: " & @CRLF & " " & _RemoveFileExt($sFileName)) if Not FileDelete("\\Z:Server\Programs\PublicDirectory\Logs\" & $sFileName) Then MsgBox(64, "Failed to remove: " & _RemoveFileExt($sFileName),"Unauthorized file found: " & @CRLF & " " & _RemoveFileExt($sFileName)) Endif EndIf My bet is it has either failed to delete or takes a bit besides that you also aren't closing the handle $hSearch #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> Opt("TrayIconHide", 1) GUISetState(@SW_HIDE) While 1 Sleep(1000) $LogPath = "\\Z:Server\Programs\PublicDirectory\Logs\*" Local $hSearch = FileFindFirstFile($LogPath) $sFileName = FileFindNextFile($hSearch) If FileExists($LogPath & ".log") Then MsgBox(64, "Warning: " & _RemoveFileExt($sFileName),"Unauthorized file found: " & @CRLF & " " & _RemoveFileExt($sFileName)) FileDelete("\\Z:Server\Programs\PublicDirectory\Logs\" & $sFileName) EndIf FileClose($hSearch) WEnd Func _RemoveFileExt($string) Return StringLeft($string,StringInStr($string,".",Default,-1)-1) EndFunc Edited March 6, 2018 by Bilgus fix the conditional Link to comment Share on other sites More sharing options...
t0nZ Posted March 6, 2018 Share Posted March 6, 2018 IF I understand, every 1000 msecs you get the list of files.log, but with that cycle you are going to process only the first one (maybe you can find 10 logs..) , and then restart for another cycle, losing the previous list and working always on only the first file.log found. My 2 cents: I think for efficiency you have to use two cycles, the first is the one that already exist, and after you can put another cycle inside to process all the files found. Bye Link to comment Share on other sites More sharing options...
Bilgus Posted March 6, 2018 Share Posted March 6, 2018 no if the file is found he deletes it and then calls findfirstfile again so it only ever processes 1 file with each loop Link to comment Share on other sites More sharing options...
KickStarter15 Posted March 6, 2018 Author Share Posted March 6, 2018 10 minutes ago, Bilgus said: no if the file is found he deletes it and then calls findfirstfile again so it only ever processes 1 file with each loop Yup, the code will loop if file exist from that path. 31 minutes ago, Bilgus said: besides that you also aren't closing the handle $hSearch I tried but the same, it will pop-up 4 times before the file will be deleted from the path. 21 minutes ago, t0nZ said: IF I understand, every 1000 msecs you get the list of files.log, but with that cycle you are going to process only the first one (maybe you can find 10 logs..) , and then restart for another cycle, losing the previous list and working always on only the first file.log found. Yes as well, however as what Bilgus said and what my code does, is every searched file it will be deleted after my msgbox and then, loop back for my FileFindNextFile() function. The problem is it will not be deleted after my Mssgbox and continue 4 times pp-ups before it will be deleted. I think the problem is the deletion of file. Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare. Link to comment Share on other sites More sharing options...
t0nZ Posted March 6, 2018 Share Posted March 6, 2018 Yes, it's exactly my point.. if you find 2 or 20 files, why not delete all the files cycling the list you have found ? Why recall FileFindFirstFile every time to process only one file ? Bilgus 1 Link to comment Share on other sites More sharing options...
Bilgus Posted March 6, 2018 Share Posted March 6, 2018 (edited) Valid point. .... .... While 1 Sleep(1000) $LogPath = "\\Z:Server\Programs\PublicDirectory\Logs\*" Local $hSearch = FileFindFirstFile($LogPath) While $hSearch <> -1 and Not @error ;Note @error is only set if folder is EMPTY $sFileName = FileFindNextFile($hSearch) If @error Then ;@error flag to 1 if no more files/directories match the search. ;ExitLoop ;oops Sleep(60000); Wait 1 minute after running a list of files ExitLoop Endif If FileExists($LogPath & ".log") Then MsgBox(64, "Warning: " & _RemoveFileExt($sFileName),"Unauthorized file found: " & @CRLF & " " & _RemoveFileExt($sFileName)) FileDelete("\\Z:Server\Programs\PublicDirectory\Logs\" & $sFileName) EndIf WEnd FileClose($hSearch) WEnd Edited March 6, 2018 by Bilgus Link to comment Share on other sites More sharing options...
Bilgus Posted March 6, 2018 Share Posted March 6, 2018 (edited) I think that might work for you that way you exhaust the whole findfile list it sleeps a bit and then rechecks ExitLoop ;oops it'd help if you did the sleep before exiting the loop though... Edited March 6, 2018 by Bilgus Link to comment Share on other sites More sharing options...
KickStarter15 Posted March 6, 2018 Author Share Posted March 6, 2018 57 minutes ago, t0nZ said: Yes, it's exactly my point.. if you find 2 or 20 files, why not delete all the files cycling the list you have found ? Why recall FileFindFirstFile every time to process only one file ? Nope, each file must be checked first and get the log report to our station. Means, we need to get the files capacity if valid or not. Else, we will delete that and notify the one who saved the file. Also, that path is exclusive for managers report only so not just .log is invalid but need to check it first if from managers or not, then do the next code. @Bilgus, Based on your code given, yes, I can do it like that kill the loop every time the folder is empty. But, even if the folder is empty still need to continue looping in it every 1000msec. It's kind of hard to think but the purpose of this code is to get the file found and validate it, then notify the one who save the file then if valid then save the file else if not valid then delete the file. Guys, The issue here is why the message box pop-up multiple times which has only one file .log was saved from that folder and even if trying to use the function FileClose(). Checking the path from server, there are two types of path we can make to it. One is name of a server, and the other one is the IP address of that server. Means, this is the IP 10.0.0.123 and the corresponding name for that IP is "server". So, I made it this way and it fixes the issue but why if we use server name and not the IP, issue will continue. "\\Z:10.0.0.123\Programs\PublicDirectory\Logs\*" Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare. Link to comment Share on other sites More sharing options...
Bilgus Posted March 6, 2018 Share Posted March 6, 2018 Its probably because you are working off a cached copy of the server contents I'm not sure how you could force it to be faster / uncached Link to comment Share on other sites More sharing options...
Bilgus Posted March 6, 2018 Share Posted March 6, 2018 I bet if you tried to open the file it'd quickly figure it out While 1 Sleep(1000) $LogPath = "\\Z:Server\Programs\PublicDirectory\Logs\*" Local $hSearch = FileFindFirstFile($LogPath) While $hSearch <> -1 and Not @error ;Note @error is only set if folder is EMPTY $sFileName = FileFindNextFile($hSearch) If @error Then ExitLoop If FileExists($LogPath & ".log") Then $hFile = FileOpen ($sFileName, 0);Open Readonly If $hFile <> -1 Then; ;and FileSetEnd ($hFile) FileClose ($hFile) MsgBox(64, "Warning: " & _RemoveFileExt($sFileName),"Unauthorized file found: " & @CRLF & " " & _RemoveFileExt($sFileName)) FileDelete("\\Z:Server\Programs\PublicDirectory\Logs\" & $sFileName) EndIf EndIf WEnd FileClose($hSearch) WEnd Link to comment Share on other sites More sharing options...
KickStarter15 Posted March 9, 2018 Author Share Posted March 9, 2018 Hi Guys, Thanks for your checking and time. I figured it out what's the cause of this issue. It's the filedelete() that won't delete the .log from the path and that is why mssgbox() pop-up multiple time until .log has been deleted. It's also a matter of path declaration from my code. Thanks! KS15 Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare. Link to comment Share on other sites More sharing options...
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