Jump to content

Run @ComSpec Error: Not Getting StdOutRead


Go to solution Solved by noellarkin,

Recommended Posts

This is the AutoIt script I'm using:
 

#include <AutoItConstants.au3>

Local $Python = "C:\Program Files\Python38"

Local $Script = "levenstein.py"

Local $String1Path = @ScriptDir & "\string1.txt"
Local $String2Path = @ScriptDir & "\string2.txt"
Local $Arguments = '"' & $String1Path & '" "' & $String2Path & '"'

Local $Concat = $Script & " " & $Arguments
ConsoleWrite(@CRLF & $Concat)

Local $RunIt = Run(@ComSpec & " /c " & $Concat, @ScriptDir, @SW_HIDE, $STDERR_MERGED)
$Output = StdOutRead($RunIt)

ConsoleWrite(@CRLF & "Output:" & $Output)

It calls the following Python script (levenstein.py):
 

import re
import sys
import editdistance

def main():
  # Check if there are 2 command line arguments
  if len(sys.argv) != 3:
    print("Error: Two file paths must be provided")
    return

  # Read the contents of the files
  filepath1 = sys.argv[1]
  filepath2 = sys.argv[2]
  string1 = read_file_contents(filepath1)
  string2 = read_file_contents(filepath2)

  # Calculate and print the similarity between the two strings
  similarity = calculate_similarity(string1, string2)
  print(similarity)

def read_file_contents(filepath):
  # Read the contents of a file into a string
  with open(filepath, 'r') as f:
    return f.read()

def calculate_similarity(string1, string2):
  # Calculate the similarity between two strings
  return 100 - (100 * editdistance.eval(string1, string2) / max(len(string1), len(string2)))

# Run the main function
if __name__ == '__main__':
  main()

It's working when I run it from cmd, but not when I use AutoIt.

My AutoIt console looks like this:
 

levenstein.py "C:\test\Autoit Python\string1.txt" "C:\test\Autoit Python\string2.txt"
Output:

Whereas when I run the exact same command in cmd levenstein.py "C:\test\Autoit Python\string1.txt" "C:\test\Autoit Python\string2.txt" I get an output that's more like 77.08934 which is what I would expect from the .py script.
I believe I must be making a mistake in the way I'm using the Run command.

 

Note: I'm using Python for Edit Distance because the files I'm comparing are rather large, and I'm not sure if the code snippets I found in the Autoit forum would be effective.

Link to post
Share on other sites
  • Developers

I would start with changing the cmd param to this for testing to show&keep the window open in case of any errors:

Local $RunIt = Run(@ComSpec & " /k " & $Concat, @ScriptDir, @SW_SHOW, $STDERR_MERGED)

EDIT: And it could very well be you need to run this in x64 in stead of x86! :) 

Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to post
Share on other sites

Thanks, I tried out your suggestion - - the cmd window flashes for half a second and disappears, and the ConsoleOutput is the same as before. When you say I have to run it in x64, what would I need to do, exactly? I will be inegrating this script into others that have all been written in x32, so I'm wondering how that would work out...

Edited by noellarkin
clarity
Link to post
Share on other sites
  • Developers
8 minutes ago, noellarkin said:

the cmd window flashes for half a second and disappears,

/k should keep it open...? 

9 minutes ago, noellarkin said:

run it in x64, what would I need to do, exactly?

You need to run your script with AutoIt_x64.exe, which can be done with an directive when you have the Full SciTE4AutoIt3 installed:

#AutoIt3Wrapper_UseX64=y

 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to post
Share on other sites

/k doesn't seem to keep the window open, just flash it for a second.
I tried this:

#AutoIt3Wrapper_UseX64=y
#include <AutoItConstants.au3>


Local $Python = "C:\Program Files\Python38"

Local $Script = "levenstein.py"

Local $String1Path = @ScriptDir & "\string1.txt"
Local $String2Path = @ScriptDir & "\string2.txt"
Local $Arguments = '"' & $String1Path & '" "' & $String2Path & '"'

Local $Concat = $Script & " " & $Arguments
ConsoleWrite(@CRLF & $Concat)

Local $RunIt = Run(@ComSpec & " /k " & $Concat, @ScriptDir, @SW_SHOW, $STDERR_MERGED)
ConsoleWrite(@CRLF & "RunIt:" & $RunIt)
$Output = StdOutRead($RunIt)

ConsoleWrite(@CRLF & "Output:" & $Output)

same issues on output. I've made a test case and attached a zip file.

Test.zip

Link to post
Share on other sites

Also tried compiling the .py into an exe file and tried this:
 

