user13236236
user13236236

Reputation:

Make batch file runnable only in a specific location

I have created a batch file for my own use. The batch file itself has been added to my path so it can easily be run on the command line.

I only want to be able to run this command if my current location on the command line is something specific. i.e. I only want the file to be runable/usable if my location in the command window is something like C:\FolderA, and not runnable anywhere else, including in any subdirectories, so it would not work in C:\FolderA\FolderB or C:\FolderC. If the location doesn't match the "approved" location then a simple error should be thrown.

Is there an easy-ish way to do this? I am very new to batch-files so any help is greatly appreciated. Thanks

Upvotes: 1

Views: 91

Answers (1)

Justaus3r
Justaus3r

Reputation: 423

You can do such a thing by using the %cd% system variable which returns current directory then you can compare that with the directory you want the batch file to be executable.here is a simple batch file that will do the work.

@echo off
rem replace the path with yours
if "%cd%"=="D:\WinPwnage" goto continue
goto error
:error
echo You are not in that specific location
rem just to sleep for 5 seconds
ping localhost -n 5 >nul
exit
:continue
echo Your code
pause >nul 

Edit:
@Squashman reminded that the single qoutes wont work with special characters so i edited it.

Upvotes: 1

Related Questions