Jump to content
NiceBoy1234

FileFindFirstFile Mistake

Recommended Posts

NiceBoy1234

So I tried to get the first file of a folder with FileFindFirstFile but it always gives me a 1 as a value, so it tells me that the folder is empty.
But the folder is not empty.
Code:

Local $path_times_text_01 = "D:\xxx\test\xxx\"
   Local $search = FileFindFirstFile($path_times_text_01 & "*.*")

   Timecheck()

   Func Timecheck()

      ConsoleWrite($search & @CRLF)

      If $search = -1 Then
         MsgBox(0, "Error", "could not find extension")
         Exit
      EndIf

      If $search = 1 Then
         MsgBox(0, "Fehler", "folder is empty")
         Exit
      EndIf

      ConsoleWrite($search & @CRLF)
      
   EndFunc

This does not make any sense to make because again the folders are not empty.

Share this post


Link to post
Share on other sites
water

Did you read the help file?
FileFindFirstFile returns 'a search "handle" for use with subsequent FileFindNextFile functions.'


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
VIP
#include <File.au3>
Global $sPath = @DesktopDir & "\FF";"D:\xxx\test\"
Global $sFilter = "*.*";"*.mp3"

If Not FileExists($sPath) Then Exit MsgBox(48, "Trong.CF", "Folder is Not found: " & $sPath)
If FileExists($sPath & "\*") Then
    Local $sListFile = _FileListToArray($sPath, $sFilter)
    If Not @error Or IsArray($sListFile) Then
        For $i = 1 To $sListFile[0] - 1
            ConsoleWrite("File " & $i & ": " & $sListFile[$i] & @CRLF)
        Next
        MsgBox(0, "Trong.CF", "Found: " & $sListFile[0] - 1 & " files in " & $sPath)
    Else
        MsgBox(48, "Trong.CF", "Could not find " & $sFilter & " in " & $sPath)
    EndIf
Else
    MsgBox(48, "Trong.CF", "Folder is empty: " & $sPath)
EndIf

Exit

 

Edited by Trong

Regards,
 

Share this post


Link to post
Share on other sites
water

Trong,
your solution is a performance hog.
You create a list of ALL files in a folder while the OP only wants to "get the first file of a folder".
So I think the way to go is with FileFindFirstFile.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
BrewManNH

I think you might want to define what you mean by first file? Windows doesn't guarantee that files will be in any set order in a folder. Just because Explorer can sort them doesn't mean that's how they're stored on the disk in the file system.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
VIP

Trong,
your solution is a performance hog.
You create a list of ALL files in a folder while the OP only wants to "get the first file of a folder".
So I think the way to go is with FileFindFirstFile.

with FileFindFirstFile

Global $sFilePath = @DesktopDir ;"D:\xxx\test\"
Global $sFilter = "*.*";"*.mp3"

If Not FileExists($sFilePath) Then Exit MsgBox(48, "Trong.CF", "Folder is Not found: " & $sFilePath)
If FileExists($sFilePath & "\*") Then
    
    Local $FirstFileName = ""
    $sFilePath = StringRegExpReplace($sFilePath, "[\\/]+$", "") & "\" ; Ensure a single trailing backslash
    If StringRegExp($sFilter, "[\\/:><\|]|(?s)^\s*$") Then Exit MsgBox(48, "Trong.CF", "Invalid $sFilter ! ")
    Local $hSearch = FileFindFirstFile($sFilePath & $sFilter)
    If @error Then Exit MsgBox(48, "Trong.CF", "Could not find " & $sFilter & " in " & $sFilePath)
    While 1
        $FirstFileName = FileFindNextFile($hSearch)
        If @error Then ExitLoop
        If (1 + @extended = 2) Then ContinueLoop
        FileClose($hSearch)
        Exit MsgBox(0, "Trong.CF", "In: " & $sFilePath & @CRLF & "Filter: " & $sFilter & @CRLF & "Found first file: " & $FirstFileName)
    WEnd
    MsgBox(48, "Trong.CF", "Could not find " & $sFilter & " in " & $sFilePath)
Else
    MsgBox(48, "Trong.CF", "Folder is empty: " & $sFilePath)
EndIf

Exit

 

Edited by Trong

