Announcement

Collapse
No announcement yet.

Dir Function

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • #21
    Originally posted by Rodney Hicks View Post
    Perhaps more clearly:

    A$ = DIR$("*.htm") will match
    "Index.htm"
    "Default.html"
    "Homepages.htmb"
    "cgilib.htmlpages"
    Which in my opinion is a little inconsistent. How do you match ONLY files that end with ".htm"?
    www.pluribasic.com

    Comment


    • #22
      >Which in my opinion is a little inconsistent. How do you match ONLY files that end with ".htm"?

      You might try (meaning I did not) ....
      Code:
        D$ = "*.HTM" &  CHR$(0)
        A$ = DIR$ (D$)
      Michael Mattias
      Tal Systems Inc.
      Racine WI USA
      mmattias@talsystems.com
      http://www.talsystems.com

      Comment


      • #23
        Which in my opinion is a little inconsistent. How do you match ONLY files that end with ".htm"?
        DISPLAY OPENFILE suggests syntax such as Michael has posted in post #22, ergo, I suspect it will work with DIM as well. DISPLAY OPENFILE can return multiple files so may be an alternate solution.
        Rod
        "To every unsung hero in the universe
        To those who roam the skies and those who roam the earth
        To all good men of reason may they never thirst " - from "Heaven Help the Devil" by G. Lightfoot

        Comment


        • #24
          Alternately... you can eschew the use of DISPLAY OPENFILE and use instead..
          Explorer-Style hook Procedures for OpenFileDialog and SaveFileDialog 3-31-07

          That has the hook procedure in there already to exclude *.exe files; no reason you could not change the hook procedure to exclude files not named exactly "something.HTM."

          (FWIW, "openfiledialog" was a PB-Provided user function shipped with the compiler before the DISPLAY OPENFILE statement was added to the PowerBASIC dialect).
          Michael Mattias
          Tal Systems Inc.
          Racine WI USA
          mmattias@talsystems.com
          http://www.talsystems.com

          Comment


          • #25
            1. "mask$ specifies a filename or path which can include a drive name and system wildcard characters (* and ?)."

            2. "For example, A$ = DIR$("*.htm") will match filenames such as "Index.htm", "Default.html", "Homepages.htmb", "cgilib.htmlpages", etc. Similarly, A$ = DIR$("*.h??") and DIR$("*.ht*") will match the same filenames."


            Knock over this DOS guy with a feather!

            Since DOS 2.11, I have always understood the "?" wildcard to match "1 character only".
            (...can you hear Sean Connery in "Hunt for Red October" saying that? LOL! "That's right, Vasily: one. character. only." ...eyebrow goes up...)

            But in a Win7Pro cmd window, I just tried it and I see that "dir *.htm" returns both: files having ONLY ".HTM" extensions, AS WELL as files having ".HTML" extensions.

            Quite unexpected! (I know, Michael, I should RTFM for my OS once in awhile... OK, ok, I will... But really, I wonder when that changed?)

            So anyway, I think I'm a good example of why the Help should be explicit in its examples, showing BOTH what result IS expected, and what result IS NOT expected. Often, it's not enough to show some simplistic example of the simplest correct usage. The Help file has to establish a clear boundary, a "fence", showing some examples of proper usage that is "in", and some examples of improper usage that is "out". (One way of learning is "compare and contrast"; helps bring out the differences.)

            I know it's not "Be kind to dinosaurs" week, but that's not a bad motto to keep in mind...

            I really appreciate the opportunity to comment in this online Help, but I do hope that someday it is condensed, and the commentary/dialog removed, so that the reader does not have to keep track of whose assertions were later tweaked for clarification, and which statements were overturned, etc. In fact, when that someday comes, I'd be happy to help with that effort. Just let me get one more year of billable work in before I retire...

            -John

            Comment


            • #26
              ... but I do hope that someday it is condensed,...
              FWIW, John, the original idea was for members to post suggested wording to clarify and sample code to support assertions so that the Powers That Be could just select the most appropriate when time came to release another version, thus some of the work would be done for them. But, discussion is necessary for some things and the discussions have been topical for the most part which enhances these forums.

              Thus, your suggestion for condensing the content is valid. And perhaps timely!
              Rod
              "To every unsung hero in the universe
              To those who roam the skies and those who roam the earth
              To all good men of reason may they never thirst " - from "Heaven Help the Devil" by G. Lightfoot

              Comment


              • #27
                Originally posted by Michael Mattias View Post
                You might try (meaning I did not) ....
                Code:
                D$ = "*.HTM" & CHR$(0)
                A$ = DIR$ (D$)
                Didnt wor'k. But that's ok. Nothing that a little extra coding cannot fix. We are programmers after all.
                www.pluribasic.com

                Comment


                • #28
                  Hey,
                  What you see is normal behavior. It is not directly related to PowerBASIC DIR$ api wrapper
                  but more to Microsoft FindFirstFile/FindNextFile function.

                  The search includes both the long and short file names.
                  .cFilename is for the long file name and
                  .cAlternateFileName is for the 8.3 short file name version.
                  Important: If the LFN-.cFilename could fit in a 8.3 characters pattern then
                  the SFN-.cAlternateFileName will be empty.
                  A search for "*.htm" will return "x.html"
                  because "x.html" 8.3 SFN version will be something like "blabla~1.htm"
                  Remember, the search includes both the long and short file names.
                  PathMatchSpec or other method could help to get the wanted result. :-)

                  Pierre

                  Code:
                  #COMPILE EXE '#Win 8.04#
                  #DIM ALL
                  #INCLUDE "Win32Api.inc"
                   
                  DECLARE FUNCTION PathMatchSpec LIB "SHLWAPI.DLL" ALIAS "PathMatchSpecA"(pszFile AS ASCIIZ, pszSpec AS ASCIIZ) AS LONG
                  '______________________________________________________________________________
                   
                  FUNCTION PBMAIN() AS LONG
                   LOCAL FileInfo  AS WIN32_FIND_DATA
                   LOCAL sPath     AS ASCIIZ * %MAX_PATH
                   LOCAL sMask     AS ASCIIZ * %MAX_PATH
                   LOCAL sLog      AS STRING
                   LOCAL hFileFind AS DWORD
                   
                   sMask = "*.htm" 'Saying what is wanted via a mask
                   sPath = "C:\Tmp-LFN-and-SFN\" 'Create a test folder
                   MKDIR sPath
                   OPEN sPath & "Test.ht"    FOR OUTPUT AS #1 : CLOSE #1 'Create some test file
                   OPEN sPath & "Test.htm"   FOR OUTPUT AS #1 : CLOSE #1 'Create some test file
                   OPEN sPath & "Test.html"  FOR OUTPUT AS #1 : CLOSE #1 'Create some test file
                   OPEN sPath & "Test.html2" FOR OUTPUT AS #1 : CLOSE #1 'Create some test file
                   
                   sLog = "FindFirstFile/FindNextFile"                         & $CRLF & _
                          "LFN Long file name and SFN Short file name"         & $CRLF & _
                          "Checking with PathMatchSpec for a wanted extention" & $CRLF & $CRLF & _
                          "sPath = " & sPath                                   & $CRLF & _
                          "sMask = " & sMask                                   & $CRLF & _
                          "Using ""PathMatchSpec"" to filter result."          & $CRLF & $CRLF
                   
                   hFileFind = FindFirstFile(sPath & sMask, FileInfo) 'Find the first file if any
                   IF hFileFind <> %INVALID_HANDLE_VALUE THEN 'A valid entry was found
                     DO 'Read all filenames
                       sLog = sLog & "Long = " & FileInfo.cFileName & $TAB & " Short = " & FileInfo.cAlternateFileName & $TAB
                       IF PathMatchSpec(FileInfo.cFilename, sMask) THEN 'Check filename against wildcard
                         sLog = sLog &  $TAB &  $TAB & "Valid" & $CRLF
                       ELSE 'Does not match against wildcard
                         sLog = sLog & "Not valid" & $CRLF
                       END IF
                     LOOP WHILE FindNextFile(hFileFind, FileInfo) 'Loop while files are found
                     FindClose(hFileFind) 'Free handle
                   END IF
                   
                   sLog = sLog                                                                  & $CRLF & _
                   "The search includes both the long and short file names."                    & $CRLF & _
                   "cFilename is for the long file name and"                                    & $CRLF & _
                   "cAlternateFileName is for the 8.3 short file name version."                 & $CRLF & _
                   "Important: If the LFN-cFilename could fit in a 8.3 characters pattern then" & $CRLF & _
                   "the SFN-cAlternateFileName will be empty."                                  & $CRLF & _
                   "A search  for ""*.htm"" will return ""x.html"""                             & $CRLF & _
                   "because ""x.html"" 8.3 SFN version will be"                                 & $CRLF & _
                   "something like ""blabbla~1.htm"""                                           & $CRLF & _
                   "Remember, the search includes the long and short file names."               & $CRLF & _
                   "PathMatchSpec or other method could help to get the wanted result. :-)"
                   
                   MessageBox(%HWND_DESKTOP, BYVAL STRPTR(sLog), "FindFirstFile - PathMatchSpec", %MB_OK OR %MB_TOPMOST)
                   
                   IF MessageBox(%HWND_DESKTOP, "Erase files?", "Cleanup", %MB_YESNO OR %MB_TOPMOST OR %MB_ICONQUESTION) = %IDYES THEN
                     KILL sPath & "*"
                     RMDIR sPath
                   END IF
                   
                  END FUNCTION
                  '______________________________________________________________________________
                  '
                  Last edited by Pierre Bellisle; 29 May 2016, 07:44 PM.

                  Comment


                  • #29
                    > Remember, the search includes the long and short file names.

                    Thank you, that never crystallized before.

                    "Not my circus, not my monkeys."

                    Comment


                    • #30
                      Yes,
                      It is pretty unnatural...
                      I guess Microsoft did not have much choice when they went
                      from short to long filename to keep backward compatibility.
                      Haunted by the past. ;-)

                      Pierre

                      Comment


                      • #31
                        I also came up with this:

                        Code:
                        FUNCTION MYDIR(OPT BYVAL FCR AS STRING) AS STRING
                        
                        LOCAL RESULT AS STRING
                        STATIC CRT   AS STRING
                        STATIC LP    AS STRING
                        
                        IF VARPTR(FCR) THEN
                            CRT    = TRIM$(FCR)
                            LP     = LCASE$(parse$(CRT, "*", parsecount(CRT, "*")))
                            RESULT = DIR$(FCR)
                        ELSE
                            RESULT = DIR$
                        END IF
                        
                        IF RIGHT$(CRT, 1) <> "*" THEN
                           DO WHILE LCASE$(right$(RESULT, LEN(LP))) <> LP
                              RESULT = DIR$  
                           LOOP
                        END IF
                        
                        function = RESULT
                        
                        END FUNCTION
                        
                        
                        FUNCTION PBMAIN() AS LONG
                        
                        LOCAL FNM AS STRING
                        
                        FNM = MYDIR("*.htm")
                        
                        DO WHILE LEN(FNM)
                            ? FNM
                            FNM = MYDIR()
                        LOOP
                        
                        END FUNCTION
                        It will find, for example: "somepage.htm" but it will exclude: "anotherpage.html"

                        But i dont know if it is thread-safe.
                        www.pluribasic.com

                        Comment


                        • #32
                          > But i dont know if it is thread-safe.

                          Anything that uses static or glolbal variables is not thread-safe.
                          Forum: http://www.jose.it-berater.org/smfforum/index.php

                          Comment


                          • #33
                            It will find, for example: "somepage.htm" but it will exclude: "anotherpage.html"
                            Code:
                            FUNCTION PBMAIN () AS LONG
                            
                            LOCAL PATH, file AS STRING
                            
                            PATH = "d:\test\"
                            file = DIR$(PATH & "*.htm")
                            WHILE LEN(file)
                                IF RIGHT$(LCASE$(file),3) = "htm" THEN _
                                ? file
                                file = DIR$
                            WEND
                            
                            WAITKEY$
                            END FUNCTION

                            Comment


                            • #34
                              Good Horst, except this one works only for htm and also returns files with extension, for example: .htmhtm :P

                              The other one works for exact matches of ANY extension. The idea is the same though.
                              Last edited by Brian Alvarez; 30 May 2016, 02:31 PM.
                              www.pluribasic.com

                              Comment


                              • #35
                                Searching the forums for info related to DirData structures, I noticed that there are bunch of threads in which users wrote about updating the DIR$ Help so that it shows how to convert the Low and High to a filesize, and how to convert the QUAD timestamps to displayable text... (many users offered MANY alternatives, and I haven't yet tested them all or determined which is "best"...)

                                I mention these issues now as a placeholder, and will attempt to re-locate those threads/URLs to be added to this message at a later time... Or, the Help editors can do a forum search on those various keywords...

                                -John

                                Comment


                                • #36
                                  No need to add more links to show how to use the values for filesize and CreationTime, LastWriteTime and LastAccessTime members of the DIRDATA UDT, the arithmetic is pretty straightforward.

                                  Filesize as a PB QUAD integer:

                                  Code:
                                  FIlesize&& =  MAK (QUAD, DirData.Filesizelow, DirData.FileSizeHigh)

                                  If the file is less than 2 GB in size, you may use DirData.FilesizeLow as a LONG integer. Dirdata.FilesizeHigh will be zero in this case.

                                  QUAD Timestamps as Character Strings

                                  All three timestamps in the DirData UDT are in the form of a Windows' FILETIME structure. These may be converted to more familiar forms of date-time character strings using EITHER:
                                  • The Windows' "FiletimeToSystemTime()," "GetDateFormat()" and "GetTimeFormat() " API functions
                                  • The PowerBASIC "PowerTime" object; set the QUAD 'value of interest' from DirData as the value of the Filetime property of the PowerTime Interface object and use other properties and methods to format the output string. .



                                  Both these options support multiple character formats for datetime value . Refer to the appropriate reference (MSDN or PB Help) for details.

                                  General

                                  The structure of the PowerBASIC "DirData" UDT is identical to the structure of the Windows' WIN32_FIND_DATA structure. However, the data types used to define the Date-Time members are different in the PB-Supplied WInAPI Header files ("AS FILETIME") than in the PB DirData UDT ("AS QUAD"). This allows PB programs to use the DirData UDT without the need to #INCLUDE any Windows' headers files..
                                  Michael Mattias
                                  Tal Systems Inc.
                                  Racine WI USA
                                  mmattias@talsystems.com
                                  http://www.talsystems.com

                                  Comment


                                  • #37
                                    Nice summary!

                                    Yes, your spec covers the essence of the discussions. Saves me from a lot of searching, copying, and pasting.

                                    Thanks,
                                    -John

                                    Comment

                                    Working...
                                    X