What am I doing wrong in this (very simple!) code?

May 24, 2010
855
0
Northlake, Il
Best to show by examples:
Code:
   Mon  Jan 7, 2013   5:37:37a

TCC  14.03.53 x64   Windows 7 [Version 6.1.7601]
Copyright 2012 JP Software Inc.  All Rights Reserved
Registered to ****** *******

[Z:\]*Dir /K /M Lev*
 1/07/2013   4:43         <DIR>    Level 1A
 1/07/2013   4:44         <DIR>    Level 1B

[Z:\]*Dir /K /M Dir*
 1/06/2013  19:35         <DIR>    Dirs

[Z:\]Type Demo.btm
SetLocal
PDir /AD "Level 1A\Level*"
Do TopDir In /P PDir /AD "Level 1A\Level*"
   @Echo %%TopDir
EndDo
EndLocal

[Z:\]Demo
PDir /AD "Level 1A\Level*"
 1/07/2013   4:43         <DIR>    Level 2A
 1/07/2013   4:43         <DIR>    Level 2B
Do TopDir In /P PDir /AD "Level 1A\Level*"
%TopDir
%TopDir

[Z:\]*Dir /K /M Lev*
 1/07/2013   4:43         <DIR>    Level 1A
 1/07/2013   4:44         <DIR>    Level 1B

[Z:\]*Dir /K /M Dir*
 1/06/2013  19:35         <DIR>    Dirs
Before running the batch file I look for file system objects whose names begin with "Lev" and there are two directories by that name. There's also a single directory named "Dirs". After running the batch file nothing has changed.

And I get "%TopDir" echoed two times, which makes sense because the directory "Level1A" contains two subdirectories, one named "Level 2A" and one named "Level 2B".
Code:
[Z:\]Type Demo.btm
SetLocal
PDir /AD "Level 1A\Level*"
Do TopDir In /P PDir /AD "Level 1A\Level*"
   @Echo %TopDir
EndDo
EndLocal

[Z:\]Demo
PDir /AD "Level 1A\Level*"
 1/07/2013   4:43         <DIR>    Level 2A
 1/07/2013   4:43         <DIR>    Level 2B
Do TopDir In /P PDir /AD "Level 1A\Level*"
TCC: (Sys) Z:\Demo.btm [3]  The system cannot find the file specified.
 "Z:\DIR"
TCC: (Sys) Z:\Demo.btm [3]  The file exists.
 "Z:\Level"

[Z:\]*Dir /K /M Lev*
 1/07/2013   4:43         <DIR>    Level 1A
 1/07/2013   4:44         <DIR>    Level 1B
 1/07/2013   5:36               0  Level

[Z:\]*Dir /K /M Dir*
 1/06/2013  19:35         <DIR>    Dirs
I have changed the "@Echo %%TopDir" to "@Echo %TopDir" so that the contents of the "TopDir" variable will by echoed. I make no other changes. And when I run the batch file the first time after making that change I get "can not find the file specified" (for a file named "Dir") and a complaint that the file exists (for a file named simply "Level"). I can not see any references to a file with either name in the batch file. And after running the batch file, a zero-length file named "Level" does indeed exist. Why? How?

So I run the changed version a second time:
Code:
[Z:\]Demo
PDir /AD "Level 1A\Level*"
 1/07/2013   4:43         <DIR>    Level 2A
 1/07/2013   4:43         <DIR>    Level 2B
Do TopDir In /P PDir /AD "Level 1A\Level*"
TCC: (Sys) Z:\Demo.btm [3]  The file exists.
 "Z:\Level"
TCC: (Sys) Z:\Demo.btm [3]  The file exists.
 "Z:\Level"

[Z:\]*Dir /K /M Lev*
 1/07/2013   4:43         <DIR>    Level 1A
 1/07/2013   4:44         <DIR>    Level 1B
 1/07/2013   5:36               0  Level

[Z:\]*Dir /K /M Dir*
 1/06/2013  19:35         <DIR>    Dirs

[Z:\]
This time I get two messages that "Z:\Level" exists (and no more message about a file named "Dir"). And nothing else has changed.

I must admit that I have no clue as to where it is making a reference to a file named "Dir" nor do I understand where it is referencing in any way a file named "Z:\Level".

What on earth am I not seeing?
 

rconn

Administrator
Staff member
May 14, 2008
12,344
149
The problem is with your ECHO statement, not the DO loop or PDIR.

You're trying to execute this:

echo 1/07/2013 4:43 <DIR> Level 1A

