0

I want to run command if the folder is exist, else i will execute another command

I tried below one

if exist "c:\program files\my File name\"
(   
   \\my commands...
)
else
(
   \\my commands...
)

but it doesn't work

Note: folder name having spaces like "my file name"

4
  • 3
    The ELSE needs to be on the same line as both parentheses. ) else ( The opening parentheses also needs to be on the same line as the IF. This is clearly defined syntax n the help file for the IF command. RTM Commented Jul 10, 2018 at 11:38
  • @Squashman you mean it should be in single line?. because i have multiple command lines inside if parantheses, give me sample if you can Commented Jul 10, 2018 at 11:43
  • 1
    @mohamedfaisal, if you're checking for directory existence, you end it with a backslash: If Exist "C:\Program Files\my folder name\" (. However your question suggests you're checking existence of a file, so remove that trailing backslash: If Exist "C:\Program Files\my file name including any extension" ( Commented Jul 10, 2018 at 12:20
  • 1
    What's the point of implementing the solution into your question? I rolled it back, because otherwise, the whole thread wouldn't make sense... Commented Jul 10, 2018 at 15:44

2 Answers 2

1

Straight from the HELP file.

IF EXIST filename. (
    del filename.
) ELSE (
    echo filename. missing.
)

OR

IF EXIST filename. (del filename.) ELSE echo filename. missing
Sign up to request clarification or add additional context in comments.

3 Comments

can you see my updated one. I tried the same way. it's just popping and disappeared. but not run the command
@mohamedfaisal, How am I supposed to know if the path is correct. I don't have access to your computer to verify.
@mohamedfaisal, learn to debug your scripts. You start by opening a command prompt and executing the batch script from the command prompt. Do not run it with your mouse. Also, if you have @ECHO OFF at the top of your script, change that to @ECHO ON.
1

Maybe this will help. Its unprofessional but it works for me.

@ECHO OFF
CLS
IF EXIST C:\batch\logs;C:\backup\logs (
echo THE DIRECTORY HAS BEEN MADE
) ELSE (
md C:\batch\logs md C:\backup\logs echo Directory created)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.