Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1881
  • Last Modified:

Bat file keeps repeating at command line

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:
exit

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?

tbone
0
Rob Sanders
Asked:
Rob Sanders
2 Solutions
 
QlemoC++ DeveloperCommented:
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:
exit

BTW, the exit is superflous.
0
 
t0t0Commented:
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:
   exit

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!
0
 
Rob SandersAuthor Commented:
Thanks for the help guys, that was exactly the right answer.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now