Sign in to follow this  
Followers 0
RGong

FileExists() Not Work As Expected

6 posts in this topic

I used FileExists() many times and never had doubts on it until today. There is a file named "FlashUtil64_10_3_162_ActiveX.exe" in folder "C:\Windows\System32\Macromed\Flash". FileExists() always returned 0. I checked the file permissions and made sure they are fine. But the problem is still the same. I did a test on other file related functions, and only FileGetVersion() can "see" the file. Can somebody tell me what is going on here? My OS is Windows 7 64bit.

Dim $file="C:\Windows\System32\Macromed\Flash\FlashUtil64_10_3_162_ActiveX.exe"
ConsoleWrite("File Exist?   = "&FileExists($file)&@CRLF)
ConsoleWrite("File Attrib   = "&FileGetAttrib($file)&@CRLF)
ConsoleWrite("File Size     = "&FileGetSize($file)&@CRLF)
ConsoleWrite("File Time     = "&FileGetTime($file)&@CRLF)
ConsoleWrite("File Version  = "&FileGetVersion($file)&@CRLF)

Output:

File Exist?     = 0
File Attrib     = 
File Size       = 0
File Time       = 
File Version    = 10.3.162.28

Share this post


Link to post
Share on other sites



Windows 7 is protective over what an exe does in protected directories like System32 (System32 is a very critical directory). Compile your script, right click on the compiled script and Run As > Administrator. See if that works for you.


#include <ByteMe.au3>

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Compiled the script and ran as Administrator, the result was the same. Tested other files in system32 folder. Some of them, such as "Defrag.exe" and "SnippingTool.exe", were not correctly reported.

Dim $aApps[12]

$aApps[0]="c:\Windows\system32\Defrag.exe"
$aApps[1]="c:\Windows\system32\dfrgui.exe"
$aApps[2]="c:\Windows\system32\diskpart.exe"
$aApps[3]="c:\Windows\system32\dxdiag.exe"
$aApps[4]="c:\Windows\system32\mmc.exe"
$aApps[5]="c:\Windows\system32\msinfo32.exe"
$aApps[6]="c:\Windows\system32\msiexec.exe"
$aApps[7]="c:\Windows\system32\notepad.exe"
$aApps[8]="c:\Windows\system32\Taskmgr.exe"
$aApps[9]="c:\Windows\system32\regedt32.exe"
$aApps[10]="c:\Windows\system32\regsvr32.exe"
$aApps[11]="c:\Windows\system32\SnippingTool.exe"

For $i=0 To UBound($aApps)-1
    ConsoleWrite("File Name    = "&$aApps[$i]&@CRLF)
    ConsoleWrite("File Exist?  = "&FileExists($aApps[$i])&@CRLF)
    ConsoleWrite("File Version = "&FileGetVersion($aApps[$i])&@CRLF&@CRLF)
Next

Output:

File Name    = c:\Windows\system32\Defrag.exe
File Exist?  = 0
File Version = 6.1.7600.16385

File Name    = c:\Windows\system32\dfrgui.exe
File Exist?  = 1
File Version = 6.1.7601.17514

File Name    = c:\Windows\system32\diskpart.exe
File Exist?  = 1
File Version = 6.1.7601.17514

File Name    = c:\Windows\system32\dxdiag.exe
File Exist?  = 1
File Version = 6.1.7600.16385

File Name    = c:\Windows\system32\mmc.exe
File Exist?  = 1
File Version = 6.1.7600.16385

File Name    = c:\Windows\system32\msinfo32.exe
File Exist?  = 1
File Version = 6.1.7601.17514

File Name    = c:\Windows\system32\msiexec.exe
File Exist?  = 1
File Version = 5.0.7601.17514

File Name    = c:\Windows\system32\notepad.exe
File Exist?  = 1
File Version = 6.1.7600.16385

File Name    = c:\Windows\system32\Taskmgr.exe
File Exist?  = 1
File Version = 6.1.7601.17514

File Name    = c:\Windows\system32\regedt32.exe
File Exist?  = 1
File Version = 6.1.7600.16385

File Name    = c:\Windows\system32\regsvr32.exe
File Exist?  = 1
File Version = 6.1.7600.16385

File Name    = c:\Windows\system32\SnippingTool.exe
File Exist?  = 0
File Version = 6.1.7600.16385

But finally, I found the reason - It is because the script was running in 32bit mode during debugging. If you go to "Tools" >> "Compile", choose "Output arch" as "Use X64 version", click "Save Only" (Do NOT have to compile) and then run the script, the output is right.

Here comes another question: My programs need to be run in both 32bit and 64bit OS. Usually, I compiled them as 32bit so that they can work in both environments. Now it looks like that I have to compile 2 different versions and choose which version to use during run-time. Or maybe, only for this case, use FileGetVersion() to test file existence. What is the best practice to make sure a program can run correctly in both 32bit and 64bit OS?

Edited by RGong

Share this post


Link to post
Share on other sites

Thanks, JFX. Your script works.

Share this post


Link to post
Share on other sites

What is the best practice to make sure a program can run correctly in both 32bit and 64bit OS?

I would write code that could handle both.

I recommend you look at this MSDN article; Programming Guide for 64-bit Windows. If you are in a hurry, concentrate on File System Redirector

Example:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_UseX64=n
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

If @AutoItX64 Then Exit ConsoleWrite("Unsupported" & @LF)

$sApps = "Defrag.exe|dfrgui.exe|diskpart.exe|dxdiag.exe|mmc.exe|msinfo32.exe|msiexec.exe|notepad.exe|Taskmgr.exe|regedt32.exe|regsvr32.exe|SnippingTool.exe"
$asApps = StringSplit($sApps, "|")

For $iX = 1 To $asApps[0]
    If @OSArch = "X86" Then
        $asApps[$iX] = @SystemDir & "\" & $asApps[$iX]
    ElseIf @OSArch = "X64" Then
        $asApps[$iX] = @WindowsDir & "\Sysnative\" & $asApps[$iX]
    Else
        MsgBox(0, "Title", "lol")
    EndIf
Next

For $iX = 1 To $asApps[0]
    ConsoleWrite("File Name    = "& $asApps[$iX] & @CRLF)
    ConsoleWrite("File Exist?  = "& FileExists($asApps[$iX]) & @CRLF)
    ConsoleWrite("File Version = "& FileGetVersion($asApps[$iX]) & @CRLF)
Next

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
Sign in to follow this  
Followers 0