0

I have a batch file script that needs to verify if the directory that the user enters in is a real directory or path so that I can create it if it dose not exist.

:GetDir
set /p Dir=
if not <is directory?> %Dir% goto:GetDir
if not exist %Dir% mkdir %Dir%

How can I determine if the value given by the user is a directory? There are millions of things that the user could enter in that is obviously not a directory.

notes for editor: This note is to be removed after the editor reads it. I apologize ahead of time if I was not supposed to add this note in. Please correct me so I don't do this again if this is the case and I would appreciate being told where to add these types of notes if a similar situation comes up again. I am trying to maintain and recover this question from when I originally asked it in order to fix my bad reputation for asking bad questions on here. This is not a duplicate question because the marked duplicate is in a different scripting language that I am unfamiliar with. I would also like to mention that I did to some heavy research before asking this question on the the issue that I was having. It would also be greatly appreciated if answering this question could be enabled again so that I can answer my own question because I am not sure that anybody else is going to have the answer, and I currently know how to answer it.

  • What's your question? – numentar Apr 01 '16 at 20:39
  • 1
    What do you mean "without the directory existing?" If it doesn't exist, it's not a directory. – SomethingDark Apr 01 '16 at 20:54
  • You can use `if not exist` to test if something is a directory/file, or do you mean test if it is a valid name? – Dennis van Gils Apr 01 '16 at 20:54
  • I would like to apologize to the community and the moderators for not maintaining this question. I personally would prefer that this is removed because it would be a lot less work on my end. I no longer need an answer to this because I was working in the wrong language to do what I expected. I am going to try to rewrite this question and explain what I was originally trying to do and answer it. – 中野春子 Apr 28 '16 at 19:00

4 Answers4

1

How do I test if a file is a directory in a Batch script ?

@echo off
set VAR="C:\Program Files"
FOR %%i IN (%VAR%) DO IF EXIST %%~si\NUL ECHO %VAR% It's a directory 
pause
Community
  • 1
  • 1
Hackoo
  • 18,337
  • 3
  • 40
  • 70
1

You can use this to test if it is specifically a directory (and not a file)

@echo off
set "file=C:\someDirectory"
if exist "%file%\." echo directory
pause
Dennis van Gils
  • 3,487
  • 2
  • 14
  • 35
0

Here's a way (two ways, really) to determine if a directory exists, and that it's actually a directory and not a file.

set /p "ServerMods=Enter a Directory: "

REM First, make sure the input ends with a backslash (\)
if %ServerMods:~-1% NEQ "\" set "ServerMods=%ServerMods%\

REM Option 1
if not exist %ServerMods% goto :GetDir

REM Option 2
dir %ServerMods% 1>nul 2>nul || (goto :GetDir)
Wes Larson
  • 1,042
  • 8
  • 17
-1
if exist "%ServerMods%\." goto GetDir