Skip navigation

JSI Tip 7779. How do I prevent a script from running if it is not started from a specific current directory?


If you have a script that you require to be launched when the current directory is set to a specific path, you can use the following technique to abort the script:

@echo off
if /i "%CD%" NEQ "SpecificCurrentDirectory" @echo The %0 script was launched with the following current directory: %CD%&exit /b 1
:: Continue

Example

To only run if the current directory is the Windows folder: @echo off if /i "%CD%" NEQ "%SystemRoot%" @echo The %0 script was launched with the following current directory: %CD%&exit /b 1 :: Continue



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish