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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

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

    • 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)
    • dadalt95
      By dadalt95
      Perform a simple google search!
      The script below works fine until fill the google form!
      What I can't find is how to submit the form, tried a couple of ways and none of them worked.

       
      #include <IE.au3> $oIE = _IECreate ("www.google.com") $o_form = _IEFormGetObjByName ($oIE, "f") $o_login = _IEFormElementGetObjByName ($o_form, "q") $username = "80251369" _IEFormElementSetValue ($o_login, $username) $o_numer = _IEGetObjByName($o_form, "btnK") _IEAction ($o_numer, "click")  
      The code runs without any problem.
      I don't know how to proceed!
      Thanks in advance!
    • nacerbaaziz
      By nacerbaaziz
      Hi dears
      how are you? I hope You fine
      I have a question please
      I've created a listView
      It has several columns
      Is there any way  to search for text in an element of this list with text in all columns
      for example
      list view with 2 column
      the first is the file name and the second is the file path
      and i want to search for the item witch Containt the name and the path toGether
      I searched a lot but could not find what I was looking for
      If you do not understand the idea that I'm looking for, I can put an example
      Thanks in advance
    • lewisg
      By lewisg
      Suddenly a RunWait command has stop working after 2 years of no errors, issues, or problems. The code uses RunWait to start Plink.exe, a command-line remote connection tool similar to UNIX ssh. I'm using it to ssh to a linux (Centos) machine, run a Perl script, and redirect the output to a file on a PC running the AutoIt script.  
       
      $FilePath = "C:\AutoIT\LED" $FilePathPlus = $FilePath & "\plink.exe" $Code1 = RunWait(@ComSpec & " /c " & $FilePathPlus & " -ssh -l root -pw ?????? 10.170.4.163 /usr/local/nagios/etc/led.pl > C:\AutoIT\LED\led.txt ", @SW_SHOW) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : @ComSpec & " /c " & $FilePathPlus & " -ssh -l root -pw ????? 10.170.4.163 /usr/local/nagios/etc/led.pl > C:\AutoIT\LED\led.txt " = ' & @ComSpec & " /c " & $FilePathPlus & " -ssh -l root -pw ?????? 10.170.4.163 /usr/local/nagios/etc/led.pl > C:\AutoIT\LED\led.txt " & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console The ConsoleWrite output when cut-n-pasted into a DOS box produces the expected file so I know it works. I've also tried many variations of the function moving, adding, and changing the " and ' (quote) marks. Also tried it without the @ComSpec macro and other related functions...e.g.  ShellExecuteWait , etc.
      The PC is a Windows 10 64bit and AutoIT is version 3.3.14.2.
      Searching here and Google has not yielded any clues that helped. 
    • Gowrisankar
      By Gowrisankar
      Hello everyone,
      I'm working on a task where, a PDF file is opened (in IE browser) when I click a link in a website.
      I have to read the first page of the PDF to find particular strings. Can you please share some ideas?
×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.