Batch File, If exists not working for me.

August 3, 2011 at 17:51:44
Specs: Windows 7 x64
Hey, I have been working on a batch file mostly for fun and am trying to have the file check to see if there is a password file and if so then request a password, if not then just move on. but with the if exist command It seems to require the full directory. (C:\windows.......etc)

here is the code I have,
if exist DGPWD.PWD goto DGPWD else goto PasswordChecker


:DGPWD
title David's Gateway Password Check
set /p %ranran%=<DGPWD.PWD
if %DGPWD22%==0 goto PWend
cls
echo Please Enter Your David's Gateway Password.
echo Attempts Left: %DGPWD22% (3 max)
echo.
set /p ZJZ=Password:
if %ZJZ%==%ranran% goto DGPWDCHKC
if not %ZJZ%==%ranran% goto DGPWDCHKIC

:DGPWDCHKC
echo Correct Login Attempts Left: %DGPWD22% (3 max) >> log.temp
goto PasswordChecker

:DGPWDCHIC
cls
echo INCORRECT PASSWORD TRY AGAIN
ping localhost -n 3 >nul
set /a %DGPWD22%==%DGPWD22%-1
echo Incorrect Password Attempts Left: %DGPWD22% (3max) >> log.temp
goto DGPWD


See More: Batch File, If exists not working for me.

Report •


#1
August 3, 2011 at 19:14:13
I tried getting around it using if exist %cd%\DGPWD.PWD goto DGPWD else goto PasswordChecker but now it does not ask for the PW if it is there before it would just ask for the password if the file was there or not, now it never asks for it...

Report •

#2
August 3, 2011 at 20:10:49
AFAIK If Exist only searches the default directory. If your default is C:\ then that is the only one to be searched.

If the file you are seaching for is not in the default directory the return will always be False. A better path is probably required unless you CD or Pushd to the folder where your file resides. (C:\Windows\.....)

Good luck


Please come back & tell us if your problem is resolved.


Report •

#3
August 3, 2011 at 21:00:19
I was thinking of doing that, but I was trying to make it atleast semi easy to move from computer to computer. however I am using 7z commands so I have to make sure the computer has 7z in system32. would it be better to make a folder C:\...etc for all the extra files for the batch?

Report •

Related Solutions

#4
August 3, 2011 at 22:47:19
Using a dedicated folder for an applications files makes sense, no-one would be in doubt then as to where the files were located. 7z could be located in the same dedicated folder provided the folder was added to the Path environment variable.

Up to you.


Please come back & tell us if your problem is resolved.


Report •

#5
August 3, 2011 at 23:46:51
The problem is that if/else requires at least the first command to be contained in parenthesis, be it a single or multiple line format.

if exist DGPWD.PWD (goto DGPWD) else goto PasswordChecker

This is probably so cmd can distinguish between the keyword "else" and the text "else" being used as a command.


Report •


Ask Question