up vote 0 down vote favorite
share [g+] share [fb]

Is it possible to auto-execute a command in a folder if you move to it via the cd command?

Of course I can define my own command (e.g. CHD.BAT) doing something like this:

@echo off
cd %1
if exist init.bat (
    init.bat
) else (
    color 0F
    title Command Prompt
)

But I wonder if something like this would be possible using the standard CD command.

link|improve this question
feedback

2 Answers

up vote 1 down vote accepted

No, as this would be insanely dangerous. If it was, you could trick an administrator into executing random malicious code just by visiting one of your folders.

link|improve this answer
Good point. This would indeed be very dangerous. – Patrick Mar 11 '10 at 14:27
feedback

you can do this by creating a doskey macro:

doskey cd=cd $*$Tif exist init.bat (init.bat) else (color 0f^&title Command Prompt)

However, it seems like output done in the init.bat file seems to get written after the usual prompt:

C:\Users\Me>doskey cd=cd $*$Tif exist init.bat (init.bat) else (color 0f^&title Command Prompt)

C:\Users\Me>cd stuff

C:\Users\Me\Stuff>Foo

(My init.bat in Stuff only contains @echo Foo.)

link|improve this answer
Even when @ECHO OFF is present? – Phoshi Mar 11 '10 at 20:17
It doesn't output the command, it outputs the prompt before the bat file's output is printed. – Joey Mar 12 '10 at 0:18
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.