Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i have a simple bat file that list directory files only.

if exist "output.txt" del "output.txt" 
dir /a /b /-p /O:N >>output.txt

when text file is created it also adds the bat file and the output.txt entries in the text file. I would like to remove those 2 entries to make the final list clean.

Thanks and hope i explained it correctly

share|improve this question
add comment

3 Answers

up vote 1 down vote accepted

Assuming that the batch file is named list.bat, with the help of findstr you can exclude the two files from the dir output.

Replace the second line with

dir /a /b /-p /O:N |findstr /vi output.txt|findstr /vi list.bat >>output.txt

BTW, the if exist... line is useless as the file output.txt is re-created by the dir command redirection before it starts to list the files.

share|improve this answer
add comment

Here is a small improvement over the Seki answer:

1) Use overwrite redirection instead of append

2) Eliminate both files using a single FINDSTR

3) Use %~nx0 to get the name of the running batch file

@dir /a /b /-p /o:n|findstr /vi /c:"output.txt" /c:"%~nx0" >output.txt
share|improve this answer
add comment

The version below doesn't require FINDSTR, so it is faster:

@echo off
(for %%f in (*.*) do if "%%f" neq "%~NX0" if "%%f" neq "output.txt" echo %%f) > output.txt
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.
{"cc_re__cc_re__cc_by_sa_3_0": ["        <li><a href=\"http://area51.stackexchange.com\" title=\"proposing new sites in the Stack Exchange network\">Area 51</a></li>\r\n                        <li><a href=\"http://careers.stackoverflow.com\">Stack Overflow Careers</a></li>\r\n                    \r\n                </ol>\r\n            </td>\r\n    </tr>\r\n</table>\r\n                </div>\r\n            </div>\r\n\r\n            <div id=\"copyright\">\r\n                site design / logo &#169; 2013 stack exchange inc; user contributions licensed under <a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" rel=\"license\">cc-wiki</a> \r\n with <a href=\"http://blog.stackoverflow.com/2009/06/attribution-required/\" rel=\"license\">attribution required</a>\r\n            </div>\r\n            <div id=\"footer-flair\">\r\n                <a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" class=\"cc-wiki-link\"></a>\r\n            </div>\r\n            <div id=\"svnrev\">\r\n                rev 2013.12.13.1225\r\n            </div>\r\n            \r\n        </div>\r\n    </div>\r\n    <noscript>\r\n        <div id=\"noscript-warning\">Stack Overfl", "ol>\r\n            </td>\r\n    </tr>\r\n</table>\r\n                </div>\r\n            </div>\r\n\r\n            <div id=\"copyright\">\r\n                site design / logo &#169; 2013 stack exchange inc; user contributions licensed under <a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" rel=\"license\">cc-wiki</a> \r\n with <a href=\"http://blog.stackoverflow.com/2009/06/attribution-required/\" rel=\"license\">attribution required</a>\r\n            </div>\r\n            <div id=\"footer-flair\">\r\n                <a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" class=\"cc-wiki-link\"></a>\r\n            </div>\r\n            <div id=\"svnrev\">\r\n                rev 2013.12.13.1225\r\n            </div>\r\n            \r\n        </div>\r\n    </div>\r\n    <noscript>\r\n        <div id=\"noscript-warning\">Stack Overflow works best with JavaScript enabled<img src=\"http://pixel.quantserve.com/pixel/p-c1rF4kxgLUzNc.gif\" alt=\"\" class=\"dno\"></div>\r\n    </noscript>\r\n<script type=\"text/javascript\">var p = \"http\", d = \"static\"; if (document.location.protocol == \"https:\") { p += \"s\"; d = \"engine\"; } va"], "copyright__copyright__copyright": [" /a /b /-p /o:n|findstr /vi /c:\"output.txt\" /c:\"%~nx0\" >output.txt\nshare|improve this answer\nadd comment\n\nThe version below doesn't require FINDSTR, so it is faster:\n\n@echo off\n(for %%f in (*.*) do if \"%%f\" neq \"%~NX0\" if \"%%f\" neq \"output.txt\" echo %%f) > output.txt\nshare|improve this answer\nadd comment\n\nYour Answer\n\n\u00a0\ndiscard\n\nBy posting your answer, you agree to the privacy policy and terms of service.\n\nNot the answer you're looking for? Browse other questions tagged or ask your own question."]}