Page 1 of 1

ExamineDirectory() not displaying all files ?

Posted: Thu Jan 04, 2024 7:12 pm
by lisa_beille
Hello
I've just noticed that when I list the files of c:\windows\system32\*.* with dir command i get 4391 files and 139 folders
But when i use ExamineDirectory() it shows only 2898 entries !!

What could be the issue and how to fix it ? Thanks

Here's my code :

Code: Select all

If ExamineDirectory(0, "c:\windows\system32", "*")
  While NextDirectoryEntry(0)
    i + 1
  Wend
EndIf
Debug i

Re: ExamineDirectory() not displaying all files ?

Posted: Thu Jan 04, 2024 7:26 pm
by Mindphazer
No problem here.
ExamineDirectory returns 4178 files
Dir returns 4045 Files and 124 directories. I guess there are hidden files

Re: ExamineDirectory() not displaying all files ?

Posted: Thu Jan 04, 2024 7:54 pm
by lisa_beille
Mindphazer wrote: Thu Jan 04, 2024 7:26 pm No problem here.
ExamineDirectory returns 4178 files
Dir returns 4045 Files and 124 directories. I guess there are hidden files
and how to show hidden files in examineDirectory() then ??

Re: ExamineDirectory() not displaying all files ?

Posted: Thu Jan 04, 2024 9:02 pm
by Little John
lisa_beille wrote: Thu Jan 04, 2024 7:54 pm and how to show hidden files in examineDirectory() then ??
ExamineDirectory() does show hidden directory entries.
First step is to use a bit more detailed code snippet:

Code: Select all

Define.i files, dirs, hf, hd

If ExamineDirectory(0, "c:\windows\system32", "*.*")
   While NextDirectoryEntry(0)
      If DirectoryEntryType(0) = #PB_DirectoryEntry_File
         files + 1
         If DirectoryEntryAttributes(0) & #PB_FileSystem_Hidden
            hf + 1
         EndIf   
      Else
         dirs + 1
         If DirectoryEntryAttributes(0) & #PB_FileSystem_Hidden
            hd + 1
         EndIf   
      EndIf
   Wend
   FinishDirectory(0)
EndIf

Debug Str(files) + " files (" + hf + " hidden)"
Debug Str(dirs) + " directories (" + hd + " hidden)"
The result is here (PureBasic 6.04 LTS (x64) on Windows 11):
4641 files (0 hidden)
151 directories (1 hidden)
The dir command yields:
4641 files
140 directories (1 hidden)


So my question in this context is: Why are 11 directories missing with Windows' dir command?

@lisa_beille:
What is your Windows version, and what is your PureBasic version?

Re: ExamineDirectory() not displaying all files ?

Posted: Thu Jan 04, 2024 9:08 pm
by DarkDragon
Print them into a file and fgrep them like this:

Code: Select all

fgrep -v -f pb_output.txt dir_output.txt
Are they marked as system files maybe? Can you see pagefil.sys for example under C:?

Re: ExamineDirectory() not displaying all files ?

Posted: Thu Jan 04, 2024 11:14 pm
by ChrisR
lisa_beille wrote: Thu Jan 04, 2024 7:12 pm I've just noticed that when I list the files of c:\windows\system32\*.* with dir command i get 4391 files and 139 folders
But when i use ExamineDirectory() it shows only 2898 entries !!
Given the significant difference in the number of files, it is clearly a problem of file system redirection.
Compile with the same architecture, x64 or disables file system redirection with Wow64DisableWow64FsRedirection Api.

Re: ExamineDirectory() not displaying all files ?

Posted: Fri Jan 05, 2024 12:04 am
by lisa_beille
Little John wrote: Thu Jan 04, 2024 9:02 pm
lisa_beille wrote: Thu Jan 04, 2024 7:54 pm and how to show hidden files in examineDirectory() then ??
ExamineDirectory() does show hidden directory entries.
First step is to use a bit more detailed code snippet:

Code: Select all

Define.i files, dirs, hf, hd

If ExamineDirectory(0, "c:\windows\system32", "*.*")
   While NextDirectoryEntry(0)
      If DirectoryEntryType(0) = #PB_DirectoryEntry_File
         files + 1
         If DirectoryEntryAttributes(0) & #PB_FileSystem_Hidden
            hf + 1
         EndIf   
      Else
         dirs + 1
         If DirectoryEntryAttributes(0) & #PB_FileSystem_Hidden
            hd + 1
         EndIf   
      EndIf
   Wend
   FinishDirectory(0)
EndIf

Debug Str(files) + " files (" + hf + " hidden)"
Debug Str(dirs) + " directories (" + hd + " hidden)"
The result is here (PureBasic 6.04 LTS (x64) on Windows 11):
4641 files (0 hidden)
151 directories (1 hidden)
The dir command yields:
4641 files
140 directories (1 hidden)


So my question in this context is: Why are 11 directories missing with Windows' dir command?

@lisa_beille:
What is your Windows version, and what is your PureBasic version?
my result :
2793 files (0 hidden)
105 directories (0 hidden)

i use pb 6.02 x86 on windows 11 64bits

Re: ExamineDirectory() not displaying all files ?

