Computer Hope

Microsoft => Microsoft DOS => Topic started by: Xeneinxe14 on May 21, 2009, 10:40:17 PM

Title: HELP ME PLEASE!!!...IF command
Post by: Xeneinxe14 on May 21, 2009, 10:40:17 PM
I don`t know what`s wrong...
-----------------------------------------------------------
IF EXIST file.ext (GOTO yes) ELSE (GOTO no)
:yes
...
Exit
:no
...
Exit
-----------------------------------------------------------

or the other way...
-------------------------------------------
IF EXIST file.ext GOTO yes
IF NOT EXIST file.ext GOTO no
:yes
...
Exit
:no
...
Exit
-------------------------------------------

Somebody can help me whit these batch`s files...

 :P sorry for my english!
Title: Re: HELP ME PLEASE!!!...IF command
Post by: BatchFileBasics on May 21, 2009, 10:45:27 PM
I don`t know what`s wrong...
-----------------------------------------------------------
IF EXIST file.ext (GOTO yes) ELSE (GOTO no)
:yes
...
Exit
:no
...
Exit
-----------------------------------------------------------

or the other way...
-------------------------------------------
IF EXIST file.ext GOTO yes
IF NOT EXIST file.ext GOTO no
:yes
...
Exit
:no
...
Exit
-------------------------------------------

Somebody can help me whit these batch`s files...

 :P sorry for my english!

well lets look at:
Code: [Select]
IF EXIST file.ext GOTO yes
IF NOT EXIST file.ext GOTO no
:yes
...
Exit
:no
...
Exit

You don't need
Code: [Select]
IF NOT EXIST file.ext GOTO no
since theres no other option
so your looking at
Code: [Select]
IF EXIST file.ext GOTO yes
goto no
so if it exists, goto yes, if it doesn't it will continue the script and goto no

ALSO.
you might want to add in the directory of the file
Title: Re: HELP ME PLEASE!!!...IF command
Post by: Xeneinxe14 on May 21, 2009, 10:53:13 PM
I try it but the IF command don`t work with GOTO
Title: Re: HELP ME PLEASE!!!...IF command
Post by: BatchFileBasics on May 21, 2009, 10:56:27 PM
are you sure your doing it like this?
Code: [Select]
if exist c:\windows\myfile.txt goto yes
goto no

well try this code out, except replace myfile.txt with the name of your file
Code: [Select]
@echo off
if exist c:\windows\myfile.txt goto yes
goto no

:yes
echo Worked
pause
exit

:no
echo didn't work
pause
exit

Title: Re: HELP ME PLEASE!!!...IF command
Post by: Xeneinxe14 on May 21, 2009, 11:05:24 PM
The original .bat be...
-----------------------------------------------
IF EXIST autorun.inf GOTO yes
goto no
:yes
ren ....
del...
exit
:no
echo...
set /p...
...etc...
exit
---------------------------------------------------
but the shell show me something like this
"The command`s syntax is incorrect"
Title: Re: HELP ME PLEASE!!!...IF command
Post by: BatchFileBasics on May 21, 2009, 11:10:17 PM
thats interesting.

well lets see the full error:

open up command prompt
type the full path and name of your batch
c:\path of file\file.bat

and press enter.

and it should say the error then resume to command prompt
Title: Re: HELP ME PLEASE!!!...IF command
Post by: Xeneinxe14 on May 21, 2009, 11:19:00 PM
I don`t understand why appear the message "The syntax of the command is incorrect"...I follow you step to step....fu___ batch >:(
Title: Re: HELP ME PLEASE!!!...IF command
Post by: BatchFileBasics on May 21, 2009, 11:40:08 PM
well there goes my ideas.
the if syntax shouldn't have any problems with exist.

so if anyone else wants to try and help. please
Title: Re: HELP ME PLEASE!!!...IF command
Post by: Dias de verano on May 22, 2009, 12:54:57 AM
(1) If the file is not in the same folder as the batch file you need to use the full path.

(2) If the file name and/or path has one or more spaces you need quote marks before and after it. They do not do any harm if there are no spaces.

(3) Contents of folder:

Code: [Select]
S:\Test\Batch\ifexist>dir
 Volume in drive S is USBHD
 Volume Serial Number is 2C51-AA7F

 Directory of S:\Test\Batch\ifexist

22/05/2009  07:40    <DIR>          ..
22/05/2009  07:40    <DIR>          .
22/05/2009  07:40                 7 test.txt
22/05/2009  07:43               137 test.bat
               2 File(s)            144 bytes

(4) test.bat:

Code: [Select]
@echo off
if exist "test.txt" (goto yes) else (goto no)
:yes
echo file exists
goto end
:no
echo file does not exist
:end
pause

(5) Output on screen:

Code: [Select]
S:\Test\Batch\ifexist>test.bat
file exists
Press any key to continue . . .

(6) I edited test.bat: I changed if exist test.txt to if exist test1.txt (which does not exist)...

Code: [Select]
S:\Test\Batch\ifexist>test.bat
file does not exist
Press any key to continue . . .



Title: Re: HELP ME PLEASE!!!...IF command
Post by: Dusty on May 22, 2009, 02:09:25 AM
Can the OP show which command line is causing the error or is it just assumed that it's the If exist command line?
Title: Re: HELP ME PLEASE!!!...IF command
Post by: Xeneinxe14 on May 22, 2009, 09:28:36 AM
OK tanks everybody...but, for example I have in a directory named "Test" the next files: autorun .inf, autorunH.inf and the batch.
The batch cotains the next commands:
-----------------------------------------------------------------------------------
@echo off
IF exist "Autorun.inf" (GOTO yes) ELSE (GOTO no)
:yes
ren %USERPROFILE%\desktop\test\Autorun.inf Autorun1.inf
ren %USERPROFILE%\desktop\test\AutorunH.inf Autorun.inf
pause
Exit
:no
ren %USERPROFILE%\desktop\test\AutorunH.inf Autorun.inf
pause
Exit
------------------------------------------------------------------------------------
but the batch file doesn`t works cause appear the same message ervery time "The syntax of the command is incorrect"...
What is it OP?
Title: Re: HELP ME PLEASE!!!...IF command
Post by: Dias de verano on May 22, 2009, 10:55:01 AM
1. You didn't take any notice of what I wrote about paths and filenames with spaces in them.

2. The OP is you. Original Poster.