{batch} IF NOT EXIST <mon dossier> ne fonctionne pas

Solved
Tinouboom Posted messages 12 Status Member -  
barnabe0057 Posted messages 14329 Registration date   Status Contributor 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

3 answers

  1. barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention   4 930
     
    Hello,

    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.”
    0
    1. Tinouboom Posted messages 12 Status Member
       
      Hello,

      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 folder is indeed in the location specified by the user (I think :erreur_chemin1 is ultimately unnecessary as it is included in :erreur_chemin2), except that even the default value doesn't work and redirects to :erreur_chemin2.
      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
      0
    2. barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention   4 930 > Tinouboom Posted messages 12 Status Member
       
      When you post code, you need to use syntax highlighting tags.

      Regarding your issue, try it like this:
      set chemin="%ProgramFiles%\OpenVPN" 
      0
    3. Tinouboom Posted messages 12 Status Member > barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention  
       
      How to do it? Sorry, first time on the forum.
      0
    4. Tinouboom Posted messages 12 Status Member
       
      It doesn't work...

      Thanks for helping me anyway!
      0
    5. barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention   4 930 > Tinouboom Posted messages 12 Status Member
       
      When you write a message, there are the tags in the corner on the right, it's the one with the symbols <>
      0
  2. barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention   4 930
     
    In your place, instead of letting the user enter the OpenVPN path, I would fetch it myself:

    cd "%ProgramFiles(x86)%"
    for /f "tokens=* " %%A in ('dir /b /ad /s "*.*" ^| findstr /i "easy-rsa"') do (set chemin=%%A)


    “Artificial intelligence is defined as the opposite of natural stupidity.”
    0
    1. Tinouboom Posted messages 12 Status Member
       
      Great!

      I didn't understand everything about the order, but I'll look into it. However, "path" went all the way to the subfolder contained in "easy-rsa"!

      Thank you very much, I mark it as RESOLVED.
      0
  3. barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention   4 930
     
    Does it work like this :

    :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.”
    0
    1. Tinouboom Posted messages 12 Status Member
       
      But actually it only works if the user installed it in Program Files (x86), right?
      Is there a way to search the folder throughout the entire hard drive?
      0
      1. barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention   4 930 > Tinouboom Posted messages 12 Status Member
         
        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.
        0