for %1 %%i in (*.*) do [...] not working

Apr 1, 2010
43
0
Re: not working

This fails in CMD -- what is "SET rpath=%~1" supposed to do? In CMD on my
system (Server 2008), it removes RPATH from the environment, as %~1 AFAIK
doesn't have any meaning. So the subsequent "set rpath=!rpath:%CD%\=!" does
nothing, and "rpath" is empty when it returns to the FOR loop. All I get is
a lot of "echo is off" messages from the FOR loop.

Rex Conn
JP Software

This one should work:
Code:
@echo off
setlocal enabledelayedexpansion
for /R %%i in (.) do (
 call :rpath "%%~i"
 echo !rpath!
)
endlocal
GOTO :EOF

rem sub rpath(filename) OUT: %rpath%
:rpath
 rem debug print %1 here
 echo (rpath: %1)

 SET rpath=%~1
 set rpath=!rpath:%CD%\=!
GOTO :EOF
 
Apr 1, 2010
43
0
Re: not working

This one should work:

and this one match original usage of variables:
Code:
@echo off
setlocal enabledelayedexpansion
for /R %%i in (.) do call :disprpath "%%~i"
endlocal
GOTO :EOF

rem sub disprpath(filename)
:disprpath
 call :rpath "%~1"
 echo %rpath%
GOTO :EOF

rem sub rpath(filename) OUT: %rpath%
:rpath
 rem debug print %1 here
 rem echo (rpath: %1)

 SET rpath=%~1
 set rpath=!rpath:%CD%\=!
GOTO :EOF

And I noticed that TCC changes %CD% when doing recursive FOR loop.
 
Apr 1, 2010
43
0
Re: not working

This fails in CMD -- what is "SET rpath=%~1" supposed to do? In CMD on my
system (Server 2008), it removes RPATH from the environment, as %~1 AFAIK
doesn't have any meaning. So the subsequent "set rpath=!rpath:%CD%\=!" does
nothing, and "rpath" is empty when it returns to the FOR loop. All I get is
a lot of "echo is off" messages from the FOR loop.

Rex Conn
JP Software

