LoganMurphy
LoganMurphy

Reputation: 89

How to use parentheses in .bat files, set command and IF EXIST

Ok, I have batch file, just a simple one that hides and unhides folders.

I don't see why it cannot seem to execute accordingly;

Here is extended sample code:

@echo off
color a
title Folder/Drive hider Service

:jiki
echo Loading...
TIMEOUT /T 2 >nul
goto inputs
:inputs
echo Enabling security...
TIMEOUT /T 2 >nul
cls
goto menu

:menu
if EXIST "%~dp0\Encryption" (set status=Folder is locked.)
if EXIST "%~dp0\Logan_Documents" (set status=Folder is unlocked, to open it, enter open as your            `action.)`
cls
echo.
echo STATUS: %status%
echo.
echo ----------------------------------------
echo  FOLDER PROTECTOR by Logan 
echo ----------------------------------------
echo.
echo  Lock      = Lock the folder(s)
echo  Unlock    = Unlock the folder(s)
echo  Credits   = For more info
echo  V         = Display your current version
echo  Exit      = Close the program
echo.
echo ----------------------------------------
echo For more info, just ask Logan!
echo ----------------------------------------
echo.
echo Select your action, %USERNAME%.
echo.
set /p "menu=>"
if /I %menu%== lock goto lock
if /I %menu%== unlock goto unlock
if /I %menu%== credits goto credits
if /I %menu%== v goto version
if /I %menu%== exit goto exit
goto invalid

and also a lot more, and every time I go to execute the script, it just leaves the status variable blank.

Here's what I've tried.

Appreciate all efforts to resolve my problem and get this program working! -Logan

Upvotes: 2

Views: 834

Answers (2)

ths
ths

Reputation: 2942

probably neither of the ifs are true, maybe because the active directory is not what you think it is. you can test this easily by inserting a set status=none above the ifs. or insert dir to see what the scrips actually sees at this point.

Upvotes: 0

GolezTrol
GolezTrol

Reputation: 116120

if exist should work fine exactly as you use it. You don't strictly need the quotes, since the names don't include spaces. Also you don't need the parentheses since it is a single command.

But then again, it should work with them as well (I actually tested this), so the only thing I can imagine is that the files or folders are not found because the script is running in the wrong directory. After all you use just the names without any path, so the current directory should contain those files.

The 'current directory' isn't necessarily the directory in which the script is saved. If you are in 'C:\Foo' and you call 'C:\Bar\Script.bat', the current directory will still be 'C:\Foo'. The same goes for starting scripts through a shortcut.

To try this, you can use echo %CD% in your script to echo the current directory.

As a possible solution, you can use %~dp0 to use the actual directory in which the batch script is saved, so you always have a starting point to start from:

REM Check if 'Encryption' exists in the same folder as the batch file.
if EXIST "%~dp0\Encryption" (set status=Folder is locked.)

Upvotes: 2

Related Questions