%COMMANDER_PATH% question

English support forum

Moderators: Hacker, petermad, Stefan2, white

Post Reply
Spiderkeys
Junior Member
Junior Member
Posts: 53
Joined: 2015-04-07, 10:02 UTC

%COMMANDER_PATH% question

Post by *Spiderkeys »

I've started using the variable %COMMANDER_PATH% in my batch files, it's makes portability now much easier than typing the whole path which was different from every machine/OS I've got.

But something puzzles me, my batch files recognise the variable, the command prompt built into TC (cm_ExecuteDOS) also recogises it, but using C:\WINDOWS\system32\cmd.exe and typing 'ECHO TC commander Path is %COMMANDER_PATH%' it doesn't recognise it, it just returns TC commander Path is %COMMANDER_PATH% message.

And how can this be global? I mean I cannot find the term anywhere %COMMANDER_PATH% in the system registry.
User avatar
karlchen
Power Member
Power Member
Posts: 4605
Joined: 2003-02-06, 22:23 UTC
Location: Germany

Post by *karlchen »

Hello, Spiderkeys.

The Variable %COMMANDER_PATH% will be set by Total Commander itself when starting up. As a consequence only Total Commander itself and all the processes which Total Command spawns will know the variable %COMMANDER_PATH%.
In case, however, you launch the Windows command prompt from outside Total Commander, e.g. from the Windows application start menu or by using <WinKey>R and typing "cmd.exe", then this incident of cmd.exe will have no knowledge of the variable %COMMANDER_PATH%, because it is not a child process of Total Commander.

Cheers,
Karl
Spiderkeys
Junior Member
Junior Member
Posts: 53
Joined: 2015-04-07, 10:02 UTC

Post by *Spiderkeys »

Ahh, now i'm beginning to get it, about 95% of my batches are always launched within TC, and very few are launched from Scheluded Tasks or desktop. Just dragged one from TC from to desktop, and as expected as reading above, it didn't work.

Still handy, just got to think before I write a script, if I've been ever needing to launch it outside TC, then obviously the full paths would then be required.

Or maybe use the SET command at the start of those scripts just to be sure.
User avatar
Stefan2
Power Member
Power Member
Posts: 4281
Joined: 2007-09-13, 22:20 UTC
Location: Europa

IF NOT defined Commander_Path Exit

Post by *Stefan2 »

Spiderkeys wrote:Or maybe use the SET command at the start of those scripts just to be sure.
If you want you can check if a variable is defined.

Examples:

Code: Select all

@ECHO OFF
IF NOT defined Commander_Path Exit
REM else:
ECHO I'm still here and do my stuff now...
rem
rem
rem

- - - or:

Code: Select all

@ECHO OFF
Setlocal EnableDelayedExpansion

if defined Commander_Path ( 
    ECHO TC-Path is set already to: %Commander_Path% 
    ECHO All fine, will go on...
) else (
    ECHO Commander_Path var is not defined yet!
    SET /P TCPATH="Enter path to TC-folder (or x to close):"
    IF !TCPATH!==x   Exit
    IF [!TCPATH!]==[] Exit
    SET Commander_Path=!TCPATH!
    ECHO TC-Path is set now to: !Commander_Path!
    ECHO Everything is fine now, will go on...
)

ECHO Do your stuff here:
rem
rem
rem


rem All done!
PAUSE

 
Spiderkeys
Junior Member
Junior Member
Posts: 53
Joined: 2015-04-07, 10:02 UTC

Post by *Spiderkeys »

Thats very clever Stefan2, I prefer your second example more, even still if it comes to worse, I only need to type out the path once and not 50 times when it came to very long scripts.

A batch file which would work nearly perfectly on one OS, move it another required changing paths to TC, and always I missed one somewhere, now I only need to pay attention to check if variable is defined on top of each script.
User avatar
Dalai
Power Member
Power Member
Posts: 10035
Joined: 2005-01-28, 22:17 UTC
Location: Meiningen (Südthüringen)

Post by *Dalai »

I'm using another approach: launch TC via batch file which is located in the same path as totalcmd.exe, like this:

Code: Select all

start /wait "Total Commander" "%~dp0totalcmd.exe"
So the %~dp0 is equal to %COMMANDER_PATH%. To automate this, and to be able to use the variable before launching TC, you could do something like this:

Code: Select all

if "%COMMANDER_PATH%"=="" set COMMANDER_PATH=%~dp0
somewhere in the first couple lines.

Be careful, though, as %~dp0 includes the trailing backslash!

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
Post Reply