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
    This is Microsoft owns documentation. It can be useful. docs.microsoft.com/en-us/windows-server/administration/… – akane Sep 24 at 14:31
  • 1
    Did you mean that dir /B > someFile.txt will also create a somefile.txt file in the txt file? If yes, I think this is a normal behavior. If you want to know about DIR command, please refer to the following link:howtogeek.com/363639/how-to-use-the-dir-command-in-windows – Candy Sep 25 at 3:25
  • Thanks for your time. Please see the updated question now. I don't want this someFile.txt at the end of the list. This is the file in which I'm writing the list of files. I hope I'm clear and also thanks for the link. @Candy – Ajay Dhiman Sep 25 at 6:58
  • Thanks a lot @akane. – Ajay Dhiman Sep 25 at 6:59
  • Cmd.exe, although it still functions, is pretty much obsolete. Powershell is now the standard command line application on Windows. – kreemoweet Sep 29 at 1:28
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>

| improve this answer | |
  • Thanks for your time. But, dir /B > D:\my\folders\to\go\through\somefile.txt, as you suggested, states The system cannot find the path specified. (because there's no file there with such a name right now). I hope what I did is what you meant. what else can I try next? Thanks again. – JustCurious Sep 28 at 12:30
  • I believe this is related to the directory path do not exists. Not related to that the file, since file will be auto created. I updated the answer. You can check if the path exists by dir D:\my\folders\to\go\through\ . If it do not exist you can either redirect to other directory path that exist or you can create the directory path by mkdir – akane Sep 29 at 0:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.