Buffering the output of a "dir" command


  1. Posts : 149
    Windows 7 Ultimate x64
       #1

    Buffering the output of a "dir" command


    I had a command prompt open in a directory that contained 8 files. I wanted to keep an inventory of the 8 files, so I ran the command:

    C:\>dir > inventory.2015-06-03

    When I looked at the contents of inventory.2015-06-03 I was surprised to find that it contained the inventory.2015-06-03 file itself, as a 0 byte file.

    Is there a technique to buffer the output from the "dir" command before the ">" takes effect, so I don't get the extra file in my listing?
      My Computer


  2. Posts : 5,656
    Windows 7 Ultimate x64 SP1
       #2

    You can try:
    dir | findstr /v /i "\myfile.txt$" > myfile.txt

    in case symbols don't make it
    Buffering the output of a "dir" command-dir-exclude-file-write-txt.jpg
      My Computer


  3. Posts : 149
    Windows 7 Ultimate x64
    Thread Starter
       #3

    Thanks - your example works perfectly (of course).

    It's interesting to find the use of Regular Expressions in a Windows context. For a moment I thought I'd strayed into a Linux discussion.
      My Computer


  4. Posts : 5,656
    Windows 7 Ultimate x64 SP1
       #4

    :) I don't know much about switches and whatnot, searched google with: cmd dir exclude files

    this came up:
    Excluding files of particular extension using DIR command on windows command line - Super User

    then added your "> text.file" part to it, tried in my PC, posted for you
      My Computer


  5. Posts : 6,285
    Windows 10 Pro X64
       #5

    No regular expression there: dir | findstr /v /i "\myfile.txt$" > myfile.txt

    In english this says:

    Display the list of files and folders in the current directory.
    Pipe the output to the find string command. Make it case insensitive and exclude the file myfile.txt$ ($ meaning anything)
    then redirect the output to myfile.txt.
      My Computer


  6. Posts : 721
    Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
       #6

    Ztruker said:
    No regular expression there: dir | findstr /v /i "\myfile.txt$" > myfile.txt
    The text in red is a regular expression. At least it's supposed to be.

    GokAy, your regex, \myfile.txt$, is slightly incorrect.


    Recalling a few special character meanings in regular expressions:
    • "\" is an escape character,
    • "." represents any single character,
    • "$" at the end of a regex means that the string to match should only be matched at the end of a line (immediately before any line break).

    So the regex \myfile.txt$ actually says to match the string literal myfile, any character, and the string literal txt. $ at the end of a regex says that the string to match must be at the end of a line, before a line break character. Escaping the literal character m doesn't do anything.

    This particular regular expression can be expressed in wildcard notation too. Its wildcard notation would be myfile?txt (where ? in wildcard notation represents any single character). This, however, will not say to match at the end of a line.

    The correct regular expression that should be used in the Findstr command should be myfile\.txt$.


    I presume you've already realised this by now, but there is no way to extend the output buffer of commands, Swiftie. GokAy's solution of filtering the output of Dir is exactly how your problem should be tackled.
      My Computer


  7. Posts : 149
    Windows 7 Ultimate x64
    Thread Starter
       #7

    Thanks to all for the help offered. It was the "$" in "\myfile.txt$" that showed me it was a regular expression. I hadn't noticed that the "\" and the "." were not quite right, but I no longer use regular expressions much since I retired (from IBM)
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 04:11.
Find Us