How to? dir /s unexpected results

rps

Jul 6, 2008
436
6
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
 
May 20, 2008
11,411
99
Syracuse, NY, USA
I'd guess that not finding test1* in some subdirectory set ERRORLEVEL. I don't know if that's appropriate behavior.
 

rps

Jul 6, 2008
436
6
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
12,356
150
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!
 
May 20, 2008
11,411
99
Syracuse, NY, USA
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
 

rps

Jul 6, 2008
436
6
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?
 
May 20, 2008
11,411
99
Syracuse, NY, USA
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
12,356
150
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
 
May 20, 2008
11,411
99
Syracuse, NY, USA
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.
 
Similar threads
Thread starter Title Forum Replies Date
E Fixed Bug with DIR /Z displaying descriptions Support 8
J Paths shown in DIR /B Support 2
K Fixed Prompt display will be shifted after use dir to display a filename with Chinese. (v25.00.28 x64) Support 18
Jesse Heines How to? How to display picture creation date with dir command Support 6
vefatica WAD DIR.BTM? Support 11
DrusTheAxe DIR reports meaningless SYMLINK information Support 14
C show file description? with dir? Support 8
vefatica DIR /F and streams? Support 7
rps Multi-column DIR /v not displaying all files. Support 5
R How to? Dir specific file search patterns with spaces in the pathnames? Support 6
rps Dir /Nfv -> Alt-F2 Support 2
rps @FILESIZE[....,a] allocated size not matching Dir results Support 8
A TCMD - Dir Command puts out blank lines? Support 16
S Problems with dir command in the debugger Support 5
M TCC incorrect dir output since Windows 1803 Support 6
x13 Problem listing repository files using DIR http(s)://... Support 8
cxxl dir /s works in mysterious ways :( Support 4
vefatica Help nit (FFIND and DIR with /S) Support 0
N Fixed Strange dir behavior Support 6
JohnQSmith Weird DIR output (missing lines) Support 1
C 7zip with date range .vs. filelist created with dir and daterange Support 0
D Towards shared (dir-)history lists Support 3
vefatica WAD DIR /HL still gets names wrong Support 16
vefatica DIR /S /HL? Support 4
H Fixed DIR /G returns wrong sizes Support 2
nickles WAD dir.htm Support 2
vefatica DO dir in /s /a:+d /d"g:\" * ( ... ) Support 26
vefatica DIR \\.\...? Support 4
M Fixed DIR /S /B1 ignores "/S" Support 5
C tcmd.ini not loading from program dir? Support 5
D Fixed Dir /Nm:n has changed Support 1
vefatica Update to current install dir? Support 8
cgunhouse Problem with "dir /=" Support 4
T dir /h error in empty directory Support 22
P WAD TC 15.0.1.58 x64 crasches with a simple dir command Support 18
CWBillow dir /4 strange Support 2
samintz WAD DIR /B1 and /X Support 2
nickles dir behaves inconsistently Support 5
vefatica DIR, streams, and wildcards? Support 1
vefatica DIR /: /u ... streams not counted? Support 7
vefatica Documentation DIR /B /S /: Support 2
samintz How to? DIR listing for exact match Support 1
dcantor WAD dir "ftp:// ..." fails in TCC 15 Support 7
T How to? dir/pdir - 2nd level down only Support 7
MikeBaas How to? DIR: supress extensions? Support 5
old coot dir /s dies on my C: drive Support 2
A WAD Dir daterange + multiple path wildcards crashes tcc Support 2
old coot TC DIR command has trouble on my SSD Support 2
M Fixed character set in dir/copy Support 3
C odd behavior of "dir" Support 0

Similar threads