batch for tokens delims discrepancy TC and CMD

English support forum

Moderators: white, Hacker, petermad, Stefan2

Post Reply
igarny
Junior Member
Junior Member
Posts: 58
Joined: 2023-01-26, 19:01 UTC

batch for tokens delims discrepancy TC and CMD

Post by *igarny »

Hello guys

Please help with this tricky situation.
The main objective is compile a directory location for the target tab based on a file in the source tab, but preserving the first 2 elements from the location of the target tab only in case it starts with \\\
I would like to integrate that in the menu

I have this batch file: loc.cmd

Code: Select all

@echo off
set /p loc=<%1.loc

 echo %loc%
set destp=%2

 echo %destp%

IF "%destp:~0,3%" == "\\\" (
  FOR /f "tokens=1,2 delims=\" %%a IN ("%destp%") do SET base=%%a& SET srv=%%b
  
  echo %base%
  echo /R=\\\%base%\%srv%%loc%
    %COMMANDER_EXE% /O /R=\\\%base%\%srv%%loc%
)
SET /p exit=Press any key to exit
of course with extra echo for debug

Unfortunately it produces this output when ran from the TC DirectoryHotlist menu with params=%P %T
LOC= \home\navi\dir2\test3\
DESTP= \\\FTP\1.Ansible_11\home\navi\dir2\test3\
BASE=
TCopt= /R=\\\\\home\navi\dir2\test3\
Press any key to exit
while I would expect to have these results, which are coming when I ran this from the CMD cli
C:\totalcmd\myWork>c:\totalcmd\myScripts\loc.cmd C:\totalcmd\myWork\TC-batch-var-from-cmd-FAIL\ \\\FTP\1.Ansible_11\home\
LOC= \home\navi\dir2\test3\
DESTP= \\\FTP\1.Ansible_11\home\
BASE= FTP
TCopt= /R=\\\FTP\1.Ansible_11\home\navi\dir2\test3\
Press any key to exit
I am totally lost... any other solution would also be appreciated
igarny
Junior Member
Junior Member
Posts: 58
Joined: 2023-01-26, 19:01 UTC

Re: batch for tokens delims discrepancy TC and CMD

Post by *igarny »

so the problem apparently is not in the parameter submission, but in the processing of the data by the FOR operator
the FOR operator does nothing in the environment of TC
User avatar
Horst.Epp
Power Member
Power Member
Posts: 6480
Joined: 2003-02-06, 17:36 UTC
Location: Germany

Re: batch for tokens delims discrepancy TC and CMD

Post by *Horst.Epp »

igarny wrote: 2023-03-07, 16:09 UTC so the problem apparently is not in the parameter submission, but in the processing of the data by the FOR operator
the FOR operator does nothing in the environment of TC
Thats wrong, the FOR operator works fine even in a button like in this example:

Code: Select all

TOTALCMD#BAR#DATA
cmd.exe
/k "for /f "usebackq delims=" %%f in (`dir /b /a-d /o-d`) do start "" "%%COMMANDER_EXE%%" /O /S /L="%%~ff\:" & exit"
C:\Tools\Wincmd\Icons\Down-Blue.ico
Newest File

1
-1
This selects the newest file in the current TC window regardless of the sort order.
Windows 11 Home x64 Version 23H2 (OS Build 22631.3447)
TC 11.03 x64 / x86
Everything 1.5.0.1371a (x64), Everything Toolbar 1.3.3, Listary Pro 6.3.0.73
QAP 11.6.3.2 x64
User avatar
Dalai
Power Member
Power Member
Posts: 9385
Joined: 2005-01-28, 22:17 UTC
Location: Meiningen (Südthüringen)

Re: batch for tokens delims discrepancy TC and CMD

Post by *Dalai »

2igarny
This code won't work, neither in CMD directly nor when called from within TC. You cannot set environment variables within a bracketed block and use them within the same block. Try the following code directly on CMD and you'll see what I mean:

Code: Select all

set somevar=foo
for /L %V IN (1,1,3) DO (
    echo %somevar%
    set somevar=%somevar%%V
)
echo %somevar%
The output will be

