Bat file keeps repeating at command line

Posted on 2010-01-06
Last Modified: 2012-05-08
I'm not sure completely how to explain this, but I have a probelm in that I am trying to create a bat file that defrags the hard drive of a computer. I think I have the syntax down correctly:

defrag c:

and I name the file defrag.bat. When I double click on it to run it, it pulls up the command window as you would expect, but then it just infinately repeats the command c:\defrag c: over and over but doesn't actually do anything. The only way to get it to stop is to close the command window. I could swear I have resolved this once before by moving the location of the bat file to a different location other than the root of the c: drive. I tried doing this to resolve and it didn't make a difference. Could someone give me some advice on how to fix this?

Question by:Rob Sanders
    LVL 67

    Accepted Solution

    Since your batch file is called defrag, it is called by defrag.cmd. You can get around that by either renaming your batch file to e.g. dfrg.cmd, or by using:

    defrag.exe c:

    BTW, the exit is superflous.
    LVL 16

    Assisted Solution

    Yep, as qlemo points out, your batch file is named "defrag" (forget the .bat extension for now) and DOS's defrag command is also named defrag (albeit with an .exe extension).

    Therefore, when you run your batch file by typing DEFRAG C:, the first thing you batch file does is run DEFRAG C: again. In other words, it runs another copy of itself again. In turn, that one runs yet another copy. And so the cycle never ends.

    As qlemo points out, the difference between both DEFRAGs is their extension name. So, simply changing you batch file to:

       defrag.exe c:

    would remedy your problem.

    The batch file is almost triovially superflous. I'm not referring to the EXIT command as qlemo has already covered that one, I'm referring to the fact that the only difference between running DOS's defrag command and running your batch file is not having to specify the 'c:' part as in the following examples:

       defrag c:      <-- DOS command

       defrag          <-- Your batch file

    Not a great saving in terms of batch processing..... but a good start for a beginner!
    LVL 1

    Author Closing Comment

    by:Rob Sanders
    Thanks for the help guys, that was exactly the right answer.

    Featured Post

    Live: Real-Time Solutions, Start Here

    Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

    Join & Write a Comment

    This article was inspired by a question here at Experts Exchange ( The requirements stated in that question are (1) reduce the file size of a large number of…
    I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
    This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
    In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now