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

  • This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.
#1
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
10,100
85
#3
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".
 
#4
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.