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

How to? dir /s unexpected results

Discussion in 'Support' started by rps, Nov 29, 2014.

  1. rps

    rps

    Joined:
    Jul 6, 2008
    Messages:
    202
    Likes Received:
    3
    Would someone please tell me what is happening here:
    Code:
    C:\JPSoft>*dir /s /b test1* || (echo. & echo there were %_dir_files files displayed and %_dir_errors dir errors)
    C:\JPSoft\test1.bak
    C:\JPSoft\test1.btm
    C:\JPSoft\test1.zip
    C:\JPSoft\TCMD15x64\logs\test1.txt
    
    there were 4 files displayed and 0 dir errors
    
    C:\JPSoft>*dir /b test1* || (echo. & echo there were %_dir_files files displayed and %_dir_errors dir errors)
    test1.bak
    test1.btm
    test1.zip
    
    
    I do not understand an errorlevel=2 when using the /s option. This is also true when using pdir.
    Code:
    C:\JPSoft>*dir /b /s test1* & (echo. & echo errorlevel === %errorlevel)
    C:\JPSoft\test1.bak
    C:\JPSoft\test1.btm
    C:\JPSoft\test1.zip
    C:\JPSoft\TCMD15x64\logs\test1.txt
    
    errorlevel === 2
    
    C:\JPSoft>*dir /b test1* & (echo. & echo errorlevel === %errorlevel)
    test1.bak
    test1.btm
    test1.zip
    
    errorlevel === 0
    
    C:\JPSoft>*pdir /b test1* & (echo. & echo errorlevel === %errorlevel)
    test1.bak
    test1.btm
    test1.zip
    
    errorlevel === 0
    
    C:\JPSoft>*pdir /b /s test1* & (echo. & echo errorlevel === %errorlevel)
    C:\JPSoft\test1.bak
    C:\JPSoft\test1.btm
    C:\JPSoft\test1.zip
    C:\JPSoft\TCMD15x64\logs\test1.txt
    
    errorlevel === 2
    
    
     
  2. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,954
    Likes Received:
    30
    I'd guess that not finding test1* in some subdirectory set ERRORLEVEL. I don't know if that's appropriate behavior.
     
  3. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,860
    Likes Received:
    83
  4. rps

    rps

    Joined:
    Jul 6, 2008
    Messages:
    202
    Likes Received:
    3
    Huh, when I used CMD the results were as expected with errorlevel=0.
    I just didn't include the CMD output with the OP.

    Code:
    c:\JPSoft>ver
    
    Microsoft Windows [Version 6.1.7601]
    
    c:\JPSoft>dir /b test1* & (echo. & echo errorlevel === %errorlevel%)
    test1.bak
    test1.btm
    test1.zip
    
    errorlevel === 0
    
    c:\JPSoft>
    c:\JPSoft>dir /b /s test1* & (echo. & echo errorlevel === %errorlevel%)
    c:\JPSoft\test1.bak
    c:\JPSoft\test1.btm
    c:\JPSoft\test1.zip
    c:\JPSoft\TCMD15x64\logs\test1.txt
    
    errorlevel === 0
    
    c:\JPSoft>dir /b /s test1* && (echo. & echo errorlevel === %errorlevel%)
    c:\JPSoft\test1.bak
    c:\JPSoft\test1.btm
    c:\JPSoft\test1.zip
    c:\JPSoft\TCMD15x64\logs\test1.txt
    
    errorlevel === 0
    
    c:\JPSoft>dir /b /s test1* || (echo. & echo errorlevel === %errorlevel%)
    c:\JPSoft\test1.bak
    c:\JPSoft\test1.btm
    c:\JPSoft\test1.zip
    c:\JPSoft\TCMD15x64\logs\test1.txt
    
    c:\JPSoft>
    Well, I guess I could just use the internal variable "%_dir_errors" to verify any actual errors.
     
  5. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,860
    Likes Received:
    83
    Your example is not proving what you think it is ...

    CMD expands all the variables on the command line, including those following a compound command or command separator, before executing anything. So what you're actually executing is this:

    dir /b test* & (echo. & echo errorlevel == 0)

    TCC delays expansion of the variables following a command separator or compound command. I doubt that you want me to duplicate CMD's behavior of pre-evaluating the variables!
     
  6. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,954
    Likes Received:
    30
    CMD returns 0 anyway.
    Code:
    v:\> ver
    
    Microsoft Windows [Version 6.1.7601]
    
    v:\> dir /b /a:d
    empty
    InternetRegistries
    mtest
    stream
    tcsh
    words
    
    v:\> dir /s /b ø*
    v:\ø.txt
    
    v:\> echo %ERRORLEVEL%
    0
     
  7. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,860
    Likes Received:
    83
    Nope:

    Code:
    D:\TakeCommand17\dll>ver
    Microsoft Windows [Version 6.3.9600]
    
    D:\TakeCommand17\dll>dir  /s wockawocka
     Volume in drive D is Development
     Volume Serial Number is 581D-31AD
    
     Directory of D:\TakeCommand17\dll
    
    File Not Found
    
    D:\TakeCommand17\dll>echo %errorlevel%
    1
    
     
  8. rps

    rps

    Joined:
    Jul 6, 2008
    Messages:
    202
    Likes Received:
    3
    Rex -- Thanks for the CMD clarification.
    Replicating that silly behavior would not be helpful. :joyful:

    I am still confused about when the DIR internal variable "%_dir_errors " is set to something other than 0.
    The following are a few examples I tried.
    Code:
    C:\JPSoft>ver
    
    TCC  17.00.59 x64   Windows 7 [Version 6.1.7601]
    
    
    C:\JPSoft>*dir /s /b test1* & echo. & echo there were %_dir_files files displayed and %_dir_errors dir errors & echo errorlevel ===
    %errorlevel
    C:\JPSoft\test1.bak
    C:\JPSoft\test1.btm
    C:\JPSoft\test1.zip
    C:\JPSoft\TCMD15x64\logs\test1.txt
    
    there were 4 files displayed and 0 dir errors
    errorlevel === 2
    
    C:\JPSoft>*dir /b test1* & echo. & echo there were %_dir_files files displayed and %_dir_errors dir errors & echo errorlevel === %e
    rrorlevel
    test1.bak
    test1.btm
    test1.zip
    
    there were 3 files displayed and 0 dir errors
    errorlevel === 0
    
    C:\JPSoft>*dir /b test8* & echo. & echo there were %_dir_files files displayed and %_dir_errors dir errors & echo errorlevel === %errorlevel
    TCC: (Sys) The system cannot find the file specified.
    "C:\JPSoft\test8*"
    
    there were 0 files displayed and 0 dir errors
    errorlevel === 2
    
    Can someone provide an example of how the "%_dir_errors " variable actually works?
     
  9. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,954
    Likes Received:
    30
    rps's original post used /S and /B and a wildcard spec that was found in some but not all subdirectories. That's where TCC and CMD differ. Compare:
    Code:
    v:\> ver
    
    TCC  17.00.59  Windows 7 [Version 6.1.7601]
    
    v:\> dir /s /b v:\ø*
    V:\ø.txt
    
    v:\> echo %errorlevel%
    2
    Code:
    v:\> ver
    
    Microsoft Windows [Version 6.1.7601]
    
    v:\> dir /s /b v:\ø*
    v:\ø.txt
    
    v:\> echo %errorlevel%
    0
     
  10. rconn

    rconn Administrator
    Staff Member

    Joined:
    May 14, 2008
    Messages:
    9,860
    Likes Received:
    83
    Not reproducible here.

    Code:
    [C:\D_Drive\TakeCommand17\tcc]ver
    
    TCC  17.00.62 x64   Windows 10 [Version 6.4.9879]
    
    [C:\D_Drive\TakeCommand17\tcc]dir /s /b zz.new
    C:\D_Drive\TakeCommand17\tcc\zz.new
    
    [C:\D_Drive\TakeCommand17\tcc]echo %errorlevel
    0
    
     
  11. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,954
    Likes Received:
    30
    That behavior changed in some build after 59.
     

Share This Page