Jump to content

complex command line with run or shellexecute


rilian
 Share

Go to solution Solved by DW1,

Recommended Posts

I've searched many examples on these forums and can't figure out why my command won't run from AutoIT. It runs perfectly fine on a regular command line. 

The command I am running is from the sound utility SoX. It works as below in a command prompt:

"C:Program Files (x86)sox-14-4-1sox "c:path withspacesto filefilename" -c 1 -b 16 -e signed-integer "c:path withspacesto filefilename_converted.wav" trim 0 175

That is the correct SoX command for what I am trying to do. It requires an input filename, an output filename and parameters around both. 

I have tried and tried to get either run or shellexecute to run the above command after parsing a drag and drop file name(s). I have no trouble parsing the file names and can get them into variables but for the life of me can't get the command to run. If it works, a sound file is created in the same folder as the source and has _converted added to the end of it. 

Below is my code

;This segment checks 64-bit vs 32-bit and sets the program files path appropriately. It matters for FF and Chrome.
if @OSArch="X86" Then
    $progdir = "c:\Program Files"
Else
    $progdir = "c:\Program Files (x86)"
EndIf
$sox='"' & $progdir & "\sox-14-4-1\sox.exe" & '"'
$soxoptions = " -c 1 -b 16 -e signed integer "

;if $cmdline[0] <> 0 then msgbox(4000, "Drag n Drop Test", "Dropped file is: " & $cmdline[1])
$msgstr=""
if $cmdline[0] > 0 then
   $numfiles=$cmdline[0]
    for $i=1 to $numfiles
      $infile=$cmdline[$i]
      $outfilepath1=stringsplit($infile,".")    ;returns array. [0] has num strings (2 in this case). [1] has the path [2] has the format
      $outfilepath=$outfilepath1[1]             ;get path from splitstring run.
      $outfile=$outfilepath & "_converted.wav"  ;add _converted.wav to outfile name. 
      
      ;$myruncmd='"' & $sox & '" "' & $infile & '"' & $soxoptions & '"' & $outfile & '"' & " trim 0 175"
      
      $soxparameters='"' & $infile & '"' & $soxoptions & '"' & $outfile & '"' & " trim 0 175 "
      msgbox(4000,"run command test",$sox & @crlf & $soxparameters)
      ShellExecute($sox, $soxparameters,$outfilepath,"",@SW_SHOW)
      ;run(@ComSpec & ' /c $myruncmd')
      ;$msgstr=$msgstr & $infile & @TAB & $outfile & @CRLF
      ;$msgstr=$msgstr & $cmdline[$i] & @CRLF
    Next
   ;msgbox(4000,"Drag n Drop Test", "File List: " & @CRLF & $msgstr)
EndIf

The output of the msgbox right before shellexecute is "c:program files (x86)sox-14-4-1sox.exe" "C:UsersusernameDesktopsoxinfile.wav" -c 1 -b 16 -e signed integer "C:UsersusernameDesktopsoxinfile_converted.wav" trim 0 175

Quotes displayed in the above are actually in the string. I added them in because of the potential for spaces in the file-names and folder paths.  

Can someone please tell me how to structure a run command or a shellexecute that will run this complex command I need to use?

Thank you

Link to comment
Share on other sites

It does say "signed-integer" on the website. So seems like a good pickup, danwilli. :)

rilian,

Not sure if I like the StringSplit. It may give you problems if more then one dot exists in the path from the parameter.

Here is some alternative code that I created using regex to get the paths.

;This segment checks 64-bit vs 32-bit and sets the program files path appropriately. It matters for FF and Chrome.
if @OSArch = "X86" Then
    $progdir = EnvGet('ProgramFiles')
Else
    $progdir = EnvGet('ProgramFiles(x86)')
EndIf
$sox = $progdir & '\sox-14-4-1\sox.exe'
$soxoptions = '-c 1 -b 16 -e signed-integer'
If Not FileExists($sox) Then Exit 1

If $CMDLINE[0] Then
    For $i = 1 To $CMDLINE[0]
        $aPath = _GetPaths($CMDLINE[$i]); returns an array. [0] = path\filename, [1] = extension, [2] = workingdir
        If @error Then ContinueLoop
        $soxparameters = '"' & $CMDLINE[$i] & '" ' & $soxoptions & ' "' & $aPath[0] & '_converted.' & $aPath[1] & '" trim 0 175'
        MsgBox(0, 'ShellExecute - ' & $CMDLINE[$i], 'ShellExecute("' & $sox & '", ' & $soxparameters & ', ' & $aPath[2] & ')' )
        ;ShellExecute('"' & $sox & '"', $soxparameters, $aPath[2], "", @SW_SHOW)
        MsgBox(0, 'Run - ' & $CMDLINE[$i], 'Run("' & @ComSpec & '" /c "' & $sox & '", ' & $soxparameters & ', ' & $aPath[2] & ')' )
        ;Run('"' & @ComSpec & '" /c "' & $sox & '" ' & $soxparameters, $aPath[2], @SW_SHOW)
    Next
EndIf

Func _GetPaths($sPath)
    Local $aPath
    $aPath = StringRegExp($sPath, _
        '(.*[\/]|.*)' & _ ; get up to last slash including file name
        '\.(.*?)$' & _ ; get extension
        '|' & _ ; or
        '(.*)' & _ ; get filename
        '\.(.*?)$', _ ; get extension
        3 _ ; flag
    )
    If UBound($aPath) = 2 Then
        ReDim $aPath[3]
        ; get workingdir
        $aPath[2] = StringRegExpReplace($aPath[0], '(.*\\).*', '\1')
        ; set workingdir to current directory if no slash found
        If Not @extended Then $aPath[2] = '.\'
        Return $aPath
    EndIf
    Return SetError(1, 0, '')
EndFunc

I do not have or use sox to test it. You are welcome to use it if it suits your needs. :)

Link to comment
Share on other sites

@Danwili: Thanks for the catch on that parameter. It does work now w/ ShellExecute. Hard to believe I've been beating my head into a wall over a missed hyphen. 

@MHz: Thanks for the Regex code. You're right that my stringsplit won't handle all cases. My cases are going to be simple so I'm not terribly worried but in the long run, you're right, it should be fixed. Thanks for the code. 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...