Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 (
) else (
    color 0F
    title Command Prompt

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

share|improve this question
Why do you need that anyway? Sounds fishy. – Camilo Martin Aug 31 '12 at 14:31
It was just an idea that I had to automatically set the window title of my command prompt depending on the current folder. E.g. when in my development folder of release 1.0 of my application X, I want to set the command prompt title to "X 1.0". This is useful if you are performing a big script in that command prompt window and you cannot scroll back to the prompt (which is indeed set to $p$g). – Patrick Sep 3 '12 at 11:52
What do you mean "cannot scroll back to the prompt"? You can configure CMD.EXE's buffer (right-click the icon on the left top of the window, properties. Or something like that) to be able to scroll back a lot. If that's what you mean. Also, you could create a batch file to use instead of CD - something like mycd.bat. – Camilo Martin Sep 4 '12 at 1:00
When the script outputs lots of lines (e.g. one line for every file that is being compiled in a full compilation), 9999 lines are not enough. But indeed something like mycd.bat would solve the problem. – Patrick Sep 4 '12 at 9:18
I just wonder if it wouldn't be easier for you to compile from an IDE, or if you have a machine just for compiling, automate it. Also, why not edit the build script itself to change the title? – Camilo Martin Sep 4 '12 at 16:04
up vote 2 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.

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

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


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

share|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

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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