Avatar of missymadi
missymadi

asked on 

What is the correct syntax for a DOS Batch file If Else command?

Experts,

      I am trying to check to see if a file exists using a DOS batch file. The code I have is :
IF EXISTS C:\woo.txt (erase C:\woo.txt) ELSE ECHO woo.txt is missing >>"C:\IASCRIPT\OracleIA.out"

When I run the batch file DOS doesnt execute the line of code??

I tried this also:
@Echo
pause      
IF EXISTS "C:\woo.txt" (goto :yes) ELSE goto :no
pause
:Yes
del c:\woo.txt
:no
echo "missing file"

this did not work either.

Thanks, MissyMadi
Microsoft DOS

Avatar of undefined
Last Comment
missymadi
Avatar of dacselat
dacselat

Try this:

IF EXIST filename. (del filename.) ELSE echo filename. missing
Avatar of missymadi
missymadi

ASKER

I tried your example in a separate batch file:
IF EXIST C:\woo.txt (del C:\woo.txt) ELSE echo C:\woo.txt missing >>"C:\IAScript\OracleIA.out"

But when I put it in my original script, DOS doesn't execute??


@Echo on
CD..
CD..
CD..
CD..
REM Create xcopy to place xcacls.exe in c: root :xcopy "D:\xcacls.exe" C:\IAHOLD
If not exist C:\IASCRIPT goto make
If exist C:\IASCRIPT\OracleIA.out goto DELOLD
REM start /wait sqlplus SYSTEM/scdsadmin@CDSPROD '@C:\IAScript\ORACLEIA.sql'
IF exist C:\woo.txt (del C:\woo.txt) Else echo woo.txt missing 
 
Goto Permission
Goto END
 
:make 
set Echo on 
md c:\IASCRIPT
"IASCRIPT directory created" >>"C:\IAScript\OracleIA.out"
 
:DELOLD
del "C:\IASCRIPT\OracleIA.out" 
 
 
 
:Del
rem @Echo V0003804- Delete ODBCTRAC.dll  >>"C:\IASCRIPT\OracleIA.out" 
rem del C:\WINDOWS\system32\odbctrac.dll >>"C:\IASCRIPT\OracleIA.out"
rem @Echo "C:\WINDOWS\system32\odbctrac.dll" has been deleted >>"C:\IASCRIPT\OracleIA.out"
@Echo "C:\Woohoo1.txt" has been deleted >>"C:\IASCRIPT\OracleIA.out"
 
 
:Permission
@Echo V0003851- Protect TMDB UDUMP file from Unauthorized Access >>"C:\IASCRIPT\OracleIA.out" 
XCACLS C:\xcacls.txt /G Administrator:F >>"C:\IASCRIPT\OracleIA.out" 
@Echo "XCACLS.txt - Full Admin rights added" >>"C:\IASCRIPT\OracleIA.out" 
XCACLS c:\xcacls.txt /E /G Users:RXC >>"C:\IASCRIPT\OracleIA.out"
@Echo "XCACLS.txt - Read/Execute User rights added" >>"C:\IASCRIPT\OracleIA.out" 
XCACLS c:\xcacls.txt /E /G Bubba1:R >>"C:\IASCRIPT\OracleIA.out"
@Echo "XCACLS.txt - Read Bubba1 rights added" >>"C:\IASCRIPT\OracleIA.out" 
 
:END
@Echo "ORACLEIA Batch File is now complete" 
pause
spool off

Open in new window

Avatar of dacselat
dacselat

It works, just add the ECHO or @ECHO before "IASCRIPT directory created .......

Avatar of AmazingTech
AmazingTech

EXIST not EXISTS

IF EXIST C:\woo.txt (erase C:\woo.txt) ELSE ECHO woo.txt is missing >>"C:\IASCRIPT\OracleIA.out"
ASKER CERTIFIED SOLUTION
Avatar of AmazingTech
AmazingTech

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of missymadi
missymadi

ASKER

Thanks, great example.

What is the difference between del and erase commands?
Also, does it matter whether you have spaces between commands? Or is using REM statements better for spacing?
Avatar of AmazingTech
AmazingTech

DEL and ERASE are the same. Just like CD and ChDir or RD and RmDir.

No problems with REMs or spaces(blank lines).
IF exist C:\ (
  ECHO Start




  REM HELLO
  ECHO HI.


) ELSE (

  ECHO Not exist.

)
Avatar of dacselat
dacselat

Title of the question: "What is the correct syntax for a DOS Batch file If Else command?"
Avatar of missymadi
missymadi

ASKER

I just did a small test batch file but cannot get it to del test.txt, any ideas?

@ECHO on
If exist C:\test.txt (
del C:\test.txt
) ELSE(
@ECHO C:\test.txt not found>>"C:IASCRIPT\OracleIA.out"
@ECHO %date%,%time%,C:\IASCRIPT\OracleIA.out not found>>C:\woo.log
Avatar of AmazingTech
AmazingTech

) ELSE( needs a space after the E.
Needs the ending ) to encapsulate everything.

@ECHO on
If exist C:\test.txt (
del C:\test.txt
) ELSE (
@ECHO C:\test.txt not found>>"C:IASCRIPT\OracleIA.out"
@ECHO %date%,%time%,C:\IASCRIPT\OracleIA.out not found>>C:\woo.log
)
SOLUTION
Avatar of dacselat
dacselat

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of missymadi
missymadi

ASKER

I found the problem. I edited the text and forgot a backslash
Microsoft DOS
Microsoft DOS

Microsoft Disk Operating System (MS-DOS) was an operating system for x86-based personal computers, and traces of it are still found in the Windows operating system. DOS is still used in some embedded systems and for certain legacy 16-bit networks.

14K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo