Custom command for switching between directories...

English support forum

Moderators: white, Hacker, petermad, Stefan2

Post Reply
Capa79
Junior Member
Junior Member
Posts: 6
Joined: 2024-03-01, 08:25 UTC

Custom command for switching between directories...

Post by *Capa79 »

Hello.

I have no experience with creating my own commands, so I would need help. I can probably handle the user-command definition itself (usercmd.ini) and the connection to some keyboard shortcut. But I have no idea how to do the following:

I need to quickly switch (in the current panel) between "corresponding" dirs (will explain later). I would probably also use the option of switching in to the given path the 2nd panel, but that is a "nice to have" feature. I am working with a UE5 (Unreal Engine) projects and it has its source-files of any module divided into "Public" directory (*.h files) and "Private" directory (*.cpp). The tree structure underneath is more or less identical. And I would like to quickly switch between the corresponding sub-directories - if I am in the dir with *.h, then switch to the dir with corresponding *.cpp file and vice versa.

I suppose I should have some *.bat script that would output the path I want to switch to... but I don't know how to do it in TC. The script will travel "up" in the tree structure (remembering the relative path) until it is in a "root" directory with both "Public" and "Private" subdirectories, and then it will dive (into the same relative path) to the opposite of the one I came from.

Does anyone know how to do it? I'm talking about building the custom TC command itself using some *.bat file, which would receive the current panel's path as a parameter and, on the contrary, result in the path where to switch.

Thanks a lot for any "guidance".
Martin
Last edited by Capa79 on 2024-03-01, 11:26 UTC, edited 1 time in total.
User avatar
Gral
Power Member
Power Member
Posts: 1467
Joined: 2005-01-26, 15:12 UTC

Re: Custom command for switching between directories...

Post by *Gral »

Create custom commands for directory change, e.g add such a lines to your USERCMD.INI

Code: Select all

[em_CDPublic]
cmd=CD ..\Public

[em_CDPrivate]
cmd=CD ..\Private
Bind keyboard shortcuts to both.
sa16
Senior Member
Senior Member
Posts: 217
Joined: 2021-09-10, 07:15 UTC

Re: Custom command for switching between directories...

Post by *sa16 »

Here user yozhic suggested the following BAT-file:

Code: Select all

@rem Script: test.bat
@rem Parameters: "%P" "\part of the path\" "\part of the path\"
 
@echo off
set go=%~1
call set go=%%go:%~2=%~3%%
if "%~1"=="%go%" (call set go=%%go:%~3=%~2%%)
%COMMANDER_EXE% /O /S "%go%"
For example, such a button for it:

Code: Select all

TOTALCMD#BAR#DATA
%COMMANDER_PATH%\CMD\test.bat
"%Z%P" "c:\Wincmd\" "d:\Wincmd_x64\"
wcmicons.dll,75
Replacing part of the path and navigating the modified path|c:\Wincmd <--> d:\Wincmd_x64

1
-1
Capa79
Junior Member
Junior Member
Posts: 6
Joined: 2024-03-01, 08:25 UTC

Re: Custom command for switching between directories...

Post by *Capa79 »

Gral wrote: 2024-03-01, 10:30 UTC Create custom commands for directory change, e.g add such a lines to your USERCMD.INI

Code: Select all

[em_CDPublic]
cmd=CD ..\Public

[em_CDPrivate]
cmd=CD ..\Private
Bind keyboard shortcuts to both.
Thanks, but it won't be so easy :)... as the "root" directory (the one that contains both 'Public' and 'Private' sub-directories) is'nt always just one level-up in hierarchy.

But I found much easier way how the script for generating target path should work:
1) take current path
2) if it contains '\Public\' replace it with '\Private\' or
if it contains '\Private' replace it with '\Public'\
3) if modified path is valid (exists) "switch" to it... otherwise stay where you are
Fla$her
Power Member
Power Member
Posts: 2318
Joined: 2020-01-18, 04:03 UTC

Re: Custom command for switching between directories...

Post by *Fla$her »

2Capa79
There are several solutions here. If you need help with translation, we will provide it.
I advise you to pay attention to the last two codes.
Overquoting is evil! 👎
Capa79
Junior Member
Junior Member
Posts: 6
Joined: 2024-03-01, 08:25 UTC

Re: Custom command for switching between directories...

Post by *Capa79 »

So far I got here but it is still not working and I don't know why...

Custom command:

Code: Select all

cmd=c:\switch_ue5_src_dirs.bat
params="%P"
Content of the switch_ue5_src_dirs.bat:

Code: Select all

@echo off
setlocal

set "inputPath=%~1"

:: try to switch between 'Public' and 'Private'
echo %inputPath% | findstr /i "\\Public\\" > nul
if not errorlevel 1 (
    set "newPath=%inputPath:\Public\=\Private\%"
    goto verifyPath
)

echo %inputPath% | findstr /i "\\Private\\" > nul
if not errorlevel 1 (
    set "newPath=%inputPath:\Private\=\Public\%"
    goto verifyPath
)

::given path doesn't contain '\Public\' or '\Private\'
exit /b 1

:verifyPath
if exist "%newPath%" (
    %COMMANDER_EXE% /O /S "%newPath%"
)

endlocal
Fla$her
Power Member
Power Member
Posts: 2318
Joined: 2020-01-18, 04:03 UTC

Re: Custom command for switching between directories...

Post by *Fla$her »

2Capa79
Above, I suggested using non-console js/vbs. Are there any difficulties?
Overquoting is evil! 👎
Capa79
Junior Member
Junior Member
Posts: 6
Joined: 2024-03-01, 08:25 UTC

Re: Custom command for switching between directories...

Post by *Capa79 »

Oh... I can try to do it via Javascript. Have no experience, probably ChatGPT will help me :), but I would need something to allow me execute JScript in Win10, isn't it?
Fla$her
Power Member
Power Member
Posts: 2318
Joined: 2020-01-18, 04:03 UTC

Re: Custom command for switching between directories...

Post by *Fla$her »

2Capa79
I've already given you the solutions, you don't need ChatGPT. JScript on Win 98-11 should work. .js is the same executable file as .bat, but you need to save it in ANSI.

Code: Select all

/*********************** JS **********************
 Switch between two parts of a working folder path

 Parameters: "%P" <Path part 1> <Path part 2>
    Example: "%P" "\DirName 1\" "\DirName 2\"
*************************************************/
a = WSH.arguments
if(a(0).indexOf(a(1))>-1){i = 1; n = 2} else {if(a(0).indexOf(a(2))>-1){i = 2; n = 1} else WSH.quit()}
WSH.CreateObject('WScript.Shell').exec('%COMMANDER_EXE% /O0 /S "' + a(0).replace(a(i), a(n)) + '"')
Overquoting is evil! 👎
Capa79
Junior Member
Junior Member
Posts: 6
Joined: 2024-03-01, 08:25 UTC

Re: Custom command for switching between directories...

Post by *Capa79 »

Thank you @Fla$her. It worked work me... don't know why but I ended with 2nd and 3rd param (exchanged part of path) without quotation.
Otherwise I got "Subscript out of range" error.
Fla$her
Power Member
Power Member
Posts: 2318
Joined: 2020-01-18, 04:03 UTC

Re: Custom command for switching between directories...

Post by *Fla$her »

2Capa79
Any parameters containing spaces must be in quotation marks. This applies to all parameterized programs.
Overquoting is evil! 👎
Post Reply