Posted: Fri Jan 05, 2024 12:06 am
by lisa_beille
Little John wrote: Thu Jan 04, 2024 9:02 pm
lisa_beille wrote: Thu Jan 04, 2024 7:54 pm and how to show hidden files in examineDirectory() then ??
ExamineDirectory() does show hidden directory entries.
First step is to use a bit more detailed code snippet:

Code: Select all

Define.i files, dirs, hf, hd

If ExamineDirectory(0, "c:\windows\system32", "*.*")
   While NextDirectoryEntry(0)
      If DirectoryEntryType(0) = #PB_DirectoryEntry_File
         files + 1
         If DirectoryEntryAttributes(0) & #PB_FileSystem_Hidden
            hf + 1
         EndIf   
      Else
         dirs + 1
         If DirectoryEntryAttributes(0) & #PB_FileSystem_Hidden
            hd + 1
         EndIf   
      EndIf
   Wend
   FinishDirectory(0)
EndIf

Debug Str(files) + " files (" + hf + " hidden)"
Debug Str(dirs) + " directories (" + hd + " hidden)"
The result is here (PureBasic 6.04 LTS (x64) on Windows 11):
4641 files (0 hidden)
151 directories (1 hidden)
The dir command yields:
4641 files
140 directories (1 hidden)


So my question in this context is: Why are 11 directories missing with Windows' dir command?

@lisa_beille:
What is your Windows version, and what is your PureBasic version?
when i use PB x64, the results are correct. but i don't understand why ??

Re: ExamineDirectory() not displaying all files ?

Posted: Fri Jan 05, 2024 12:27 am
by AZJIO
Because it is looking for files in a different folder.
An x86 application uses the SysWOW64 folder when accessing the System32 folder because System32 contains x64 resources

Try this, it should be perfect

Code: Select all

EnableExplicit

; https://www.purebasic.fr/english/viewtopic.php?t=82308
; Debug FileDirListCmd(Files(), "C:\Windows\System32", "*", 0, 0, 0) ; CMD + Dir
; ; ForEach Files()
; ;     Debug Files()
; ; Next
; 
; ; 4853


; If ExamineDirectory(0, "C:\Windows\System32", "*")
;   While NextDirectoryEntry(0)
;     i + 1
;   Wend
; EndIf
; Debug i
; 4855

; https://www.purebasic.fr/english/viewtopic.php?p=581152#p581152
CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
	Procedure.i Is64BitOS()
		Protected HDLL, IsWow64Process_, Is64BitOS
		
		If SizeOf(Integer) = 8
			Is64BitOS = 1   ; this is a 64 bit exe
		Else
			HDll = OpenLibrary(#PB_Any, "kernel32.dll")
			If HDll
				IsWow64Process_ = GetFunction(HDll, "IsWow64Process")
				If IsWow64Process_
					CallFunctionFast(IsWow64Process_, GetCurrentProcess_(), @Is64BitOS)
				EndIf
				CloseLibrary(HDll)
			EndIf
		EndIf
		
		ProcedureReturn Is64BitOS
	EndProcedure
CompilerEndIf

Global x86_64, Wow64
CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
	Wow64 = 1
CompilerElse
	Wow64 = 0
CompilerEndIf

CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
	If Is64BitOS()
		x86_64 = 64
	Else
		x86_64 = 32
	EndIf
CompilerElse
; 	since the x64 program will run only on OS_x64, we unambiguously set the variables
	x86_64 = 64
CompilerEndIf
; Debug x86_64


; to disable redirection to the SysWOW64 folder when using an x86 program
Global IsWow64ProcessFlag, hKrnDLL, *Func
; Redirection conditions: file 32 bit, system 64 bit and higher than WinXP,
; otherwise we simply disable redirection with the Wow64=0 flag (forced)
CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
	If x86_64 = 64 And OSVersion() >= #PB_OS_Windows_Vista
		hKrnDLL = OpenLibrary(#PB_Any, "Kernel32.dll")
		If hKrnDLL
			*Func = GetFunction(hKrnDLL, "IsWow64Process")
			If *Func
				CallFunctionFast(*Func, GetCurrentProcess_(), @IsWow64ProcessFlag)
				*Func = 0
				If IsWow64ProcessFlag And SizeOf(Integer) = 4
					*Func = GetFunction(hKrnDLL, "Wow64DisableWow64FsRedirection")
				EndIf
			EndIf
		EndIf
	Else
	; 	for x64 redirection is not needed, so disable it
		Wow64 = 0
	EndIf
CompilerEndIf


; WindowsXP
If OSVersion() < #PB_OS_Windows_Vista Or Not *Func
	Wow64 = 0
EndIf



If Wow64
	CallFunctionFast(*Func, 0)
EndIf

Define i
If ExamineDirectory(0, "C:\Windows\System32", "*")
	While NextDirectoryEntry(0)
		i + 1
	Wend
EndIf
Debug i
; 4855

If Wow64
	CallFunctionFast(*Func, 1)
EndIf

CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
	If IsLibrary(hKrnDLL)
		CloseLibrary(hKrnDLL)
	EndIf
CompilerEndIf

Re: ExamineDirectory() not displaying all files ?

Posted: Fri Jan 05, 2024 8:30 am
by Little John
lisa_beille wrote: Fri Jan 05, 2024 12:04 am i use pb 6.02 x86 on windows 11 64bits
That's what I suspected. :-)
You have already received a detailed explanation.