Jump to content

Get file path from parameter string


Recommended Posts

There is a wonderful way by CreatoR:

Dim $aPath[10]

$aPath[0] = '"C:\Program Files\Unknown.dat\setup.exe" test.exe'
$aPath[1] = 'C:\Program Files\Unknown.dat\setup.exe test.exe'
$aPath[2] = 'C:\Program Files\Unknown.dat\setup.exe /d test.exe'
$aPath[3] = 'C:\Program Files\Unknown.dat\setup.exe test.exe /d'
$aPath[4] = 'C:\Program Files\Unknown.dat\setup.exe C:\#test.exe'
$aPath[5] = 'C:\Program Files\Unknown.dat\setup.exe #d C:\test.exe'
$aPath[6] = 'C:\Program Files\Unknown.dat\setup.exe test1.exe test2.exe'
$aPath[7] = 'C:\Program Files\Unknown.dat\setup.exe %1 -d -text.log'
$aPath[8] = 'explorer.exe C:\Program Files\test.txt'
$aPath[9] = 'explorer.exe %1 -a -b ...'

$sQuotes_Pattern = '(?:"?)+' ;Quotes (optional, can be contained in the plural - in a row).
$sPath_Pattern = '((?:.*\\)?.*?\..*?)' ;Extractable path (taking into account the point and the extension of the file name).
$sParams_Pattern = ' (.*)$' ;Parameters (space after the path, and all that after).

$sPattern = $sQuotes_Pattern & $sPath_Pattern & $sQuotes_Pattern & $sParams_Pattern
$sReplace = '\1' ;Replacement (used in commented out RegExpReplace), in this case we need only the first group, that is, the extracted path

For $i = 0 To UBound($aPath) - 1
    $aRet = StringRegExp($aPath[$i], $sPattern, 3)
   
    ConsoleWrite("[" & $aPath[$i] & "]" & @LF)
   
    For $j = 0 To UBound($aRet)-1
        ConsoleWrite(@TAB & $j & ": " & $aRet[$j] & @LF)
    Next
   
;~  ConsoleWrite(@TAB & "[RegExpReplace] " & StringRegExpReplace($aPath[$i], $sPattern, $sReplace) & @CRLF)
    ConsoleWrite(@LF)
Next

But it lacks one necessary option - if the line is C:\Program Files\Unknown.dat\setup.exe /s C:\test.exe /s
That is, in the parameters there is also a path to the file with parameters.

Please help make a correction in the pattern from CreatoR

Variants of string:

%SystemRoot%\System32\cmd.exe /C "%1" %*
%windir%\system32\mmc.exe /s %SystemRoot%\system32\devmgmt.msc /s
%SystemRoot%\System32\rundll32.exe "%ProgramFiles%\Windows Photo Viewer\PhotoViewer.dll", ImageView_Fullscreen %1
%SystemRoot%\system32\rundll32.exe %SystemRoot%\system32\shell32.dll,Control_RunDLL %SystemRoot%\system32\desk.cpl desk,@Appearance /Action:OpenMSTheme /file:"%1"
C:\Windows\System32\RUNDLL32.EXE "C:\Windows\System32\scrobj.dll",GenerateTypeLib "%1"

I would also like to get an array of paths from the parameter string in order to process it in the right order. Or include options for patterns to extract the first path or last.

Edited by musicstashall
Link to comment
Share on other sites

Sorry I won't be much help with RegExp, normally I use something basic like:

For $i = 0 To UBound($aPath) - 1
    ConsoleWrite("File Paths: " & StringStripWS(StringLeft(StringReplace($aPath[$i], '"', ""), StringInStr(StringReplace($aPath[$i], '"', ""), ".exe") + 4), 3) & @CRLF)
    ConsoleWrite("Parameters: " & StringStripWS(StringTrimLeft(StringReplace($aPath[$i], '"', ""), StringInStr(StringReplace($aPath[$i], '"', ""), ".exe") + 4), 3) & @CRLF)
Next

 

Link to comment
Share on other sites

There WinAPI UDF functions included to do this.  Use _WinAPI_PathGetArgs and/or _WinAPI_CommandLineToArgv.  You might also need _WinAPI_PathRemoveArgs for just the path itself.  Example below.  

#include <Array.au3>
#include <WinAPIShPath.au3>

Global $aPath[5]

$aPath[0] = '%SystemRoot%\System32\cmd.exe /C "%1" %*'
$aPath[1] = '%windir%\system32\mmc.exe /s %SystemRoot%\system32\devmgmt.msc /s'
$aPath[2] = '%SystemRoot%\System32\rundll32.exe "%ProgramFiles%\Windows Photo Viewer\PhotoViewer.dll", ImageView_Fullscreen %1'
$aPath[3] = '%SystemRoot%\system32\rundll32.exe %SystemRoot%\system32\shell32.dll,Control_RunDLL %SystemRoot%\system32\desk.cpl desk,@Appearance /Action:OpenMSTheme /file:"%1"'
$aPath[4] = 'C:\Windows\System32\RUNDLL32.EXE "C:\Windows\System32\scrobj.dll",GenerateTypeLib "%1"'

Global $aArgs = 0
For $i = 0 To UBound($aPath) - 1
    ConsoleWrite("Path only: " & _WinAPI_PathRemoveArgs($aPath[$i]) & "   Arguments: " & _WinAPI_PathGetArgs($aPath[$i]) & @CRLF)
    $aArgs = _WinAPI_CommandLineToArgv($aPath[$i])
    _ArrayDisplay($aArgs, "Path & Arguments Array")
Next

 

Adam

 

Edited by AdamUL
Link to comment
Share on other sites

Global $aPath[6]

$aPath[0] = 'C:\Program Files\Unknown.dat\setup.exe /s C:\test.exe /s'
$aPath[1] = 'C:\Windows\System32\RUNDLL32.EXE "C:\Windows\System32\scrobj.dll",GenerateTypeLib "%1"'
$aPath[2] = '%SystemRoot%\System32\cmd.exe /C "%1" %*'
$aPath[3] = '"%windir%\system32\mmc.exe" /s "%SystemRoot%\system32\devmgmt.msc" /s'
$aPath[4] = '%SystemRoot%\System32\rundll32.exe "%ProgramFiles%\Windows Photo Viewer\PhotoViewer.dll", ImageView_Fullscreen %1'
$aPath[5] = '"%SystemRoot%\system32\rundll32.exe" %SystemRoot%\system32\shell32.dll,Control_RunDLL %SystemRoot%\system32\desk.cpl desk,@Appearance /Action:OpenMSTheme /file:"%1"'

For $i = 0 To UBound($aPath) - 1
    $aArray = StringRegExp(_WinAPI_ExpandEnvironmentStrings($aPath[$i]), '([[:alpha:]]\:\\.*?\.\w+)(?:\W|$)', 3)
    $sStr = @LF & '-[' & $aPath[$i] & ']' & @LF
    For $j = 0 To UBound($aArray) - 1
        $sStr &= '+' & @TAB & $j & ': [' & $aArray[$j] & ']' & @LF
    Next
    ConsoleWrite($sStr)
Next

Another option suggested. Both options are good.

Edited by musicstashall
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...