#AutoIt3Wrapper_UseX64=y
#include <AutoItConstants.au3>


Local $Python = "C:\Program Files\Python38"

Local $Script = "levenstein.exe"

Local $String1Path = @ScriptDir & "\string1.txt"
Local $String2Path = @ScriptDir & "\string2.txt"
Local $Arguments = '"' & $String1Path & '" "' & $String2Path & '"'

Local $Concat = $Script & " " & $Arguments
ConsoleWrite(@CRLF & $Concat)

Local $RunIt = Run(@ComSpec & " /k " & $Concat, @ScriptDir, @SW_SHOW, $STDERR_MERGED)
ConsoleWrite(@CRLF & "RunIt:" & $RunIt)
$Output = StdOutRead($RunIt)

ConsoleWrite(@CRLF & "Output:" & $Output)

Working via cmd but not via ComSpec

levenstein.exe

Link to post
Share on other sites
  • Developers
10 minutes ago, noellarkin said:

Also tried compiling the .py into an exe file and tried this:

No need for that, so show me the whole SciTE ouputpane info when you run this.

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to post
Share on other sites

using levenstein.py

>"C:\Program Files (x86)\AutoIt3\SciTE\..\AutoIt3.exe" "C:\Program Files (x86)\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.au3" /run /prod /ErrorStdOut /in "C:\Test\AutoIt.au3" /UserParams    
+>20:17:00 Starting AutoIt3Wrapper (21.316.1639.1) from:SciTE.exe (4.4.6.0)  Keyboard:00000409  OS:WIN_7/Service Pack 1  CPU:X64 OS:X64  Environment(Language:0409)  CodePage:0  utf8.auto.check:4
+>         SciTEDir => C:\Program Files (x86)\AutoIt3\SciTE   UserDir => C:\Users\DESKTOP\AppData\Local\AutoIt v3\SciTE\AutoIt3Wrapper   SCITE_USERHOME => C:\Users\DESKTOP\AppData\Local\AutoIt v3\SciTE 
>Running AU3Check (3.3.14.5)  from:C:\Program Files (x86)\AutoIt3  input:C:\Test\AutoIt.au3
+>20:17:00 AU3Check ended.rc:0
>Running:(3.3.14.5):C:\Program Files (x86)\AutoIt3\autoit3_x64.exe "C:\Test\AutoIt.au3"    
+>Setting Hotkeys...--> Press Ctrl+Alt+Break to Restart or Ctrl+BREAK to Stop.

levenstein.py "C:\Test\string1.txt" "C:\Test\string2.txt"
RunIt:2956
Output:+>20:17:01 AutoIt3.exe ended.rc:0
+>20:17:01 AutoIt3Wrapper Finished.
>Exit code: 0    Time: 1.016


Using levenstein.exe

>"C:\Program Files (x86)\AutoIt3\SciTE\..\AutoIt3.exe" "C:\Program Files (x86)\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.au3" /run /prod /ErrorStdOut /in "C:\Test\AutoIt.au3" /UserParams    
+>20:18:36 Starting AutoIt3Wrapper (21.316.1639.1) from:SciTE.exe (4.4.6.0)  Keyboard:00000409  OS:WIN_7/Service Pack 1  CPU:X64 OS:X64  Environment(Language:0409)  CodePage:0  utf8.auto.check:4
+>         SciTEDir => C:\Program Files (x86)\AutoIt3\SciTE   UserDir => C:\Users\DESKTOP\AppData\Local\AutoIt v3\SciTE\AutoIt3Wrapper   SCITE_USERHOME => C:\Users\DESKTOP\AppData\Local\AutoIt v3\SciTE 
>Running AU3Check (3.3.14.5)  from:C:\Program Files (x86)\AutoIt3  input:C:\Test\AutoIt.au3
+>20:18:37 AU3Check ended.rc:0
>Running:(3.3.14.5):C:\Program Files (x86)\AutoIt3\autoit3_x64.exe "C:\Test\AutoIt.au3"    
+>Setting Hotkeys...--> Press Ctrl+Alt+Break to Restart or Ctrl+BREAK to Stop.

levenstein.exe "C:\Test\string1.txt" "C:\Test\string2.txt"
RunIt:3400
Output:+>20:18:37 AutoIt3.exe ended.rc:0
+>20:18:37 AutoIt3Wrapper Finished.
>Exit code: 0    Time: 1.248

 

Link to post
Share on other sites
  • Developers
27 minutes ago, noellarkin said:

"C:\Program Files (x86)\AutoIt3\SciTE\..\AutoIt3.exe"

