1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Regex & conditionals

Discussion in 'Support' started by Ville, Jun 12, 2011.

  1. Ville

    Joined:
    Jul 7, 2008
    Messages:
    35
    Likes Received:
    0
    I'm having tough time getting regular expression matching working with IFF.

    Here's the current test:

    Code:
    @echo off
    DO I in (.) /A:D *
     iff isdir %[I] then
        pushd %[I]
        DO F in /S *
           iff isfile %[F] then
              echo file being tested: "%[F]"
              iff ("%[F]" =~ ".+\.pdf$") then
                 echo *********PDF: %[F]
              endiff 
           endiff
        ENDDO
        popd
     endiff
    ENDDO
    
    The iff in the middle doesn't ever match the filenames with ".pdf" as the extension even though there are many such files in the subdirs being tested. The files print out fine with the line above the iff, but match never occurs, so I'm guessing there is something wrong with the way I'm using the regex. The conditional expressions page in the TCC help doesn't give examples of regex use, however, so I'm not sure what to try next.

    The goal is to eventually create a script that iterates through the folders on the top level and flag and move to a different location any top level folder [and all of it's contents] if one or more PDF files exist anywhere under it.

    Thanks for any advice!
     
  2. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,523
    Likes Received:
    4
    Ville wrote:
    ...
    | The goal is to create a script that iterates through the folders on
    | the top level and flag and move to a different location any top level
    | folder [and all of it's contents] if one or more PDF files exist
    | anywhere under it.

    I did not analyze your code, but a much simpler method to achieve your is
    the use of @FILES function. Something like this ought to work (UNTESTED):

    @echo off
    setlocal
    do I in /a:d *
    iff %@files[/s %i\*.pdf,-d] gt 0 then
    ... do something with directory %I
    endiff
    enddo

    Just remember, if whatever you do with a directory %I creates a new match in
    the DO which found it originally, its new name may be processed again.

    General notes, not relevant with the method I suggested.
    - *.PDF located all files with the PDF extension (not case sensitive), no
    need for regular expressions
    - the @EXT[] function allows you to check what a file's extension, again
    without resorting to
    regular expressions

    --
    HTH, Steve
     
  3. Ville

    Joined:
    Jul 7, 2008
    Messages:
    35
    Likes Received:
    0
    Hi Steve,

    Thanks for that info.. it's working perfectly! This will allow me to complete the script.

    --

    For future reference.. if someone would have an example of regex use with IFF it would be very interesting to see (and it also would make it searchable here as a reference :). Specifically I'm wondering about the use of the "=~" operator.

    Thanks!
     
  4. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,523
    Likes Received:
    4
    Ville wrote:
    | Hi Steve,
    |
    | Thanks for that info.. it's working perfectly! This will allow me to
    | complete the script.

    Glad to hear it.
    --
    Steve
     
  5. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,784
    Likes Received:
    29
    On Sun, 12 Jun 2011 20:50:45 -0400, Ville <> wrote:

    |I'm having tough time getting regular expression matching working with IFF.

    Something seems amiss with conditional expressions involving =~. I'm using the
    PERL syntax.

    Code:
    v:\> if d =~ e echo yes
    yes
    
    v:\> if "dog" =~ "cat" echo yes
    yes
    
    v:\> if "dog" =~ ".*cat.*" echo yes
    yes
     
  6. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,784
    Likes Received:
    29
    On Sun, 12 Jun 2011 20:50:45 -0400, Ville <> wrote:

    |iff ("%[F]" =~ ".+\.pdf$") then

    And seems as if you cannot parenthesize an entire conditional expression. IF
    may choke on it or give a wrong result.

    Code:
    v:\> if ( 1 == 1 ) echo yes
    
    v:\> if ( 1 != 1 ) echo yes
    
    v:\> if ( d =~ e ) echo yes
    Usage : IF [/I] [NOT] condition [.AND. | .OR. | .XOR. [NOT] condition ...]
    command
    
    v:\>
    And IFF gives bad results but doesn't choke as above.

    Code:
    v:\> iff ( 1 == 1 ) then & echo yes & endiff
    
    v:\> iff ( 1 != 1 ) then & echo yes & endiff
    
    v:\> iff ( d =~ d ) then & echo yes & endiff
    
    v:\>
     
  7. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,523
    Likes Received:
    4
    vefatica wrote:
    | On Sun, 12 Jun 2011 20:50:45 -0400, Ville <> wrote:
    |
    || iff ("%[F]" =~ ".+\.pdf$") then
    |
    | And seems as if you cannot parenthesize an entire conditional
    | expression.

    The syntax is not that of C/C++. Once you enclose a conditional expression
    in parentheses as above
    it is no longer a conditional expression whose value can ever be TRUE.
    That's been the syntax since 4DOS.


    | IF may choke on it or give a wrong result.
    |
    |
    | Code:
    | v:\> if ( 1 == 1 ) echo yes
    |
    | v:\> if ( 1 != 1 ) echo yes

    No wrong results. Neither "( 1 == 1 )" nor "( 1 == 1 )" is a conditional
    expression with value TRUE.

    |
    | v:\> if ( d =~ e ) echo yes
    | Usage : IF [/I] [NOT] condition [.AND. | .OR. | .XOR. [NOT] condition
    | ...]
    | command

    Invalid syntax properly detected.

    | And IFF gives bad results but doesn't choke as above.
    |
    |
    | Code:
    | v:\> iff ( 1 == 1 ) then & echo yes & endiff
    |
    | v:\> iff ( 1 != 1 ) then & echo yes & endiff
    |
    | v:\> iff ( d =~ d ) then & echo yes & endiff

    No conditional expressions with value TRUE above. C that this in not C!
    Conditional expression syntax is closer to BASIC and Fortran than C - that's
    how PC-DOS 3's COMMAND.COM worked.
    --
    Steve
     
  8. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,784
    Likes Received:
    29
    Perentheses are necessary for grouping in conditional expressions. I assumed
    (incorrectly if you're right) that it'd be OK to enclose the whole thing. They
    are otherwise handled correctly. I wasn't thinking of "C" but of mathematics
    and Boolean algebra in general.

    On Sun, 12 Jun 2011 23:35:25 -0400, Steve Fabian <> wrote:

    |vefatica wrote:
    || On Sun, 12 Jun 2011 20:50:45 -0400, Ville <> wrote:
    ||
    ||| iff ("%[F]" =~ ".+\.pdf$") then
    ||
    || And seems as if you cannot parenthesize an entire conditional
    || expression.
    |
    |The syntax is not that of C/C++. Once you enclose a conditional expression
    |in parentheses as above
    |it is no longer a conditional expression whose value can ever be TRUE.
    |That's been the syntax since 4DOS.
    |
    |
    || IF may choke on it or give a wrong result.
    ||
    ||
    || Code:
    || v:\> if ( 1 == 1 ) echo yes
    ||
    || v:\> if ( 1 != 1 ) echo yes
    |
    |No wrong results. Neither "( 1 == 1 )" nor "( 1 == 1 )" is a conditional
    |expression with value TRUE.
    |
    ||
    || v:\> if ( d =~ e ) echo yes
    || Usage : IF [/I] [NOT] condition [.AND. | .OR. | .XOR. [NOT] condition
    || ...]
    || command
    |
    |Invalid syntax properly detected.
    |
    || And IFF gives bad results but doesn't choke as above.
    ||
    ||
    || Code:
    || v:\> iff ( 1 == 1 ) then & echo yes & endiff
    ||
    || v:\> iff ( 1 != 1 ) then & echo yes & endiff
    ||
    || v:\> iff ( d =~ d ) then & echo yes & endiff
    |
    |No conditional expressions with value TRUE above. C that this in not C!
    |Conditional expression syntax is closer to BASIC and Fortran than C - that's
    |how PC-DOS 3's COMMAND.COM worked.
     
  9. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,784
    Likes Received:
    29
    Regexes in conditional expressions are better.

    If they are not working as intended ....

    Code:
    v:\> if ed =~ d*  echo yes
    yes
    then I'd recommend the help be polished a little. Instead of "string1matches the regular expression in string2" better might be "the regular expression in string2 is matched in string1". If a user wants an exact match he can put the BOL and EOL anchors in the regex.

    P.S. After pasting from the help, I couldn't get rid of subsequent italics.
     
  10. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,784
    Likes Received:
    29
    TYPO: I meant to type "If they are now working ...".

     

Share This Page