Jump to content

Output Results of Dir /B command in Real Time

Value of this question  

2 members have voted

  1. 1. Is this question worthy of being asked?

    • Yes, interesting question.
    • No, this person should be shot from a cannon into space.

Recommended Posts


Obscure and painful question here, sorry.

I take the output of a dir /b command and I then roll it up into a string:

#include <Constants.au3>

$_pid = Run(@ComSpec & " /c dir " & $path & "*.ext /B", @SystemDir,@SW_HIDE,$STDERR_CHILD + $STDOUT_CHILD)

While 1
   $line = StdoutRead($_pid)          ;read contents of STDOUT into $line
   if @error Then ExitLoop            ;if read fails, its the end of the list
   $answer &= $line                   ;concatenate all results from line

Is there a way I can grab the output of the dir command line by line as I'd like to dump it into a Label in my gui in lieu of a progress bar. I have long since given up on getting any kind of meaningful progress bar for this action.

The dir listing is coming from a network share with a huge amount of text files and it takes so long to retrieve the list of files, it makes the program feel like it's frozen. ( i know i could use a marquee progress but they're not very meaningful and make me suspicious that a program has hung when one shows).

The output as it's being stitched together in the while loop is only three lines. Two empty ones and one colossal one of the entire listing.

Any help would be greatly appreciated.

Link to post
Share on other sites

Maybe something like this:

#include <Constants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiEdit.au3>
#include <WindowsConstants.au3>

$hGUI = GUICreate("Test", 500, 700)
$idEdit = GUICtrlCreateEdit("", 0, 0, 450, 700,  BitOR($ES_AUTOVSCROLL, $WS_HSCROLL, $WS_VSCROLL))
GUICtrlSendMsg($idEdit, $EM_LIMITTEXT, -1, 0)

$path = @WindowsDir & "\"
$_pid = Run(@ComSpec & " /c dir " & $path & "*.exe /B / s", @SystemDir,@SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
$answer = ""

While 1
   $line = StdoutRead($_pid)          ;read contents of STDOUT into $line
   if @error Then ExitLoop            ;if read fails, its the end of the list
   _GUICtrlEdit_AppendText($idEdit, $line)
   $answer &= $line





Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to post
Share on other sites

OK, I've stopped being so lazy and made an effort to solve this myself. The problem I had was that StdOutRead wasn't grabbing the output of the dos dir command line by line but returning it in random sized chunks and I need to put it into a label so all my progress messages are consistent.

I'm pulling out a list of the files with extension EXT in a folder with more than 60,000 text files (on the worst of the servers) and it takes so long just to get the dir listing it feels like the program has stalled out.

Would you guys mind having a look at my code below and letting me know if there's a better way.

#include <Constants.au3>
#include <Array.au3>

$gui_My = GUICreate("Show Dir Output as Evidence of Activity", 700, 100, 200, 150)
$lbl_Output = GUICtrlCreateLabel("Label1", 10, 10, 500, 40)

$path = "\\SERVERNAME\folder\folder\folder\"
$_pid = Run(@ComSpec & " /c dir " & $path & "*.ext /B / s", @SystemDir,@SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)
$answer = ""

$loops = 0
While 1
    $line = StdoutRead($_pid)          ;read contents of STDOUT into $line, grabs random
    if @error Then ExitLoop            ;sized chunks of dir's output, not individual lines..why?
    $show_this = StringSplit($line,@CRLF)   ;split this chuck of dir's output into an array
    for $x = 1 to $show_this[0]         ;step thru this chunk of output to display each
        if $show_this[$x] <> "" then    ;ignore blank lines in the output
            $loops += 1     ;increment loop counter to show number progress, not flashing paths
            GUICtrlSetData($lbl_Output,"Compiling file list: (" & $loops &") " & $show_this[$x])
   $answer &= $line         ;this is the actual list of file names that will be processed later
Link to post
Share on other sites

when you process the file are you planning on converting it to an array for processing?

If you are then you can simply redirect the output to a temp file and process that easily with another function I have included below. As soon as the function returns, you can delete the temp file.

Otherwise, search example scripts for _FileListToArrayEX() which uses the method you want to use and just output the text before the array is created.

At least looking at that UDF will let you see how to manage it.

#Include<array.au3> ;; For _ArrayDisplay Purposes Only
Local $a_File = _StringReadToArray(@ScriptFullPath)

Func _StringReadToArray($s_String, $i_Blanklines = -1)
    If FileExists($s_String) Then $s_String = FileRead($s_String)
    If NOT StringRegExp($i_Blanklines, "(?i)default|1|-1") Then;; anything except these values will exclude blank lines.
        $i_Blanklines = 0
        $i_Blanklines = 1
    Local $a_Lines
    Local $s_SRE = "(?m:^)(.*\S.*)";; excludes blank lines
    If $i_Blanklines Then $s_SRE = "(?m:^)(.*)";; includes blank lines
    $a_Lines = StringRegExp($s_String, $s_SRE, 3)
    If NOT @Error Then Return $a_Lines
    Return SetError(@Error, @Extended)

EDIT: I forgot to add that the above function will also accept a simple string input so you really don't even need a temp file. The only drawback I see is if you don't want an array returned but that is easily overcome with _ArrayToString()

Edited by GEOSoft


Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver: - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...