0

Using already an answer here

And it works with just one difference. Running dir /B returns all the file's names correctly.

Now when I run dir /B > someFile.txt it creates a .txt file but also append the name someFile at the end of the file.

Please see this

This behaviour is just confusing me a bit. This means that it creates the .txt file before listing the files name.

Shouldn't it be the other way around? Please enlighten.

Also, I find cmd very intriguing and want to learn more about it and it's commands. Can you please refer some guides/articles/docs for the same. That would be really helpful. Thanks :)

1 Answers1

0

I think easiest way to get rid of the someFile.txt inside it's self is to give an absolute or relative path to another directory.

For example, let's say you run dir inside C:\Users\bosse. Then you can choose to locate the file somewhere else outside this directory and it will not be included. Let's say in your C:\tmp directory if you have.

Using absolute path:
C:\Users\bosse>dir /B > C:\tmp\someFile.txt

Using relative path:
C:\Users\bosse>dir /B > ..\..\tmp\someFile.txt

In these case when you open C:\tmp\someFile.txt it shall not contain it's own name.

Here you can read more absolute and relative path in case you are not familiar with that.
Windows file path formats

Even though the the target file do not need to exist and will be created, please notice that the directory path must exist in advance.
dir can be used to check path exists.
dir [<drive>:]<path>
If directory path do not exist it can be created by mkdir.
mkdir [<drive>:]<path>

akane
  • 234