Code: Select all

foo
foo
foo
foo3
That's because the assignment is valid as soon as the bracketed block is left but not within the block.

In your case it worked when the same code was called a second time because the variable was still set from the previous run. But it will break again if the code is enclosed in setlocal/endlocal commands...

You can solve this by setting the variables in a goto target which is then called in the appropriate location. A rough outline:

Code: Select all

if "%destp:~0,3%"=="\\\" call :PROCESS
goto END

:PROCESS
REM you var assignments here
goto :EOF

:END
Regards
Dalai
#101164 Personal licence
Ryzen 5 2600, 16 GiB RAM, ASUS Prime X370-A, Win7 x64

Plugins: Services2, Startups, CertificateInfo, SignatureInfo, LineBreakInfo - Download-Mirror
igarny
Junior Member
Junior Member
Posts: 58
Joined: 2023-01-26, 19:01 UTC

Re: batch for tokens delims discrepancy TC and CMD

Post by *igarny »

@Horst.Epp
Thanks. Let's adjust your example then...
my string that needs to be parsed by the FOR operator is not split by new-line chars, but \
so I am looking for something that parses this: \\\ABC\xyz\.whatever..\.blah.blah.
the first 2 tokens are important
the 1st is the network plugin of TC
the 2nd is the server can have all sorts of chars allowed for a windows filename



@Dalai
This code won't work, neither in CMD directly
Guys I am not trying to trick or cheat you here...
The results I posted are from a real execution... the extra descriptions and echo commands I have put are to demonstrate the issue, not that I need these in the batch file.

Please note the execution and the output is real:

Code: Select all

C:\totalcmd\myWork>c:\totalcmd\myScripts\loc.cmd C:\totalcmd\myWork\TC-batch-var-from-cmd-FAIL\ \\\FTP\1.Ansible_11\home\
LOC= \home\navi\dir2\test3\
DESTP= \\\FTP\1.Ansible_11\home\
BASE= FTP
TCopt= /R=\\\FTP\1.Ansible_11\home\navi\dir2\test3\
Press any key to exit
@Dalai
On top of that the code block of the FOR operator is opened and closed in 1 line, so there is no such thing as
You cannot set environment variables within a bracketed block and use them within the same block
Thanks
User avatar
white
Power Member
Power Member
Posts: 4614
Joined: 2003-11-19, 08:16 UTC
Location: Netherlands

Re: batch for tokens delims discrepancy TC and CMD

Post by *white »

igarny wrote: 2023-03-07, 16:04 UTC while I would expect to have these results, which are coming when I ran this from the CMD cli
C:\totalcmd\myWork>c:\totalcmd\myScripts\loc.cmd C:\totalcmd\myWork\TC-batch-var-from-cmd-FAIL\ \\\FTP\1.Ansible_11\home\
LOC= \home\navi\dir2\test3\
DESTP= \\\FTP\1.Ansible_11\home\
BASE= FTP
TCopt= /R=\\\FTP\1.Ansible_11\home\navi\dir2\test3\
Press any key to exit
You only get this result from CMD cli when you run the batch file a second time. So when the variables have the values from running it the first time.
Dalai is right, your code doesn't work. The strings %base%, %srv% and %loc% are evaluated at the start if the IF statement.
Try something like this (untested):

Code: Select all

@echo off
set loc=
set destp=
set base=
set srv=

set /p loc=<%1.loc

 echo %loc%
set destp=%2

 echo %destp%

IF "%destp:~0,3%" == "\\\" FOR /f "tokens=1,2 delims=\" %%a IN ("%destp%") do SET base=%%a& SET srv=%%b

IF defined base (
  echo %base%
  echo /R=\\\%base%\%srv%%loc%
    %COMMANDER_EXE% /O /R=\\\%base%\%srv%%loc%
)

SET /p exit=Press any key to exit

igarny wrote: 2023-03-07, 18:09 UTC @Dalai
On top of that the code block of the FOR operator is opened and closed in 1 line, so there is no such thing as
You cannot set environment variables within a bracketed block and use them within the same block
He was talking about the IF code block.
User avatar
beb
Senior Member
Senior Member
Posts: 432
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