SET rpath=%~1 removes any surrounding quotes (") from %1.
 

samintz

Scott Mintz
May 20, 2008
1,503
16
Solon, OH, USA
Re: not working

I did get your code to run in CMD. However, it is much easier to do this
in TCC:

function rpath=.\%%@right[-%@len[%_CWDS],%%@full[%%1]]
do fi in /S /a:d *
echo full: %@full[%fi] rpath: %@rpath[%fi]
enddo

One difference is that the current directory ".\" is not included in the
loop. Only the subdirectories of .\ are included. However, if the intent
is to operate on the files and not just the directories, then removing the
/a:d switch yields exactly what I expect.

Another thing to keep in mind is that as DO recurses into each
subdirectory the CWD changes. So the need for a relative path is
diminished.

-Scott

roytam1 <> wrote on 04/21/2010 12:27:26 AM:


> ---Quote (Originally by rconn)---
> This fails in CMD -- what is "SET rpath=%~1" supposed to do? In CMD on
my

> system (Server 2008), it removes RPATH from the environment, as %~1
AFAIK

> doesn't have any meaning. So the subsequent "set rpath=!rpath:%CD%\=!"
does

> nothing, and "rpath" is empty when it returns to the FOR loop. All I
get is

> a lot of "echo is off" messages from the FOR loop.
>
> Rex Conn
> JP Software
> ---End Quote---
> This one should work:
>
> Code:
> ---------
> @echo off
> setlocal enabledelayedexpansion
> for /R %%i in (.) do (
> call :rpath "%%~i"
> echo !rpath!
> )
> endlocal
> GOTO :EOF
>
> rem sub rpath(filename) OUT: %rpath%
> :rpath
> rem debug print %1 here
> echo (rpath: %1)
>
> SET rpath=%~1
> set rpath=!rpath:%CD%\=!
> GOTO :EOF
> ---------
>
>
>
 

rconn

Administrator
Staff member
May 14, 2008
12,344
149
Re: not working

> ---Quote (Originally by roytam1)---
> This one should work:
> ---End Quote---
> and this one match original usage of variables:
>
> Code:
> ---------
> @echo off
> setlocal enabledelayedexpansion
> for /R %%i in (.) do call :disprpath "%%~i"
> endlocal
> GOTO :EOF
>
> rem sub disprpath(filename)
> :disprpath
> call :rpath "%~1"
> echo %rpath%
> GOTO :EOF
>
> rem sub rpath(filename) OUT: %rpath%
> :rpath
> rem debug print %1 here
> rem echo (rpath: %1)
>
> SET rpath=%~1
> set rpath=!rpath:%CD%\=!
> GOTO :EOF
> ---------

There's two problems with this batch file. The first is the same as the
previous batch file -- there's a character inside the substitution variable
('\') that TCC doesn't like. Though unlike the case of %abc: ...%, I can
add a kludge to TCC to accept it and only look for a closing ! if
DelayedExpansion is set.

The second problem is below ...


> And I noticed that TCC changes %CD% when doing recursive FOR loop.

TCC is right, and CMD is wrong. CMD's behavior is a bug -- the problem
being that CMD is expanding %CD% before executing the FOR, despite (1)
delayed expansion being set, and (2) the %CD% variable being embedded in a
delayed expansion variable. As I said before, I am definitely *not*
changing TCC to expand all variables before execution.

Rex Conn
JP Software
 
Apr 1, 2010
43
0
Re: not working

There's two problems with this batch file. The first is the same as the
previous batch file -- there's a character inside the substitution variable
('\') that TCC doesn't like. Though unlike the case of %abc: ...%, I can
add a kludge to TCC to accept it and only look for a closing ! if
DelayedExpansion is set.

The second problem is below ...




TCC is right, and CMD is wrong. CMD's behavior is a bug -- the problem
being that CMD is expanding %CD% before executing the FOR, despite (1)
delayed expansion being set, and (2) the %CD% variable being embedded in a
delayed expansion variable. As I said before, I am definitely *not*
changing TCC to expand all variables before execution.

Rex Conn
JP Software

For the second problem, even if I use !CD! instead of %CD%, CMD won't change %CD% variable to "current directory where recursive FOR loop goes". Because CMD does not "Change Directory" in recursive FOR loop, but lookup files in subdirectories in "Current Directory" where "FOR /R" runs.

a CMD script that emulating equivalent behavior in TCC will look like this:
Code:
@echo off
setlocal enabledelayedexpansion
for /R %%i in (.) do call :disprpath "%%~i"
endlocal
GOTO :EOF

rem sub disprpath(filename)
:disprpath
 cd "%~1"
 call :rpath "%~1"
 echo %rpath%
GOTO :EOF

rem sub rpath(filename) OUT: %rpath%
:rpath
 rem debug print %1 here
 rem echo (rpath: %1)
 echo (CD: %CD%)

 SET rpath=%~1
 set rpath=!rpath:%CD%\=!
GOTO :EOF

but this one can be workaround for TCC by setting another variable to %CD% before recursive FOR loop and use it instead of %CD%.
 

rconn

Administrator
Staff member
May 14, 2008
12,344
149
Re: not working

> For the second problem, even if I use !CD! instead of %CD%, CMD won't
> change %CD% variable to "current directory where recursive FOR loop
> goes". Because CMD does not "Change Directory" in recursive FOR loop,
> but lookup files in subdirectories in "Current Directory" where "FOR
> /R" runs.

No, that's not what CMD does. From the CMD help:

---------
FOR /R
Walks the directory tree rooted at [drive:]path, executing the FOR
statement in each directory of the tree.
---------

There is no way to execute the FOR statement without changing the current
directory. (And monitoring CMD shows it doing exactly that.)

Rex Conn
JP Software
 

rconn

Administrator
Staff member
May 14, 2008
12,344
149
Re: not working

Also CMD style escape char fails in delayed variables (hellohtml.bat).
[ script content and results were pasted in pastebin.ca ( http://pastebin.ca/1868846 ) because forum kills html tags. ]

This has nothing to do with escape characters or delayed variables. (The escape characters are being stripped in the SET command in both CMD and TCC.)

What's happening is that in CMD it is doing the redirection before the delayed expansion; in TCC the redirection occurs after variable expansion. This is a TCC feature (allowing you to define redirection in your variables), and will definitely not be changed.
 
Apr 1, 2010
43
0
Re: not working

This has nothing to do with escape characters or delayed variables. (The escape characters are being stripped in the SET command in both CMD and TCC.)

What's happening is that in CMD it is doing the redirection before the delayed expansion; in TCC the redirection occurs after variable expansion. This is a TCC feature (allowing you to define redirection in your variables), and will definitely not be changed.

quote from http://ss64.com/nt/setlocal.html :
With delayed expansion the caret ^ escapes each special character all the time, not just for one command.
This makes it possible to work with HTML and XML formatted strings in a variable.
 
Apr 1, 2010
43
0
Re: not working

No, that's not what CMD does. From the CMD help:

---------
FOR /R
Walks the directory tree rooted at [drive:]path, executing the FOR
statement in each directory of the tree.
---------

There is no way to execute the FOR statement without changing the current
directory. (And monitoring CMD shows it doing exactly that.)

Rex Conn
JP Software

but with this script, it proves CMD not only have %CD% unchanged, but really not changing "current directory" by CD/CHDIR:
Code:
@echo off
setlocal enabledelayedexpansion
for /R %%i in (.) do call :disprpath "%%~i"
endlocal
GOTO :EOF

rem sub disprpath(filename)
:disprpath
 cd
 call :rpath "%~1"
 echo rpath = %rpath%
GOTO :EOF

rem sub rpath(filename) OUT: %rpath%
:rpath
 rem debug print %1 here
 echo (rpath: %1)
 echo (CD in rpath: %CD%)

 SET rpath=%~1
 set rpath=!rpath:%CD%\=!
GOTO :EOF

CMD:
H:\test>forx.bat
H:\test
(rpath: "H:\test\.")
(CD in rpath: H:\test)
rpath = .
H:\test
(rpath: "H:\test\dir1\.")
(CD in rpath: H:\test)
rpath = dir1\.
H:\test
(rpath: "H:\test\dir1\dir1-1\.")
(CD in rpath: H:\test)
rpath = dir1\dir1-1\.
H:\test
(rpath: "H:\test\dir2\.")
(CD in rpath: H:\test)
rpath = dir2\.

TCC:
[H:\test]forx.bat
H:\test
(rpath: "H:\test\.")
(CD in rpath: H:\test)
rpath = .
H:\test\dir1
(rpath: "H:\test\dir1\.")
(CD in rpath: H:\test\dir1)
rpath = .
H:\test\dir1\dir1-1
(rpath: "H:\test\dir1\dir1-1\.")
(CD in rpath: H:\test\dir1\dir1-1)
rpath = .
H:\test\dir2
(rpath: "H:\test\dir2\.")
(CD in rpath: H:\test\dir2)
rpath = .
 
Apr 1, 2010
43
0
Re: not working

This has nothing to do with escape characters or delayed variables. (The escape characters are being stripped in the SET command in both CMD and TCC.)

What's happening is that in CMD it is doing the redirection before the delayed expansion; in TCC the redirection occurs after variable expansion. This is a TCC feature (allowing you to define redirection in your variables), and will definitely not be changed.

I'll agree with you for TCC style variables. *BUT* not with CMD's !var! style delayed variables.
 

rconn

Administrator
Staff member
May 14, 2008
12,344
149
Re: not working

> ---Quote (Originally by rconn)---
> This has nothing to do with escape characters or delayed variables.
> (The escape characters are being stripped in the SET command in both
> CMD and TCC.)
>
> What's happening is that in CMD it is doing the redirection before the
> delayed expansion; in TCC the redirection occurs after variable
> expansion. This is a TCC feature (allowing you to define redirection
> in your variables), and will definitely not be changed.
> ---End Quote---
> I'll agree with you for TCC style variables. *BUT* not with CMD's !var!
> style delayed variables.

The delayed expansion isn't relevant.

I have no intention of duplicating this tortured syntax, because:

1) It's undocumented by Microsoft (and IMO is more likely to be a bug than a
feature)

2) It's exceptionally goofy

3) It's completely useless, as there's a simpler, documented, and actually
used syntax that works in both CMD and TCC:

Set _html=^^^<title^^^>Hello world ^^^</title^^^>
Echo %_html%

TCC also has an even simpler solution:

Echo `<title>Hello world </title>`

Rex Conn
JP Software
 

rconn

Administrator
Staff member
May 14, 2008
12,344
149
Re: not working

> ---Quote (Originally by rconn)---
> This has nothing to do with escape characters or delayed variables.
> (The escape characters are being stripped in the SET command in both
> CMD and TCC.)
>
> What's happening is that in CMD it is doing the redirection before the
> delayed expansion; in TCC the redirection occurs after variable
> expansion. This is a TCC feature (allowing you to define redirection
> in your variables), and will definitely not be changed.
> ---End Quote---
> quote from http://ss64.com/nt/setlocal.html :
> With delayed expansion the caret ^ escapes each special character all
> the time, not just for one command.
> This makes it possible to work with HTML and XML formatted strings in a
> variable.

Already possibly (more easily); see my other post.

Rex Conn
JP Software
 
Similar threads
Thread starter Title Forum Replies Date
twaterloo FolderMonitor for the 1st time not working Support 5
Tim Rowe NTFS Descriptions not working Support 13
J Batcomp still not working for me in TCMD27 Support 1
M Oracle batch OPatch.bat.bat not working in TCC but correctly in CMD Support 5
M Why did BEEP just quit working? Support 12
cgunhouse SETLOCAL not working in BDEBUGGER Support 9
nickles ANSI Colors Windows 7 no longer working Support 6
K ANSI Not Working Support 8
thorntonpg option /u not working The directory name is invalid Support 5
Charles Dye Is the "File Associations" installer dialog working? Support 4
H @ceiling @floor @int not working version 21.01.63 Support 9
K Command Line Selection Not Working Support 12
G Extended directory search not working Support 4
B %@fileseek is not working ? Support 2
C "Take Command has stopped working" Support 1
H New command not working well in Windows 7 Support 4
C "TakeCommand has stopped working" Support 3
N ping DNS lookup not working Support 1
N Updater/silentall not working? Support 1
K Help Not Working Support 11
R Fixed set /r has stopped working in build 35 Support 4
U Keys and IDX not working Support 11
Jesse Heines How to? Everything Tool Not Working After Version 20 Installation Support 8
WavSlave Fixed LIST /U not working in TCC v19 Support 5
Jay Sage %_selected no longer working Support 15
S iftp copying pgp files from ftp is not working Support 11
fishman@panix.com Extended Directory Searches are not working for me in CDD Support 9
J How to set working directory Support 10
vefatica Fixed LEAVE N not working Support 7
P %date5 not working Support 6
M WAD PDir not working correctly... Support 5
Detlef Fixed ParameterChar in TCMD.INI no longer working Support 35
Jay Sage WAD Variable _LOGFILE Not Working Support 2
I v16 - Shortcuts not working Support 6
rps Fixed Win "set /p" not working in V17 build 53 Support 3
Jay Sage Toolbar Update to INI File Not Working Support 10
Jay Sage Ctrl-C Not Working Reliably to Copy Screen Content Support 4
Jay Sage Unknown_CMD Not Working Support 4
fromano Fixed /E not working in TCMD 17.0.40 while doing one way synch Support 1
R WAD Copy's command dialogue not working Support 2
C WAD @index not working in v16 x64 Support 14
Dan Glynhampton Fixed @version "force" flag not working, and unable to set separator Support 3
C RSS feed subscription not working Support 9
M @Index not working like I thought it would... Support 2
C Working with links Support 6
C Move /MD not always working Support 1
C Startup folder not working on v15 Support 6
T Documentation Comspec not working Support 3
T view command not working Support 5
J Extended directory search has stopped working Support 9

Similar threads