Jump to content

trying to get path and file name


Recommended Posts

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

$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

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 by gcue
Link to comment
Share on other sites

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

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

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

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 by mikell
Link to comment
Share on other sites

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 by MrCreatoR

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: 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 Program

AutoIt_Icon_small.pngUDFs: 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
 
AutoIt_Icon_small.pngExamples: 
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 AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

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

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

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

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 :D

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

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_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: 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 Program

AutoIt_Icon_small.pngUDFs: 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
 
AutoIt_Icon_small.pngExamples: 
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 AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

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

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...