How to? dir /s unexpected results

  • This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
Jul 6, 2008
238
3
#1
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
 
Jul 6, 2008
238
3
#4
WAD - for compatibility with CMD.
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.
 

rconn

Administrator
Staff member
May 14, 2008
10,096
85
#5
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
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
 
Jul 6, 2008
238
3
#8
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
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
 

rconn

Administrator
Staff member
May 14, 2008
10,096
85
#10
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:
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
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
That behavior changed in some build after 59.