and the parser interprets that as a request for a double redirection -- redirecting input from a file named "DIR", and redirecting output to a file called "Level".
 
May 24, 2010
855
0
Northlake, Il
Well, thank you, Rex! All I can say (about myself) is "Duh!". And, due to the power of TCC, there is a fairly simple solution. To illustrate the solution and the results of executing it:
Code:
[Z:\File-System Objects]Type \Demo.btm
@Echo Off
SetLocal
Function IsDir=`%@If[IsDir %1,        -Dir- ,%@Format[14.14,%@Comma[%@FileSize[%1]]]]`
Do TopDir In /P PDir /(dy/m/d  th:m  @IsDir[*]  fn) *
   @Echo %TopDir
EndDo
UnFunction IsDir
EndLocal

[Z:\File-System Objects]\Demo.btm
2013/01/07  22:17           -Dir-   A Directory
2013/01/07  22:17           -Dir-   Another Directory
2013/01/06  15:44        2,414,220  A Large File.dat
2013/01/05  17:27           25,682  A Medium-Sized File.dat
2012/12/30  04:40               61  A Small File.dat

[Z:\File-System Objects]PDir
2013/01/07  22:17           <Dir>   A Directory
2013/01/07  22:17           <Dir>   Another Directory
2013/01/06  15:44        2,414,220  A Large File.dat
2013/01/05  17:27           25,682  A Medium-Sized File.dat
2012/12/30  04:40               61  A Small File.dat

[Z:\File-System Objects]
As you can see, exactly the same result as just "PDir" with no operands except the greater and less than signs are replaced by dashes.
 
Similar threads
Thread starter Title Forum Replies Date
M How to? What am I doing wrong/not understanding? Support 10
M What am I doing wrong? Support 12
M What am I doing wrong here? Support 2
M How to? Missing closing quote: What am I doing wrong???? Support 7
fishman@panix.com V23 cdd /s doing WIERD things. Support 5
P Doing math with TCC Support 11
B when doing SFTP via IFTP to my secure site my users always get the finger print key prompt Support 1
C take command 19 not doing well under windows 10 build 14295 Support 3
fromano Fixed /E not working in TCMD 17.0.40 while doing one way synch Support 1
fromano Fixed TCC crashing inside TCMD 17.0.35 after doing a copy Support 11
M Hotkey-mechanism, for doing something with the current command-line Support 4
M Unexpected Results from Doing Numeric Comparisons... Support 17
vefatica Who's doing the cacheing? Support 6
JohnQSmith New online help file wrong logo Support 7
vefatica @EXECSTR ... TCSTART ... something's wrong! Support 7
R History window wrong size, can't remember size change Support 12
mfarah Fixed The DATE command produces the wrong error message. Support 1
MikeBaas @regsetenv complains "wrong parameter" Support 14
mikea How to? TCC registered to wrong 'entity' Support 5
T @replace -- what is wrong here Support 2
vefatica Fixed KEYSTACK /i with repeat count sends wrong keys Support 7
C ZIP/U compares wrong timestamp Support 13
vefatica @WORDS, often wrong Support 0
vefatica gcd() ... wrong answer, lcm() hangs Support 5
D What is wrong with this code? Support 7
vefatica WAD DIR /HL still gets names wrong Support 16
H Fixed DIR /G returns wrong sizes Support 2
M Another "What is wrong?" Support 8
M What is wrong with this command? Support 4
vefatica Updater says wrong version Support 1
CWBillow Wrong icons keep sticking Support 6
C News link wrong Support 1
C Forum column headings wrong? Support 4
gschizas Installation folder for TCMD x64 15.1 is wrong Support 5
Dan Glynhampton Documentation v15 help: ECHOXERR Help Page Title is Wrong Support 0
vefatica Something wrong with @PID Support 10
J zip wrong central directory signature Support 12
S Fixed _DO_FILES and _DO_DIRS wrong Support 0
B Downloading Updates - Error: Wrong file size?? Support 3
vefatica What's wrong here? Support 2
vefatica Wrong startup file? Support 1
gschizas (minor) Help topic "Colors, Color Names and Codes" is wrong Support 0
T Wrong prompt in Windows command console Support 3
M Where am I going wrong with ALIAS??? Support 7
G Wrong File Size Support 8
vefatica Updating v10: error wrong file size Support 2
S Wrong .INI file used by TCC Support 2
K_Meinhard Wrong sender adress Support 0
vefatica Something got very slow (very recently) Support 1
K_Meinhard Very small nit in german Options dialog Support 1

Similar threads