Batch IF ELSE commands help please

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tym1200
Posts: 1
Joined: 16 Oct 2009 22:56

Batch IF ELSE commands help please

#1 Post by tym1200 » 16 Oct 2009 23:00

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

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

#2 Post by !k » 17 Oct 2009 08:46

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? ;)

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#3 Post by avery_larry » 19 Oct 2009 15:19

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.

Post Reply