1

Somewhere i a batch file i have a piece of code that checks if a directory exists. If not then ask the user what to do. Now the strange thing is then when the directory exists i still get the question "Do you want to create the directory?" which is inside the if statement.

What am i missing here?

cls
@echo off

SET PanterPath="W:\Windows\Panther\"

if NOT exist %PanterPath% (
    echo Directory %PanterPath% not found! Cannot copy and apply unattend file.
    SET /P var=Do you want to create the directory? (Y/N)
    if /I "%var%" EQU "Y" (
        echo Creating directory %PanterPath% ...
        mkdir %PanterPath%        
    ) else (
        echo Directory %PanterPath% not created so script cannot continue. The image deployment failed!    
        pause    
        exit
    )    
)
pause
pause

EDIT When i add a else statement to the If Exists then the output is:

Directory "W:\Windows\Panther" found

Directory "W:\Windows\Panther" not created so script cannot continue. The image deployment failed

Press the any key...

Code:

SET PanterPath="W:\Windows\Panther"

If Exist "%PanterPath%\" (
    echo Directory %PanterPath% found!
) else (
    echo Directory %PanterPath% not found! Cannot copy and apply unattend file.
    SET /P var=Do you want to create the directory? (Y/N)
    if /I "%var%" == "Y" (
        echo Creating directory %PanterPath% ...
        mkdir %PanterPath%        
    ) else (
        echo Directory %PanterPath% not created so script cannot continue. The image deployment failed!    
        pause    
        exit
    )    
)
pause
pause
aschipfl
  • 33,626
  • 12
  • 54
  • 99
Gforse
  • 323
  • 3
  • 20
  • `if NOT exist C:\Windows echo doesn't exists` – Noodles May 29 '19 at 07:52
  • Thanks for the tips. I tried to adjust it but still no luck. I even adjusted the code to contain a else statement in the `if exists` and the strange thing is both condition are handles of the If block – Gforse May 29 '19 at 08:28
  • did you read [Variables are not behaving as expected](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected) as Compo suggested? – Stephan May 29 '19 at 08:34
  • Aside from the code snippets I provided, **of which you've only used half** in your edit, the link I provided is where your solution lay, **please read the duplicate answer**. `Set "PanterPath=W:\Windows\Panther"` and `MD "%PanterPath%"` are still missing. I would also advise that when you're expecting known responses to questions, as in this case, that you use consider using `choice.exe` over `Set /P` too. – Compo May 29 '19 at 08:59

0 Answers0