You are not using the separate Full SciTE4Autoit3 version but the one that came with AutoIt, so the AutoIt3Wrapper directive doesn't do anything!
Either install that extra installer and benefit from it's extra's or use the internal available:

#pragma compile(x64, true)

... to run the script as x64.

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to post
Share on other sites
  • Solution

Thanks for the suggestions. I managed to fix the issue, here's what worked:

#include <AutoItConstants.au3>

Local $Python = "C:\Program Files\Python38"

Local $Script = "levenstein.exe"

Local $String1Path = @ScriptDir & "\string1.txt"
Local $String2Path = @ScriptDir & "\string2.txt"
Local $Arguments = '"' & $String1Path & '" "' & $String2Path & '"'

Local $Concat = $Script & " " & $Arguments
ConsoleWrite(@CRLF & $Concat)

Local $RunIt = Run(@ComSpec & " /c " & $Concat, @ScriptDir, @SW_HIDE, $STDERR_MERGED)
ProcessWaitClose($RunIt)
$Output = StdOutRead($RunIt)

ConsoleWrite(@CRLF & "Output:" & $Output)

It needed that extra line "ProcessWaitClose" :)

Edited by noellarkin
exe
Link to post
Share on other sites
  • Developers
6 minutes ago, noellarkin said:

It needed that extra line "ProcessWaitClose" :)