Regards,
 

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

  • Similar Content

    • nooneclose
      By nooneclose
      My program has to first search for names in Column D that do not match up with column C. I got that search to work using arrays. It was slow and I could not figure out how to delete them so I just manually put coded the names that do not belong. I found their cell location but I do not know how to store that location and delete it.
      This is what I have so far.
      Local $NameToDelete1[6]  = _Excel_RangeFind($OpenWorkbook, "Smith, Bill") _ArrayDisplay($NameToDelete1, "Excel UDF: _Excel_RangeFind Example 1", "", 0, "|", "Sheet|Name|Cell|Value|Formula|Comment") _Excel_RangeDelete($OpenWorkbook.ActiveSheet, $NameToDelete1[2], $xlShiftUp)  
      Please help, I wanted to have this program done yesterday but I did not see this problem until yesterday. 
    • AndyS19
      By AndyS19
      I'm trying to implement a Ctl-F popup box that looks something like the one that Notepad uses, but I'm not havine much luck.  I intend to get it working, then beef up the popup's contents to add several checkboxes, buttons and radio boxes.
      What my example code does is to use InputBox(), but that's not what I want.
      Here is my test code:
      #include <Array.au3> #include <GUIConstantsEx.au3> #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 Opt("GUICloseOnESC", 1) Opt("GUIOnEventMode", 1) Opt('MustDeclareVars', 1) OnAutoItExitRegister("ExitStageLeft") Opt("WinTitleMatchMode", -2) Global $hGUI _Main() Func _Main() $hGUI = GUICreate("Test ^F", 300, 200) setupSpecialKeysHandlers() GUISetOnEvent($GUI_EVENT_CLOSE, "Event_GUIClose") GUISetState() While (1) Sleep(157) WEnd EndFunc ;==>_Main Func handle_CTRL_F_key() Local $str $str = InputBox("Search", "Enter the string to search for:") ConsoleWrite("+++: $str ==>" & $str & "<==" & @CRLF) EndFunc ;==>handle_CTRL_F_key Func ExitStageLeft() Exit (99) EndFunc ;==>ExitStageLeft Func Event_GUIClose() Exit (1) EndFunc ;==>Event_GUIClose Func setupSpecialKeysHandlers() Local $ar, $parts, $key, $handler, $id Local $aAccelKeys[1][2] ; Create a table of Special keys and their handlers $ar = StringSplit("", "") _ArrayAdd($ar, "^f - handle_CTRL_F_key ") ReDim $aAccelKeys[UBound($ar) - 1][2] ; Now, create $aAccelKeys array with the table data. ; For each entry, create a Dummy GUI and associate its ; ID with the special key. For $ndx = 1 To UBound($ar) - 1 $parts = StringSplit($ar[$ndx], "-", 2) $key = StringStripWS($parts[0], 8) $handler = StringStripWS($parts[1], 8) $id = GUICtrlCreateDummy() $aAccelKeys[$ndx - 1][0] = $key $aAccelKeys[$ndx - 1][1] = $id GUICtrlSetOnEvent($id, $handler) Next GUISetAccelerators($aAccelKeys) ; Setup the Special keys hooks EndFunc ;==>setupSpecialKeysHandlers  
    • Atoxis
      By Atoxis
      Howdy, I've gone through a lot of au3 forums, and I once had a working Imagesearch script that I got from here.  However, and i'm just totally not sure how but my imagesearch scripts aren't working anymore.
      I'm not new to au3 but i'm not the most experienced with it's syntax/commands.

      Anyways, I've looked over the big threads involving imagesearch.

      Does anyone have a working Imagesearch x64 for win10 that is currently working as of the date with the post.

      Dll's and what not is fine, just when I tell the script to run, I want to be able to find the image on the screen!
      Can't find a working copy so if anyone has one please send it my way lol.

      I've taken all the imagesearch downloads and what not and have played with them but I can't get any of them working on my end, despite others saying they're working.
      Thanks.
    • Trisha
      By Trisha
      Hello,
      I Have clicked on save as option to save a file, while doing that I need to rename a file appending with sysdate. I have searched in google find the below one line of code:
      FileMove("C:\somefile.txt", "C:\somefile1.txt"), When I am trying to append with sysdate. It is not happening. Please help me out  with the small issue.
    • lenclstr746
      By lenclstr746
      HELLO GUYS
      I'm a work on a background see and click bot project 
      I can complete it if your help me
      (using imagesearch , gdi+ and  fastfind)
×