Fixed In batch files, %0 now evaluates to the absolute path

Jun 24, 2008
223
0
Siegen, Germany
Since 17, in batch files %0 now expands to the batch file's absolute path, and no longer to just the file name. This breaks some functionality here...
 

rconn

Administrator
Staff member
May 14, 2008
12,365
150
I have changed v17 in build 57 to behave the same way as v16.

HOWEVER -- what you're doing is a really, really bad idea. V16 (and all previous versions, and CMD) never expanded %0 to just the file name. They expanded it to whatever was on the command line, be it the file name (with or without extension), partial pathname, or full path. What you're doing means your batch file will break if you run it from anywhere other than the current directory, or if you try to run it from a shortcut.
 
Nov 2, 2008
231
2
I avoid using %0, because you get things like mybatch vs mybatch.bat. It's really what the user types in. Use %_batchname and some of the fancy functions in 4nt to get the desired format.
 
Jul 13, 2017
20
0
@nickles @rconn Hi Nickles, don't know if you are still interested in this, but it appears that it is broken again in V 21

Sample batch file to show all you ever wanted to know about the running batch, but didn't know how to ask.

@echo off
for /f "tokens=* delims=?" %%d in ('cd ') do set start_drv=%%~dd& set start_dir=%%~pd
echo Starting drive = %start_drv%
echo Starting directory = %start_dir%
for /f "tokens=* delims=?" %%d in ('cd ') do set start_dir=%%~pd
echo Starting directory = %start_dir%
for /f "tokens=* delims=?" %%d in ('cd ') do set start_dir=%%d
echo Starting directory = %start_dir%
echo Starting command line = %0 %*
echo %%0 = %0
echo Running batch file name = %~nx0
echo Location of Running batch file name = %~dp0
echo FQN for Running batch file name = %~dpnx0


Output when run in IDE 21

Starting drive = C:
Starting directory =
Starting directory = \Program Files\JPSoft\
Starting directory = C:\Program Files\JPSoft\TCMD21
Starting command line = J:\test.bat par1 par2
%0 = J:\test.bat
Running batch file name = test.bat
Location of Running batch file name = J:\
FQN for Running batch file name = J:\test.bat


TCC is showing the FQN for %0 again.
Also it appears TCC can't handle the 2nd command "& set start_dir=%%~pd" on the "for /f" line and fails to execute it
And when run as a single command, does not get the correct value for the path - it truncates the \TCMD21 folder of the path

Here is the output when run in CMD

Starting drive = j:
Starting directory = \
Starting directory = \
Starting directory = j:\test
Starting command line = test par1 par2
%0 = test
Running batch file name = test.bat
Location of Running batch file name = j:\test\
FQN for Running batch file name = J:\test\test.bat


Strangely, CMD can extract the drive name from the "CD" output, but it can't extract the path !!

Errol
 
Dec 7, 2009
238
2
Left Coast, USA
I suppose this thread concerns compatibility with cmd.exe (I haven't read all of it). That aside, with respect to the previous comment I can't help wondering: Why be saddled with some of cmd.exe's most painful 'features' — having to use clumsy 'for' loops to derive certain information. for example — when TCC's own solutions are so much simpler? For example:

echo Starting drive: %_disk:
echo Starting directory: %_cwp\
echo %%0, literally: %0
echo %%0, via 'batchname variable': %_batchname
echo Starting command line: %cmdline


The above lines produce the following screen display (not intended to mirror your own commands precisely):

Starting drive: C:
Starting directory: \TakeCommand20\SupportFiles\
%0, literally: test.btm
%0, via 'batchname variable': C:\TakeCommand20\SupportFiles\test.btm
Starting command line: test.btm a b c


I'm behind schedule updating and I'm running v.20. Perhaps the results differ slightly in the latest version.

When I've had to write semi-complex batch files that require maximum compatibility with both systems, I've avoided the compatibility problem entirely. Instead I've inserted a simple test near the start to detect TCC. If it's detected, the batch file goes to a label where TCC-specific (.btm only) commands are used. Yes, there's duplication of effort. But the advantages are that 1) I never have to worry about perfect compatibility with cmd.exe and 2) I can take advantage of TCC commands that would be very difficult or even impossible in a cmd.exe environment. The idea of not using "iff/then/endiff" or "do" loops or "switch/endswitch" when I could use them — too painful to contemplate!
 

rconn

Administrator
Staff member
May 14, 2008
12,365
150
@nickles @rconn Hi Nickles, don't know if you are still interested in this, but it appears that it is broken again in V 21

TCC is showing the FQN for %0 again.


That's not quite accurate.

The contents of %0 depend on how you invoke the batch file (partial name, full name, partial path, full path, etc.), and is identical in both TCC and CMD. What you see in the IDE is because the batch debugger starts the batch file with a full path + full name. There's nothing wrong with starting a batch file with the full pathname, and if your batch files choke on that you might want to consider rewriting them (as they'll also fail when started from Explorer).
 

rconn

Administrator
Staff member
May 14, 2008
12,365
150
@nickles @rconn
Sample batch file to show all you ever wanted to know about the running batch, but didn't know how to ask.

