Help with if !FileExist

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
maitresin
Posts: 45
Joined: 20 Mar 2018, 19:33

Help with if !FileExist

30 Mar 2018, 22:23

Code: Select all

fileread, No1, List.txt
loop, parse, No1, `n
{
Runwait %comspec% /c ""%A_Scriptdir%\file.exe"  "Folder1"",,hide

sleep, 200
gosub, fileexist1
sleep, 100

Traytip, FileNow1, Loop: %A_LoopField%
Sleep, 100


}

msgbox, done!
exitapp
return

fileexist1:
if !FileExist(A_Scriptdir . "\folder1\" . A_LoopField)  ; main concern is here! Why it is not msgbox when the file is not found??? Every file should be in the folder but something 	 
                                                                             ;file is missing and this function should return a messagebox if the A_LoopField value is not found in the folder...
{
  msgbox, file is not existing
 }
return
Files generated and put in the folder have exactly same name as the List.txt

The !FileExist does not seem to work or I did an error??

Thanks,
gregster
Posts: 8916
Joined: 30 Sep 2013, 06:48

Re: Help with if !FileExist

30 Mar 2018, 22:44

Does the code reach the gosub at all? Or, is the loop stuck with the RunWait, waiting for file.exe to close? Add an else to the fileexist1 label/sub to see if the label gets called at all.

Also, we don't know your list.txt - and it is not clear what you have tried to debug it. Does it reach the done! msgbox?
User avatar
JoeWinograd
Posts: 2177
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Help with if !FileExist

30 Mar 2018, 23:15

There may be other issues with the code, but one definite issue is that A_LoopField exists only within the parsing loop. So your FileExist call, which is outside the loop, can't access A_LoopField. One solution is to put this as the first statement inside the loop:

CurrentFile:=A_LoopField

Then use CurrentFile in the FileExist call. Another solution is to move the FileExist call inside the loop, in which case you can use the A_LoopField variable. Regards, Joe
gregster
Posts: 8916
Joined: 30 Sep 2013, 06:48

Re: Help with if !FileExist

31 Mar 2018, 00:01

Joe, that was actually my first concern, too. But after testing this code beneath, I was surprised and deleted my original post (hadn't saved it yet). It seems, A_loopfield is accessible as long as the loop exists:

Code: Select all

Colors = red,green,blue
Loop, Parse, Colors, `,
{
    MsgBox, Color number %A_Index% is %A_LoopField%.
	gosub test
}
gosub test
return 

test:
msgbox % A_loopfield
return
So, I concluded, there has to be some other problem. But, without a doubt, your advice is the safer way to go. And who knows, perhaps this case is different than mine - although I don't see an obvious difference.

After many years using AHK, I am still surprised about certain things.
User avatar
JoeWinograd
Posts: 2177
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Help with if !FileExist

31 Mar 2018, 01:09

Hi gregster,
That comes as quite a surprise! I would never count on A_LoopField (or A_Index or any built-in loop variable) existing outside the loop. Perhaps a subroutine (and function?) called from inside a loop is also considered to be inside the loop from the perspective of built-in loop variables, but I would not have bet on that. Interesting! Regards, Joe
maitresin
Posts: 45
Joined: 20 Mar 2018, 19:33

Re: Help with if !FileExist

31 Mar 2018, 10:21

I did Joe solution but It still not working.

file.exe create a file with exactly the same name as the value in the list.txt

Code: Select all

