1

I am trying to see if a specific folder exists as a sub folder in a certain path.

Assume a path given in a variable InputPath and a relative path given RelPath.  How can I write a block to look if a given folder name (given by a variable FolderName) exists in that path?

In that path means that if I do dir in the path InputPath\RelPath, I want to check if FolderName is one of the folders listed.

I know dir /d can list only directories, but I don't know how to search that list.

To make an example by a batch file:

@echo off

SET INPUT_PATH="D:\SomeFolder\OtherFolder\RandomFolder\"
SET REL_PATH="..\FunnyFolder"
SET FOLDER_NAME="SearchMeFolder"

goto IsFolder

:IsFolder
SET CURR_PATH=%CD%
cd "%INPUT_PATH%\%REL_PATH%"
:: Need to fetch all sub folder and check for existence
:: If exist, set FOLDER_EXIST to True, else False
7
  • Please note that superuser.com is not a free script/code writing service. If you tell us what you have tried so far (include the scripts/code you are already using) and where you are stuck then we can try to help with specific problems. You should also read How to Ask.
    – DavidPostill
    Commented May 30, 2021 at 13:33
  • @DavidPostill, I look at related questions and all of them are "How to..." in the context of a script. Moreover, Have you seen my record on other sites? Do you really think I need to see how to ask? Could you tell me what's not clear about the question?
    – Royi
    Commented May 30, 2021 at 16:24
  • Read my first comment. You haven't provided any attempt at all to solve it yourself.
    – DavidPostill
    Commented May 30, 2021 at 16:41
  • 2
    Your record on other sites is irrelevant.
    – DavidPostill
    Commented May 30, 2021 at 16:42
  • I wrote my try. I think it will be nice to have some credit and patience towards beginners. All related question are the same with no try at all.
    – Royi
    Commented May 30, 2021 at 17:04

2 Answers 2

1

Batch file programming is messy.  I can’t find any one solution that works perfectly.  But this three-pronged approach seems to work:

SET CURR_PATH=%CD%

:: Assume/anticipate failure
set "FOLDER_EXIST=False"

cd /d "%INPUT_PATH%\%REL_PATH%" 2> nul  &&  (
    if exist  %FOLDER_NAME%\    set "FOLDER_EXIST=True"
    if exist "%FOLDER_NAME%\"   set "FOLDER_EXIST=True"
    cd %FOLDER_NAME% 2> nul &&  set "FOLDER_EXIST=True"
)
cd /d %CURR_PATH%
echo FOLDER_EXIST = %FOLDER_EXIST%.

Note that the if statements are testing the (candidate) folder name followed by a \.  For example,

    if exist C:\Windows\debug
    if exist C:\Windows\debug\

and

    if exist C:\Windows\notepad.exe

should all succeed, but

    if exist C:\Windows\notepad.exe\

should fail, because C:\Windows\notepad.exe is not a folder.


    if exist  %FOLDER_NAME%\    set "FOLDER_EXIST=True"

will fail if %FOLDER_NAME% contains space(s) and is not quoted; e.g., set "FOLDER_NAME=Program Files".

    if exist "%FOLDER_NAME%\"   set "FOLDER_EXIST=True"

will fail if %FOLDER_NAME% contains space(s) and is quoted; e.g., set FOLDER_NAME="Program Files".  And

    cd %FOLDER_NAME% 2> nul &&  set "FOLDER_EXIST=True"

will fail if you don’t have permission to cd into %FOLDER_NAME%.

1

One line version using operator && or || if exist/not exist:

>nul 2>&1 pushd "%cd%\my_Folder" &&(popd & set "Bool=True")|| set "Bool=False"

set Bool & echo=%Bool%

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .