-1

I am trying to make a program that will give me the tree of my C: drive as a .txt file in batch. the program, when run, prints the following to the text file tree.txt:

C:\>cd C:\ 

C:\>tree /a  1>>tree.txt 

C:\>cd C:\ 

C:\>tree /a  1>>tree.txt 

C:\>cd C:\ 

C:\>tree /a  1>>tree.txt 

this is very strange, as it continues to print indefinitely and prints the entirety of the code to the file instead of the tree. My code is as follows:

cd C:\
tree /a >> tree.txt

any ideas? Thanks in advance!

roger
  • 55
  • 1
  • 8
  • You called the script `tree.bat`, didn't you? – SomethingDark Oct 05 '16 at 20:36
  • um... do I have to? – roger Oct 05 '16 at 20:43
  • 1
    No. In fact; you shouldn't, since calling the script `tree.bat` means that the `tree` command in your script tells CMD to run the script instead of the command you meant to run since CMD looks in the current directory before it checks the directories listed in the `%PATH%` variable. I was trying to make sure you didn't have the script accidentally calling itself, which is the only possible place the infinite loop could have come from. – SomethingDark Oct 05 '16 at 20:44
  • I just used the two lines of code above and opened the file manually... – roger Oct 05 '16 at 20:46
  • 1
    What is the name of the script? If that's you entire code, there's no reason for the script to run in an infinite loop unless you named it `tree.bat`. – SomethingDark Oct 05 '16 at 20:47
  • oh, it is named tree.bat... should renaming it fix the problem? – roger Oct 05 '16 at 20:51
  • Yes, call it anything else and that will fix the infinite loop problem. Also, your code is showing because you didn't put `@echo off` at the top of your script. – SomethingDark Oct 05 '16 at 20:57
  • I renamed it and it works! thank you! – roger Oct 05 '16 at 21:00
  • I recommend to write `cd /D C:\ ` instead of `cd C:\ ` to even change from another drive... After having renamed `tree.bat` to something else, the command echoes are no longer written to the file but only displayed in the console window, so `@echo off` is not mandatory... – aschipfl Oct 06 '16 at 08:24

2 Answers2

0

You have two problems: your code being displayed while it runs and your code running in an infinite loop.

1. YOUR CODE IS BEING DISPLAYED
By default, batch displays each line of code that it runs before that line of code is run.

To disable this, put the command @echo off at the top of your script. echo off disables this behavior, and the @ prevents that line from being displayed.

2. YOUR CODE IS RUNNING IN AN INFINITE LOOP
Since Your code consists of nothing but the two lines in your question, the only possible reason for your code to be running in an infinite loop is that your script is named tree.bat.

Your script name is causing an infinite loop because of how CMD runs commands. When you call a command and do not provide the full path to that command, batch first looks in the current directory, and then in all of the directories listed in the %PATH% variable. Because there is an executable in the current directory called tree (your script), batch assumes that you are trying to call that and runs your script again, causing an infinite loop.

To stop this behavior, rename your script, making sure not to call it anything that is already a batch keyword (a list of batch keywords can be found here).

SomethingDark
  • 13,229
  • 5
  • 50
  • 55
0

If you really, really want to name your batch file tree, you can set the pathext var to .com.

@echo off & setlocal
set pathext=.com
tree C:\ /a>>tree.txt

Then you wouldn't be recursively calling your batch file.

Or, just run this command and don't use a batch file:

tree C:\ /a>>tree.txt
soja
  • 1,587
  • 8
  • 8