Skip to content

Can't get output of "dir" command on Windows #66

@Minecraft-vIIr

Description

@Minecraft-vIIr

Describe the bug
Can't get the output of dir command on Windows. Either a timeout error is raised or the output is different from the output as cmd.exe (without space warp between columns).

To Reproduce

import wexpect

def run_cmd():
    # Start cmd.exe process
    cmd = wexpect.spawn("cmd.exe")

    # Display the initial prompt
    cmd.expect(">")
    print(cmd.before + ">", end="")

    while True:
        # Read user input
        user_input = input()
        
        # Exit loop if user types "exit"
        if user_input.lower() == "exit":
            cmd.sendline("exit")
            break

        # Send the user input to cmd.exe
        cmd.sendline(user_input+" && (echo commandend1 && echo commandend2) || (echo commandend1 && echo commandend2)")

        # Wait for the command to complete and print its output
        cmd.expect(r"(commandend1 \r\ncommandend2\r\n\r\n(?:[^\\]+\\)*[^\\]*>)")

        # Remove the first line of command output (user input)
        print("\r\n".join(cmd.before.split("\r\n")[1:]))

        prompt = cmd.after.replace("commandend1 \r\ncommandend2\r\n\r\n", "")
        print(prompt, end="")

if __name__ == "__main__":
    run_cmd()

Expected behavior
Expected output (supposed to be exactly the same output as cmd.exe):

Microsoft Windows [Version 10.0.22631.3810]
(c) Microsoft Corporation. All rights reserved.

C:\Users\vIIr>dir
 Volume in drive C is Acer
 Volume Serial Number is C226-92AB

 Directory of C:\Users\vIIr

2024/06/30  下午 07:15    <DIR>          .
2023/10/07  下午 11:43    <DIR>          ..
2024/03/27  下午 08:43    <DIR>          .arduino-create
2024/03/28  下午 01:23    <DIR>          .arduinoIDE
...
2024/05/23  上午 10:33    <DIR>          Pictures
2023/12/03  上午 10:21    <DIR>          Saved Games
2023/10/07  下午 11:11    <DIR>          Searches
2023/10/08  下午 04:16    <DIR>          Tracing
2024/06/21  下午 07:00    <DIR>          Videos
              16 File(s)     34,059,527 bytes
              27 Dir(s)  22,039,977,984 bytes free

C:\Users\vIIr>

Environment:

  • [windows version] Windows 11 Pro 23H2
  • [Python version] 3.10.6
  • [wexpect version] 4.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions