{batch} IF NOT EXIST <mon dossier> ne fonctionne pas
Solved
Tinouboom
Posted messages
12
Status
Membre
-
barnabe0057 Posted messages 14431 Registration date Status Contributeur Last intervention -
barnabe0057 Posted messages 14431 Registration date Status Contributeur Last intervention -
Hello everyone
So here it is, I want to ask the user to indicate the path to the OpenVPN installation folder (with a default path - see code). However, when I added the "if" statements to catch potential errors, it stopped working, the program constantly redirects to :error_path1 then the console closes on the second attempt (I thought it was because of the quotes due to spaces but I tested everything, nothing works :) )
Thank you for your help.
@echo off
REM The user indicates the path of the OpenVPN installation folder
:path
set /p path=Please enter in quotes the full path of the OpenVPN folder [default (press Enter) "C:\Program Files (*86)\OpenVPN"]:
echo.
if "%path%"=="" (
set path="C:\Program Files (*86)\OpenVPN"
)
IF NOT EXIST %path%\nul (
GOTO :error_path1
)
IF NOT EXIST %path%\easy-rsa\nul (
GOTO :error_path2
)
cd %path%
GOTO :choice
:error_path1
cls
echo ### The path %path% is not valid. Check the spaces and quotes ###
echo.
set "%path%"==""
GOTO :path
:error_path2
cls
echo ### The path %path% is not correct ###
echo.
set "%path%"==""
GOTO :path
:choice
echo If you want to create or configure your OpenVPN server (ONLY ONCE), type 1 and press Enter
So here it is, I want to ask the user to indicate the path to the OpenVPN installation folder (with a default path - see code). However, when I added the "if" statements to catch potential errors, it stopped working, the program constantly redirects to :error_path1 then the console closes on the second attempt (I thought it was because of the quotes due to spaces but I tested everything, nothing works :) )
Thank you for your help.
@echo off
REM The user indicates the path of the OpenVPN installation folder
:path
set /p path=Please enter in quotes the full path of the OpenVPN folder [default (press Enter) "C:\Program Files (*86)\OpenVPN"]:
echo.
if "%path%"=="" (
set path="C:\Program Files (*86)\OpenVPN"
)
IF NOT EXIST %path%\nul (
GOTO :error_path1
)
IF NOT EXIST %path%\easy-rsa\nul (
GOTO :error_path2
)
cd %path%
GOTO :choice
:error_path1
cls
echo ### The path %path% is not valid. Check the spaces and quotes ###
echo.
set "%path%"==""
GOTO :path
:error_path2
cls
echo ### The path %path% is not correct ###
echo.
set "%path%"==""
GOTO :path
:choice
echo If you want to create or configure your OpenVPN server (ONLY ONCE), type 1 and press Enter
3 réponses
Hello,
When you use set to define a variable, you should only use one equal sign (=) and you shouldn't put the %%
If I were you, I wouldn't even put the quotes
Regarding "if exist," you shouldn't put nul, just specify the folder.
“Artificial intelligence is defined as the opposite of natural stupidity.”
When you use set to define a variable, you should only use one equal sign (=) and you shouldn't put the %%
set chemin=""
If I were you, I wouldn't even put the quotes
set chemin=
Regarding "if exist," you shouldn't put nul, just specify the folder.
“Artificial intelligence is defined as the opposite of natural stupidity.”
Does it work like this :
“Artificial intelligence is defined as the opposite of natural stupidity.”
:path
rem we define a default path
set path=%ProgramFiles(x86)%\OpenVPN\easy-rsa
rem we navigate to the folder
cd "%ProgramFiles(x86)%"
rem we search for OpenVPN\easy-rsa
for /f "tokens=*" %%A in ('dir /b /ad /s "*.*" ^| findstr /i "easy-rsa"') do ((echo %%A | findstr "VPN") && (set path=%%A))
echo %path%
pause
:choice
echo If you want to create or configure your OpenVPN server (ONLY ONCE), type 1 and press Enter
“Artificial intelligence is defined as the opposite of natural stupidity.”
Yes, there is a way, but it's going to take too much time.
If I were you, I would first check whether it's a 32-bit or 64-bit Windows, then depending on that, I would look either in %ProgramFiles(x86)% for a 64-bit Windows or in %ProgramFiles% for a 32-bit Windows.
To determine if it's a 32-bit or 64-bit:
if exist "%windir%\SysWoW64\cmd.exe" ==>> if the file exists, it's a 64-bit Windows.
If I were you, I would first check whether it's a 32-bit or 64-bit Windows, then depending on that, I would look either in %ProgramFiles(x86)% for a 64-bit Windows or in %ProgramFiles% for a 32-bit Windows.
To determine if it's a 32-bit or 64-bit:
if exist "%windir%\SysWoW64\cmd.exe" ==>> if the file exists, it's a 64-bit Windows.
Thank you for your quick reply. Indeed, a small oversight. However, the error handling still doesn't work.
What we need to do is check if the
Any ideas?
REM The user specifies the installation path for OpenVPN
:chemin
set /p chemin=Please enter in quotes the full path of the OpenVPN folder [default (press Enter) "C:\Program Files (*86)\OpenVPN"]:
echo.
if "%chemin%"=="" (
set chemin="C:\Program Files (*86)\OpenVPN"
)
REM IF NOT EXIST %chemin% ( ###unnecessary###
REM GOTO :erreur_chemin1
REM )
IF NOT EXIST %chemin%\easy-rsa (
GOTO :erreur_chemin2
)
cd %chemin%
GOTO :choix
REM :erreur_chemin1
REM cls
REM echo ### The path %chemin% is not valid. Check for spaces and quotes ###
REM echo.
REM set chemin=
REM GOTO :chemin
:erreur_chemin2
cls
echo ### The path %chemin% is not correct ###
echo.
set chemin=
GOTO :chemin
Regarding your issue, try it like this:
Thanks for helping me anyway!