gcue Posted May 25, 2020 Share Posted May 25, 2020 hello i am trying to get path (c:\test\one drive) and file name (cars.exe) from any paths that have a prefix or a suffix like: $path = "cmd /k c:\test\one drive\cars.exe --tmas -asd" here is what i have tried so far $path_only = File_GetPath($path) $file_name_only = Get_FileName($path) Func File_GetPath($path) Return StringRegExpReplace($path, "\\[^\\]*$", "") EndFunc Func Get_FileName($path) Return StringRegExpReplace($path, "^.*\\", "") EndFunc ;==>Get_FileName thanks in advance! Link to comment Share on other sites More sharing options...
TheDcoder Posted May 25, 2020 Share Posted May 25, 2020 Where do you get $path from? There might be a better way than manually extracting the path than parsing the command. EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time) DcodingTheWeb Forum - Follow for updates and Join for discussion Link to comment Share on other sites More sharing options...
mikell Posted May 25, 2020 Share Posted May 25, 2020 $path contains a space, shouldn't it be enclosed with quotes ? Anyway : $path = "cmd /k c:\test\one drive\cars.exe --tmas -asd" Msgbox(0,"", File_GetPath($path) &@crlf& Get_FileName($path) ) Func File_GetPath($path) Return StringRegExpReplace($path, ".*(\S+:\\.+?)(?=\s-|$).*", "$1") EndFunc Func Get_FileName($path) Return StringRegExpReplace($path, "^.+\\(.+?)(?=\s-|$).*", "$1") EndFunc ;==>Get_FileName Link to comment Share on other sites More sharing options...
gcue Posted May 25, 2020 Author Share Posted May 25, 2020 (edited) 2 hours ago, mikell said: $path contains a space, shouldn't it be enclosed with quotes ? Anyway : $path = "cmd /k c:\test\one drive\cars.exe --tmas -asd" Msgbox(0,"", File_GetPath($path) &@crlf& Get_FileName($path) ) Func File_GetPath($path) Return StringRegExpReplace($path, ".*(\S+:\\.+?)(?=\s-|$).*", "$1") EndFunc Func Get_FileName($path) Return StringRegExpReplace($path, "^.+\\(.+?)(?=\s-|$).*", "$1") EndFunc ;==>Get_FileName that works great mikell! thank you however, for the File_GetPath - how do i exclude the exe - i just would like the path without the exe also what if the switch command / is used instead of - ? i am using this script to identify startup paths and files on remote computers. so the path might have either / or - thanks again for your help! Edited May 25, 2020 by gcue Link to comment Share on other sites More sharing options...
mikell Posted May 25, 2020 Share Posted May 25, 2020 Once you have got the path the rest is easy Local $a[3] = ["cmd /k c:\test\one drive\cars.exe --tmas -asd", _ "cmd /k c:\test\one drive\cars.exe /tmas -asd", _ "any /thing -else c:\test-1\one drive\my cars.exe --tmas /asd"] For $i = 0 to 2 $path = StringRegExpReplace($a[$i], '.*(\S+:\\.+?)(?=\s[/-]|$).*', "$1") $detail = StringRegExp($path, '^(.+)\\(.+)$', 3) Msgbox(0,$i+1, $path &@crlf& $detail[0] &@crlf& $detail[1]) Next Link to comment Share on other sites More sharing options...
UEZ Posted May 25, 2020 Share Posted May 25, 2020 12 minutes ago, mikell said: Once you have got the path the rest is easy Local $a[3] = ["cmd /k c:\test\one drive\cars.exe --tmas -asd", _ "cmd /k c:\test\one drive\cars.exe /tmas -asd", _ "any /thing -else c:\test-1\one drive\my cars.exe --tmas /asd"] For $i = 0 to 2 $path = StringRegExpReplace($a[$i], '.*(\S+:\\.+?)(?=\s[/-]|$).*', "$1") $detail = StringRegExp($path, '^(.+)\\(.+)$', 3) Msgbox(0,$i+1, $path &@crlf& $detail[0] &@crlf& $detail[1]) Next It will fail when the path is: "cmd /k c:\test\one --drive\cars.exe --tmas -asd" 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 comment Share on other sites More sharing options...
mikell Posted May 25, 2020 Share Posted May 25, 2020 1 minute ago, UEZ said: It will fail when the path is: "cmd /k c:\test\one --drive\cars.exe --tmas -asd" $path = StringRegExpReplace($a[$i], '.*(\S+:\\.+?)(?=\s[/-]+\w+(?:\s|$)|$).*', "$1") But I'm not sure that such a path would be valid if not surrounded with quotes ... Link to comment Share on other sites More sharing options...
UEZ Posted May 25, 2020 Share Posted May 25, 2020 Any space within the full path will probably not work if it is not in quotes. My primitive solution: $sCmd = 'cmd /k "c:\test\one --drive\cars.exe" --tmas -asd' $sPath = StringRegExp($sCmd, ".*([\w]\:.+?\.exe)", 3)[0] ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sPath = ' & $sPath & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console $sExe = StringRegExp($sPath, ".+\\(.+\.exe)", 3)[0] ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sExe = ' & $sExe & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console 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 comment Share on other sites More sharing options...
mikell Posted May 25, 2020 Share Posted May 25, 2020 (edited) Haha you're cheating a little ... it's obviously much easier if : - the path is in quotes, and/or - the file extension is always .exe Edit This uses the .exe extension, but using quotes it would be simpler $path = StringRegExpReplace($a[$i], '.*(\S+:\\.+?\.exe(?=\s|$)).*', "$1") Edited May 25, 2020 by mikell Link to comment Share on other sites More sharing options...
MrCreatoR Posted May 25, 2020 Share Posted May 25, 2020 (edited) To extract any string from other string, it is necessary to define the beginning and ending pattern, what are the allowed delimiters? I usually use this: $sString = 'cmd /k "c:\test\one --drive\cars.exe" --tmas -asd' $sRet = _StringExtract($sString, '[a-z]:\\.*?', '"') ConsoleWrite($sRet & @CRLF) $sString = 'cmd /k c:\test\one --drive\cars.exe --tmas -asd' $sRet = _StringExtract($sString, '[a-z]:\\.*?', '\h+--') ConsoleWrite($sRet & @CRLF) $sString = 'cmd /k c:\test\onedrive\cars.exe --tmas -asd' $sRet = _StringExtract($sString, '[a-z]:\\.*?', '("|\h+--|$)') ConsoleWrite($sRet & @CRLF) Func _StringExtract($sString, $sPttrn, $sStop_Pttrn) Local $aRet = StringRegExp($sString, '(' & $sPttrn & ')(?:' & $sStop_Pttrn & ')', 3) Return (@error ? '' : $aRet[0]) EndFunc Edited May 25, 2020 by MrCreatoR Spoiler Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1 AutoIt Russian Community My Work... Spoiler Projects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize ProgramUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF Examples: ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating ) * === My topics === * ================================================== ================================================== AutoIt is simple, subtle, elegant. © AutoIt Team Link to comment Share on other sites More sharing options...
gcue Posted May 25, 2020 Author Share Posted May 25, 2020 thank you MrCreator that is helpful when we strings are known ahead of time i am strill trying to get just the path without the exe though 😀 c:\test\one drive i apologize if i was not clear Link to comment Share on other sites More sharing options...
TheDcoder Posted May 25, 2020 Share Posted May 25, 2020 6 hours ago, gcue said: i am using this script to identify startup paths and files on remote computers Where are these startup paths defined? Do the commands with spaces in file names actually work? Right now there is no clear pattern. EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time) DcodingTheWeb Forum - Follow for updates and Join for discussion Link to comment Share on other sites More sharing options...
gcue Posted May 25, 2020 Author Share Posted May 25, 2020 on random computers at work - each of them have different software that might have startup entries. impossible to know them all. the path i provided is enough for me to go by thanks! Link to comment Share on other sites More sharing options...
TheDcoder Posted May 25, 2020 Share Posted May 25, 2020 1 minute ago, gcue said: on random computers at work That is not what I meant, I should have clarified better, the question should have been: From where in the computer are you getting the strings from EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time) DcodingTheWeb Forum - Follow for updates and Join for discussion Link to comment Share on other sites More sharing options...
gcue Posted May 25, 2020 Author Share Posted May 25, 2020 (edited) registry and profile startup folders Windows Automatic Startup Locations - gHacks Tech News Edited May 25, 2020 by gcue Link to comment Share on other sites More sharing options...
MrCreatoR Posted May 25, 2020 Share Posted May 25, 2020 2 hours ago, gcue said: trying to get just the path without the exe though So the end of the search is .exe? If you can define the string end, it will be the best solution. Spoiler Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1 AutoIt Russian Community My Work... Spoiler Projects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize ProgramUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF Examples: ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating ) * === My topics === * ================================================== ================================================== AutoIt is simple, subtle, elegant. © AutoIt Team Link to comment Share on other sites More sharing options...
gcue Posted May 25, 2020 Author Share Posted May 25, 2020 not always unfortunately Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted May 26, 2020 Share Posted May 26, 2020 @gcue What about _PathSplit()? Click here to see my signature: Spoiler ALWAYS GOOD TO READ: Forum Rules Forum Etiquette Link to comment Share on other sites More sharing options...
UEZ Posted May 26, 2020 Share Posted May 26, 2020 Cheat2: $sCmd = 'cmd /k "c:\test\one --drive\cars.jpg" --tmas -asd' $sPath = StringRegExp($sCmd, ".*([\w]\:.+\.\w+)", 3)[0] ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sPath = ' & $sPath & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console $sExe = StringRegExp($sPath, ".+\\(.+\..+)", 3)[0] ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sExe = ' & $sExe & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console $sPath2 = StringRegExp($sPath, "(.*)\\.+\..*", 3)[0] ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sPath2 = ' & $sPath2 & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console 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 comment Share on other sites More sharing options...
mikell Posted May 26, 2020 Share Posted May 26, 2020 45 minutes ago, UEZ said: Cheat2: This one is not a cheat, it's an entirely valid solution Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now