Page 1 of 1

Batch IF ELSE commands help please

Posted: 16 Oct 2009 23:00
by tym1200
I'm in high school and working on making a batch file that does this:

Navigate to windows directory

Look for a file named problem.txt

if it exists, rename it to problem.old and display this message: File has been found and renamed

if it does not exist, display this message: problem.txt is not found
and change the working directory to c:\users using a relative path



This is what I have so far, and i just can't get it to work, very frustrating since this is my first time doing DOS.


@echo off
cd \
cd windows
IF EXIST problem.txt (GOTO :true) ELSE GOTO :false

:TRUE
rename c:\problem.txt problem.old
echo File has been found and renamed

:False
echo problem.txt is not found

Posted: 17 Oct 2009 08:46
by !k
cd windows
IF EXIST problem.txt ...
rename c:\problem.txt ...

You need to determine where there really is problem.txt — c:\problem.txt or c:\windows\problem.txt
Or is it 2 different files? ;)

Posted: 19 Oct 2009 15:19
by avery_larry
You need a goto :eof statement in your :true area or else the :false stuff will also execute.

Don't forget to make sure you're on the correct drive.

c:
cd \windows

-- or --

cd /d c:\windows

Of course, are you supposed to use the %windir% variable to determine the windows folder? Wouldn't make sense with the requirement of using a relative path.

For relative path, you're going to have to figure out how to use the ".." directory.