if !FileExist(A_Scriptdir . "\folder1\" . CurrentFile) ; C:\....scriptdir\folder1\file001     please note file001 is the file and not a folder.
{
msgbox, %Currentfile%   ; show the A_LoopField value.
msgbox, the file is not existing
}
return
The file is in the folder1 with exactly the same name and it keep telling me the msgbox like if the file was not in the folder1. I added a sleep to make sure file have time to reach folder1... still same result!

Is it because my file does not have extension and fileexist require a X.X file ? (file with extension like .txt .exe ...)

Thanks,
User avatar
JoeWinograd
Posts: 2177
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Help with if !FileExist

31 Mar 2018, 11:12

I did Joe solution but It still not working.
Yes, gregster is correct. Testing here shows that A_LoopField exists in a subroutine (and function) outside the loop that is called from inside the loop, although I would personally not rely on that. However, the problem in this case is something else.
Is it because my file does not have extension and
fileexist require a X.X file ?
No. Here's a quick script that proves it:

Code: Select all

FileWithoutExt:=A_ScriptDir . "\TestNoExt"
FileAppend,HelloWorld,%FileWithoutExt%
If FileExist(FileWithoutExt)
  MsgBox file exists
Else
  MsgBox file does not exist
Run that and you will see that the FileExist call on the file without an extension says that the file exists.

Note that gregster asked a great question: Does the fileexist1 subroutine ever get executed? His point is that it may never come back from the RunWait call to file.exe. His advice to add an Else in the fileexist1 subroutine will work, or just put a MsgBox fileexist1 entered as the first line in the subroutine. Regards, Joe
maitresin
Posts: 45
Joined: 20 Mar 2018, 19:33

Re: Help with if !FileExist

31 Mar 2018, 14:05

Yes it is work because as said in previous reply: "The file is in the folder1 with exactly the same name and it keep telling me the msgbox like if the file was not in the folder1"

It does work but does not find the file... that is really weird.

file is in the folder1, then the gosub execute. The !fileexist (if file not exist) it should pop the message box. Actually the file exist in the folder1 and it still msgbox.

!FileExist = file not exist right???
gregster
Posts: 8916
Joined: 30 Sep 2013, 06:48

Re: Help with if !FileExist

31 Mar 2018, 14:12

Probably there is something wrong with the path you piece together. What happens when you hardcode an absolute path in !FileExist(...) ?
What is the content of List.txt ?
What is file.exe doing? Some file operation?
We don't have enough information to reproduce your issue, it seems... can you give us some piece of code that demonstrates the problem and that we can run 1:1 on our computers?
maitresin
Posts: 45
Joined: 20 Mar 2018, 19:33

Re: Help with if !FileExist

31 Mar 2018, 14:33

gregster wrote:Probably there is something wrong with the path you piece together. What happens when you hardcode an absolute path in !FileExist(...) ?
What is the content of List.txt ?
What is file.exe doing? Some file operation?
We don't have enough information to reproduce your issue, it seems... can you give us some piece of code that demonstrates the problem and that we can run 1:1 on our computers?
1-It return the good path

2-file001
file002
...

3-file.exe is converting and creating a file of same name as the List.txt. It create the file in the scriptdir folder1 IE: file001
maitresin
Posts: 45
Joined: 20 Mar 2018, 19:33

Re: Help with if !FileExist

31 Mar 2018, 14:37

!FileExist seem to do a FileExist and not a FileNotExist

I did test with a random filename in the folder and it return me the file exist while it is not... O.O
User avatar
JoeWinograd
Posts: 2177
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Help with if !FileExist

31 Mar 2018, 14:38

!FileExist = file not exist right???
Yes, but you need to understand that the function is FileExist. The exclamation mark is the standard Boolean NOT, which can be applied to any Boolean value, inverting its True/False value. Note that FileExist is a combination of the deprecated commands IfExist and FileGetAttrib. It returns the file attributes, but if the file does not exist, it returns a null (empty) string. As the documentation says:
Since an empty string is seen as "false", the function's return value can always be used as a quasi-boolean value. For example, the statement if FileExist("C:\My File.txt") would be true if the file exists and false otherwise.
then the gosub execute
How do you know that? Please put these lines as the first two lines after the fileexist1: label:

Code: Select all

FileFullPath:=A_Scriptdir . "\folder1\" . A_LoopField
MsgBox,,debugging,fileexist1 entered for file`n%FileFullPath%
Does that (debugging) MsgBox ever appear? If so, what is the value of FileFullPath? Btw, you may use CurrentFile there instead A_LoopField if you made the assignment statement that I recommended. Regards, Joe
gregster
Posts: 8916
Joined: 30 Sep 2013, 06:48

Re: Help with if !FileExist

31 Mar 2018, 14:41

maitresin wrote:!FileExist seem to do a FileExist and not a FileNotExist

I did test with a random filename in the folder and it return me the file exist while it is not... O.O
All my tests say something else. You must be doing something wrong.

That's why I wrote: Give us source code that we can run 1:1 and that will reproduce your problem.
We cannot test your code, because we have no file.exe and no List.txt
maitresin
Posts: 45
Joined: 20 Mar 2018, 19:33

Re: Help with if !FileExist

31 Mar 2018, 14:46

Ignore last reply.

Debugging msgbox appear with the good path and filename.
gregster
Posts: 8916
Joined: 30 Sep 2013, 06:48

Re: Help with if !FileExist

31 Mar 2018, 14:46

Or take Joe's code from above:

Code: Select all

FileWithoutExt:=A_ScriptDir . "\TestNoExt"
FileAppend,HelloWorld,%FileWithoutExt%
If FileExist(FileWithoutExt)
  MsgBox file exists
Else
  MsgBox file does not exist

It works for me. Does it for you?
User avatar
JoeWinograd
Posts: 2177
Joined: 10 Feb 2014, 20:00
Location: U.S. Central Time Zone

Re: Help with if !FileExist

31 Mar 2018, 14:48

!FileExist seem to do a FileExist and not a FileNotExist
I promise you it doesn't! Run this script to prove it:

Code: Select all

FileWithoutExt:=A_ScriptDir . "\TestNoExt"
FileAppend,HelloWorld,%FileWithoutExt%
; both should say file exists
If FileExist(FileWithoutExt)
  MsgBox file exists
Else
  MsgBox file does not exist
If !FileExist(FileWithoutExt)
  MsgBox file does not exist
Else
  MsgBox file exists
; both should say file does not exist
FileDelete,%FileWithoutExt%
If FileExist(FileWithoutExt)
  MsgBox file exists
Else
  MsgBox file does not exist
If !FileExist(FileWithoutExt)
  MsgBox file does not exist
Else
  MsgBox file exists
Regards, Joe
maitresin
Posts: 45
Joined: 20 Mar 2018, 19:33

Re: Help with if !FileExist

31 Mar 2018, 15:23

Code: Select all

FileExist1:
CurrentFile = file001
FileFullPath:=A_Scriptdir . "\folder1\" . CurrentFile
MsgBox,,debugging,fileexist1 entered for file`n%FileFullPath%
sleep, 500
if !FileExist(FileFullPath)
{
msgbox, file %Currentfile% not exist! [b];it send this message![/b]
run, notepad.exe [b]; notepad open![/b]
}
else
{
  msgbox, file exist!
}
return

file001 is in the scriptdir in folder: folder1
debugging message return the correct path and filename.
the !FileExist... execute the msgbox and notepad! the file is in the folder!
gregster
Posts: 8916
Joined: 30 Sep 2013, 06:48

Re: Help with if !FileExist

31 Mar 2018, 15:40

Well, if I run this code, it works like expected for me. I created the file named file001 in a subfolder folder1 of the script directory.
Then, the script reports "file exist!". :thumbup:

When I rename or delete file001 - or add an extension, I get file file001 not exist! :thumbup:
All good!!

But I am not sure what you are trying to do with tags, they are not supported in code boxes or AHK source code - I think you are trying to add comments: then just use a ; after at least one space.

Are you sure that your file really hasn't any extension? Windows hides known file extensions per default, you know that, right :?: :!: :wave:
To see all file extensions, you will have to activate that option in view>folder options of the file explorer...

What does file explorer report as the file type of file001 ? What do you see in 'file properties' ?
maitresin
Posts: 45
Joined: 20 Mar 2018, 19:33

Re: Help with if !FileExist

31 Mar 2018, 15:49

Well, the gosub inside a loop, parse seem to be problematic.

Everything work fine in the msgbox debugging but the fileexist seem to not work.

add this code into a loop, parse... currentfile variable is the A_LoopField in the loop parse.

I dont know why, I run this without the loop, parse thing and it work but inside... it look like it not find the file.
gregster
Posts: 8916
Joined: 30 Sep 2013, 06:48

Re: Help with if !FileExist

31 Mar 2018, 15:56

Then put the complete code of the subroutine into the loop or use Joe's workaround with CurrentFile. In both cases it should work, while you are not using A_loopfile outside of the loop.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: marypoppins_1 and 114 guests