Yea .... that makes sense !  :) 

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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.

  • Similar Content

    • By Iraj
      Hi Team,
      Greetings!
       
      One silly doubt, I need to pass one parameter using RunWait to write an output to a file using Cmd Line, but the below isn't writing to the file specified. Kindly assist.
      $pass = $CmdLine[1] $CMD = "C:\Progra~1\bin\Encrypt.exe" &$pass& " > C:\temp\pwd.txt" #write the parameter $pass in C:\temp\pwd.txt RunWait(@ComSpec & " /c " & $CMD,"",@SW_HIDE) Thank you!!
    • By mLipok
      A new quick/small UDF.
      #include-once #include <AutoItConstants.au3> #include <MsgBoxConstants.au3> Global $RUN_WRAPPER_PID Global Enum _ $RUNWRAPPER_ERR_SUCCESS, _ $RUNWRAPPER_ERR_GENERAL, _ $RUNWRAPPER_ERR_COUNTER Global Enum _ $RUNWRAPPER_EXT_DEFAULT, _ $RUNWRAPPER_EXT_NOT_FINISHED_YET, _ $RUNWRAPPER_EXT_COUNTER If Not @Compiled And @ScriptName = 'Run_Wrapper.au3' Then _Example_for_Run_Wrapper() Func _Example_for_Run_Wrapper() _Run_Wrapper('ping 8.8.8.8') If @error then Return SetError(@error, @extended, 0) While $RUN_WRAPPER_PID Sleep(10) _Run_Wrapper_GetStdout() If @error Then _Run_Wrapper_GetStderr() If @error Then ExitLoop EndIf WEnd MsgBox($MB_OK + $MB_TOPMOST + $MB_ICONINFORMATION, 'Information #' & @ScriptLineNumber, _ _Run_Wrapper_GetStdout() & @CRLF & _ _Run_Wrapper_GetStderr() _ ) EndFunc ;==>_Example_for_Run_Wrapper Func _Run_Wrapper($sCommand) _Run_Wrapper_GetStdout(Null) _Run_Wrapper_GetStderr(Null) $RUN_WRAPPER_PID = Run(@ComSpec & " /c " & $sCommand, @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD) If @error Then Return SetError($RUNWRAPPER_ERR_GENERAL, $RUNWRAPPER_EXT_DEFAULT, 0) Return $RUN_WRAPPER_PID EndFunc ;==>_Run_Wrapper Func _Run_Wrapper_GetStdout($v_Reset = Default) Local Static $s_StdOut = "" If IsKeyword($v_Reset) And $v_Reset = Null Then $s_StdOut = '' $s_StdOut &= StdoutRead($RUN_WRAPPER_PID) If @error Then Return SetError(@error, $RUNWRAPPER_EXT_DEFAULT, $s_StdOut) Return SetExtended($RUNWRAPPER_EXT_NOT_FINISHED_YET, $s_StdOut) EndFunc ;==>_Run_Wrapper_GetStdout Func _Run_Wrapper_GetStderr($v_Reset = Default) Local Static $s_StdErr = '' If IsKeyword($v_Reset) And $v_Reset = Null Then $s_StdErr = '' $s_StdErr &= StderrRead($RUN_WRAPPER_PID) If @error Then Return SetError(@error, $RUNWRAPPER_EXT_DEFAULT, $s_StdErr) Return SetExtended($RUNWRAPPER_EXT_NOT_FINISHED_YET, $s_StdErr) EndFunc ;==>_Run_Wrapper_GetStderr  
       
    • By WoodGrain
      Hi All,
      Trying to open windows explorer to a WebDav location and it's not working quite how I want, on the computers it is setup as a "network location" (as opposed to a "mapped drive", and this unfortunately can't be changed), the "Data" WebDav folder sits directly under "This PC" if that's an easier way to get to it. any suggestions as to what I can correct to get the 2nd example to work?
      ; This works, but I'm trying to avoid this as users normally see the URL style in the 2nd example below $folderToOpen = "\\mycompany.sharepoint.com@SSL\DavWWWRoot\Data" Run("Explorer.exe " & $folderToOpen) ; This does not work, it tries to open the WebDav url in the default web browser $folderToOpen = "https://mycompany.sharepoint.com/Data" Run("Explorer.exe " & $folderToOpen) ShellExecute also opens it in the default browser.
      Saw _WinAPI_ShellOpenFolderAndSelectItems but couldn't get the 2nd example to work.
      If I manually open Windows Explorer and paste in https://mycompany.sharepoint.com/Data it loads the WebDav directory without issue.
      If I have to use the pathing from the first example it is fine, just trying to give users a familiar experience.
      Thanks!
    • By nacerbaaziz
      hello autoit team
      is there any wey to check if any process run as admin or no?
      i mean e.g if i want to restart any process, now i have the ability to get the process path and commands line
      what i need is a wey to check if the process was runing as admin or no to restart it with the same state.
      here is the part that am using it to restart the process
      func _processRestart($i_pid, $s_ProcessPath) if not (ProcessExists($i_ProcessPid)) then return SetError(1, 0, -1) local $s_ProcessWorkDir = _WinAPI_GetProcessWorkingDirectory($i_ProcessPid) ProcessClose($i_ProcessPid) ProcessWaitClose($i_ProcessPid) ProcessWait(ShellExecute($i_pid,"", $s_ProcessWorkDir)) ProcessesGetList() return true endFunc thanks in advance
    • By GillesMaisonneuve
      Good morning,
       
      I am trying to read a Unicode utf8 string from a perl subprocess via StdoutRead.
      I use an AUtoIt GUI and display result in an 'Edit' control (see my code below) using 'Courier New', a font that can handle Unicode characters.
      I was expecting a result looking like (CMD console):
      ++$ chcp 65001>NUL: & perl -Mutf8 -CS -e "use 5.018; binmode STDOUT,q(:utf8); say qq(\x{03A9})" & chcp 850>NUL: Ω Instead I get someting like this (see downward the screen copy):
      ++$ chcp 1250>NUL: & perl -Mutf8 -CS -e "use 5.018; binmode STDOUT,q(:utf8); say qq(\x{03A9})" & chcp 850>NUL: Ω Obviously while I was expecting to receive an utf8 char, it seems to have been converted to Windows ANSI codepage 1250 (Windows default for Western/Central Europe, right ?)
      What am I doing wrong? Is there someone who could guide me?
       
      Here is my code and my output in the GUI.
      Creating and configuring the Edit control:
      Local $Edit1 = GUICtrlCreateEdit( "", 20, 110, 780, 500, BitOr($GUI_SS_DEFAULT_EDIT,$ES_MULTILINE,$ES_READONLY) ) GUICtrlSetData($Edit1, "This field will contain text result from external Perl command") GUICtrlSetFont($Edit1, 10, $FW_THIN, $GUI_FONTNORMAL, "Courier New")  
      Executing Perl command (note: `-Mutf8` and `-CS` garantees that I work in utf8 and STDOUT accepts wide-characters):
      local $ExePath = 'perl.exe -Mutf8 -CS ' ;~ if perl in PATH, no need for full path C:\Perl\bin\perl.exe local $Params = '-e "use 5.018; use utf8; use charnames q(:full); binmode STDOUT,q(:utf8);' & _ 'say scalar localtime; say qq(\N{GREEK CAPITAL LETTER OMEGA})"' local $Cmd = $ExePath & ' ' & $Params Local $iPID = Run($Cmd, "", @SW_HIDE, BitOR($STDERR_CHILD, $STDOUT_CHILD))  
      Reading STDOUT and displaying it into the Edit control:
      While 1 $sOutput &= StdoutRead($iPID) If @error Then ; Exit the loop if the process closes or StdoutRead returns an error. ExitLoop EndIf WEnd If $sOutput <> '' Then GUICtrlSetData($Edit1, $sOutput) EndIf  
      And now, what I get on my GUI:
       

×
×
  • Create New...