New in.exe and with.exe commands for the Windows cmd shell

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jfl
Posts: 226
Joined: 26 Oct 2012 06:40
Location: Saint Hilaire du Touvet, France
Contact:

New in.exe and with.exe commands for the Windows cmd shell

#1 Post by jfl » 10 Jan 2023 12:44

Hello,

I've published an updated version of my System Tools Library, including two new tools for the cmd.exe shell:
  • in.exe: Execute a command in a different directory, then come back. Avoids having to remember to run popd afterwards.
    Ex: `in .. cd` displays the full name of the parent directory:

    Code: Select all

    C:\JFL\Temp>in .. cd
    C:\JFL
    
    C:\JFL\Temp>
  • with.exe: Execute a command with specific environment variables.
    Useful in the Windows cmd shell, which (contrary to Unix shells) does not have that feature built in.
    Ex: `with DEBUG=1 build program.exe` builds the debug version of your application.
Initially I had implented these tools as plain batch files.
For example, in.bat contained:

Code: Select all

@echo off
pushd %1
shift
set CMD=%1
shift
:encore
if not [%1]==[] (
  set CMD=%CMD% %1
  shift
  goto :encore
)
call %CMD%
popd
This worked fine in simple cases, but had issues with a few trick characters ...
There were also problems when interrupting a command with Control-C.
Furthermore, using them in batch scripts required using a call, which is not as friendly.
Rewriting both tools in C was the best solution, and was actually easy.

The In.c source file can also be compiled for Linux or MacOS, which (as far as I know) do not have an equivalent capability.
I was forced to name the command "In" (with a capital i) instead of "in", because "in" is a reserved keyword in the bash shell.
Fortunately, it's possible to define `alias In=in`, and bingo we get in (with a lower case i) there too :-)
This is done automatically by SysToolsLib's `make install` command in Linux or MacOS.

Any comment welcome.
Enjoy!

Post Reply