BJ Dela Cruz
BJ Dela Cruz

Reputation: 5354

How to check if user is in correct directory using Windows batch file

I want to check if the user is in the correct directory in the command prompt using a Windows batch file. The correct directory is stored in a system environment variable. For example, if HOME is set to C:\path\to\home, I want to know if the user is in that directory:

c:\some\directory>check.bat
No

c:\path\to\home>check.bat
Yes

Upvotes: 2

Views: 244

Answers (3)

David Ruhmann
David Ruhmann

Reputation: 11367

You can compare the location the script in being run from with a check like this

if /i "%~dp0"=="%HOME%\"

Or check the current working directory with %CD%

However, you could also just make it the correct directory

cd /d "%HOME%"

For a true directory comparison where folders may or may not have a trailing backslash

for %%A in ("%~dp0%\") do for %%B in ("%HOME%\") do if /i "%%~fA"=="%%~fB"

Upvotes: 2

Derek
Derek

Reputation: 8773

if /i "%CD%"=="%HOME%" echo woooooooooo!

%CD% represents the current directory for the command prompt.

Upvotes: 2

nobody
nobody

Reputation: 20174

if /i "%CD%"=="%HOME%" (
    echo Yes
) else (
    echo No
)

Upvotes: 3

Related Questions