@echo off
for /f "tokens=* delims=?" %%d in ('cd ') do set start_drv=%%~dd& set start_dir=%%~pd
echo Starting drive = %start_drv%
echo Starting directory = %start_dir%
for /f "tokens=* delims=?" %%d in ('cd ') do set start_dir=%%~pd
echo Starting directory = %start_dir%
for /f "tokens=* delims=?" %%d in ('cd ') do set start_dir=%%d
echo Starting directory = %start_dir%
echo Starting command line = %0 %*
echo %%0 = %0
echo Running batch file name = %~nx0
echo Location of Running batch file name = %~dp0
echo FQN for Running batch file name = %~dpnx0


Also it appears TCC can't handle the 2nd command "& set start_dir=%%~pd" on the "for /f" line and fails to execute it
And when run as a single command, does not get the correct value for the path - it truncates the \TCMD21 folder of the path

Your syntax is incorrect (for both CMD and TCC). If you have a compound command in a FOR, you need to use a command group:

for /f "tokens=* delims=?" %%d in ('cd ') do (set start_drv=%%~dd & set start_dir=%%~pd)

CMD is only appearing to work without the command group because it expands the entire line on the first SET, and then executes the second SET outside of the FOR loop. That's certainly not behavior that should be expected, encouraged or relied upon.
 
Jul 13, 2017
20
0
The contents of %0 depend on how you invoke the batch file (partial name, full name, partial path, full path, etc.), and is identical in both TCC and CMD. What you see in the IDE is because the batch debugger starts the batch file with a full path + full name.
OK. I couldn't find any doco on how the debugger is "fed".
@rconn I presume the command line use to start the batch file can't be modified ?

Your syntax is incorrect (for both CMD and TCC).
@rconn Thanks for the correction Rex - I missed that when I collapsed the command group of the "FOR" down to a single line

Errol
 
Jul 13, 2017
20
0
I can't help wondering: Why be saddled with some of cmd.exe's most painful 'features' — having to use clumsy 'for' loops to derive certain information. for example — when TCC's own solutions are so much simpler?
@mikea I am only on the third day of evaluating Take Command's IDE & debugger and already up to my Fourth "BUG report"
The batch files I write are not for use by me on my PC, but for servers in most of 40+ sites that I monitor and maintain.
If I decide the IDE & debugger are going to be useful for this work, then I will look at what TCC features may be helpful.

Errol
 
Similar threads
Thread starter Title Forum Replies Date
fpefpe How to? shortcuts and batch files Support 4
Joe Caverly "Functions" in cmd.exe batch files Support 6
S Batch files Calling Batch Files Support 2
P exit /b in batch files Support 7
P Labels in batch files: TCC vs CMD.exe Support 9
J New lexer for TCC batch files. Support 0
jadaml Echo unicode characters from UTF-8 Batch files? Support 1
J Files Disappearing During Batch Execution Support 4
D Installation batch files Support 5
D Run all .CMD batch files with CMD.EXE Support 16
T Lua sharing data with batch files Support 2
CWBillow Setting and changing Vars in batch files Support 26
C How to? create a JPSoft BTM menu to run custom custom batch files Support 2
Charles Dye GPF in TCC 13.03.47 on batch files >= 2048 bytes Support 10
vefatica Command line DO in batch files Support 0
MBaas GUI-Tool for "File/Open" or tree (for use in batch-files) Support 3
CWBillow Switches for COPY in batch files when paused Support 6
J v10 GPF when calling complicated batch files Support 5
vefatica `Back quotes` - command line vs. batch file Support 5
fpefpe How to? batch file size Support 2
FreezerBurnt Help making a CMD and TCC compatible batch file Support 7
M Oracle batch OPatch.bat.bat not working in TCC but correctly in CMD Support 5
Joe Caverly Set a batch variable (%1 - %n) when TCC is executing a batch file Support 5
P Kill all other instances of tcc from a btm batch file Support 2
fpefpe batch file ending early Support 18
R Batch Debugger Command Expansion Window Support 3
K robocopy cancelling batch file processing Support 28
A Unable to pass batch file arguments unaltered Support 18
K Batch debugger not stepping correctly Support 4
S incorrect message at start Cancel batch job 4START.bat ? (Y/N/A) : Support 1
MickeyF Why does this batch file give me 'unknown command "else"' error? Support 17
vefatica Batch parameters broken? Support 2
cxxl Set encoding for batch file Support 4
dcantor How to? Batch file to be executed by TCSTART Support 4
T Fixed Problem with use of Batch parameters in the IDE Support 1
T Fixed IDE locks up if edit window for running batch is closed Support 6
T How to? Show the values of batch variables on the TCC screen Support 21
D Alias in batch file Support 3
TT's Help! Uninstalling TCC did not restore cmd.exe as Win10 default batch file processor. Support 4
vefatica Exit code of a batch file? Support 4
J cd command altered in batch file Support 3
MickeyF running a program from cmdline versus in batch file Support 1
B Batch file won't run if there is already an instance of TC 17 running Support 9
nickles Fixed Executing long command lines in a batch generates file name errors Support 16
T batch execution Support 9
rjperrella start /tabna leaves blank windows when running batch scripts Support 10
Jay Sage WAD Possible Bug in Batch Parameters Support 7
C How to? run a batch file in administrator mode while debugging in take command software Support 1
R Unexpected 'Cancel Batch Job' prompts Support 13
cgunhouse Fixed bdebugger with not exist batch file causes a crash Support 2

Similar threads