Re: batch for tokens delims discrepancy TC and CMD

Post by *beb »

Dalai wrote: 2023-03-07, 17:51 UTC...You cannot set environment variables within a bracketed block and use them within the same block...
You can.

Code: Select all

@echo off
Setlocal EnableDelayedExpansion
set somevar=foo
echo %somevar%
for /L %%V IN (1,1,3) DO (
    set somevar=%somevar%%%V
    echo !somevar!
)
The output will be

Code: Select all

foo
foo1
foo2
foo3
NB https://ss64.com/nt/delayedexpansion.html
#278521 User License
Total Commander [always the latest version, including betas] x86/x64 on Win10 x64/Android 10
User avatar
Dalai
Power Member
Power Member
Posts: 9385
Joined: 2005-01-28, 22:17 UTC
Location: Meiningen (Südthüringen)

Re: batch for tokens delims discrepancy TC and CMD

Post by *Dalai »

2beb
I know. I didn't want to make it more difficult or complex than necessary.

Regards
Dalai
#101164 Personal licence
Ryzen 5 2600, 16 GiB RAM, ASUS Prime X370-A, Win7 x64

Plugins: Services2, Startups, CertificateInfo, SignatureInfo, LineBreakInfo - Download-Mirror
User avatar
beb
Senior Member
Senior Member
Posts: 432
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

Re: batch for tokens delims discrepancy TC and CMD

Post by *beb »

Hence

Code: Select all

@echo off
Setlocal EnableDelayedExpansion

...

FOR /f "tokens=1,2 delims=\" %%a IN ("%destp%") do SET base=%%a& SET srv=%%b
echo !base!
echo /R=\\\!base!\!srv!!loc!
%COMMANDER_EXE% /O /R=\\\!base!\!srv!!loc!

...
#278521 User License
Total Commander [always the latest version, including betas] x86/x64 on Win10 x64/Android 10
igarny
Junior Member
Junior Member
Posts: 58
Joined: 2023-01-26, 19:01 UTC

Re: batch for tokens delims discrepancy TC and CMD

Post by *igarny »

Many thanks guys: @Dalai @white @beb @Horst.Epp

Apologies @Dalai for making remarks without having a complete understanding
igarny
Junior Member
Junior Member
Posts: 58
Joined: 2023-01-26, 19:01 UTC

Re: batch for tokens delims discrepancy TC and CMD

Post by *igarny »

I have figured it out for the 90% part and even made a oneliner, but now I am confused with the parameter settings from Total Commander

Code: Select all

Setlocal EnableDelayedExpansion & set /p loc=<%1.loc && echo LOC=!loc! > c:\test.txt
This works in a batch file when called with %P as param, but doesn't work for the menu command

Code: Select all

cmd=%COMSPEC% /k
param=Setlocal EnableDelayedExpansion & set /p loc=<%P.loc && echo LOC=!loc! > c:\test.txt
User avatar
beb
Senior Member
Senior Member
Posts: 432
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

Re: batch for tokens delims discrepancy TC and CMD

Post by *beb »

igarny wrote: 2023-03-08, 11:38 UTC ... doesn't work for the menu command

Code: Select all

cmd=%COMSPEC% /k
param=Setlocal EnableDelayedExpansion...
https://ss64.com/nt/cmd.html
see CMD /V option

Code: Select all

/V[:ON] Enable delayed environment variable expansion;
        this allows a FOR loop to specify !variable! instead of %variable%
        expanding the variable at execution time instead of at input time.)
and try something like this:

Code: Select all

cmd=%COMSPEC% /v /k set /p loc=<test.loc && echo LOC=!loc!>test.txt
or

Code: Select all

cmd=%COMSPEC% /v /k for /r %i in (*.loc) do set /p loc=<%i && echo LOC=!loc!>test.txt
#278521 User License
Total Commander [always the latest version, including betas] x86/x64 on Win10 x64/Android 10
Post Reply