butterfly Posted December 4, 2018 Share Posted December 4, 2018 must be an easier way for getting the count and locations of occurrences in strings I know my code works but I just wanted to remove the last Directory folder from a string such as C:\user\xxx\bbb\aaa to C:\user\xxx\bbb\ expandcollapse popup#include <array.au3> $s_Dir = @WorkingDir $a_dir = StringSplit($s_Dir,"\") $1=$a_dir[0] Global $A_doubleArray[$a_dir[0]][3] For $i = 1 to $a_dir[0] ConsoleWrite("Counting1: " & $i & @LF) $A_doubleArray [($i - 1)][0] = StringLen($a_dir[$i]) $A_doubleArray [($i - 1)][1] = $a_dir[$i] $A_doubleArray[($i - 1)][2] = 0 If $i = 1 Then $A_doubleArray[($i - 1)][2] = $A_doubleArray [($i - 1)][0] Else $A_doubleArray [($i - 1)][2] = $A_doubleArray [($i - 1)][0] + $A_doubleArray [($i - 2)][2] + 1 ; (because of the removed '\' $trimnumber = $A_doubleArray [($i - 2)][2] EndIf _ArrayDisplay($A_doubleArray) If $i =$a_dir[0] Then $trimnumber = $A_doubleArray [($i - 1)][2] - $A_doubleArray [($i - 2)][2] ConsoleWrite("Counting2: " & $i & @LF) Next MsgBox(0,"$s_Dir",$s_Dir) $b_Dir = StringTrimRight($s_Dir,$trimnumber) MsgBox(0,$trimnumber,$b_Dir) _ArrayDisplay($A_doubleArray) Thank you for the insight! Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted December 4, 2018 Share Posted December 4, 2018 @butterfly Maybe something like this? Global $strString = "C:\user\xxx\bbb\aaa", _ ; This MUST end with no / $arrResult, _ $strNewString = "" ConsoleWrite("Before: $strString = " & $strString & @CRLF) $arrResult = StringSplit($strString, "\") For $i = 1 To $arrResult[0] - 1 Step 1 $strNewString &= $arrResult[$i] & "\" Next ConsoleWrite("After: $strNewString = " & $strNewString & @CRLF) 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...
water Posted December 4, 2018 Share Posted December 4, 2018 This works as long as there is no trailing backslash: #include <File.au3> Global $sFilePath = "C:\user\xxx\bbb\aaa", $sDrive, $sDir, $sFilename, $sExtension _PathSplit($sFilePath, $sDrive, $sDir, $sFilename, $sExtension) MsgBox(0, "Result", $sDrive & $sDir) FrancescoDiMuro 1 My UDFs and Tutorials: Spoiler UDFs: Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki Standard UDFs: Excel - Example Scripts - Wiki Word - Wiki Tutorials: ADO - Wiki WebDriver - Wiki Link to comment Share on other sites More sharing options...
Earthshine Posted December 4, 2018 Share Posted December 4, 2018 (edited) lol, i did this somewhere, looking.,.... argh, gonna have to do it again i guess, I did it in InstallScript too. Edited December 4, 2018 by Earthshine My resources are limited. You must ask the right questions Link to comment Share on other sites More sharing options...
Subz Posted December 4, 2018 Share Posted December 4, 2018 Another option: MsgBox(0,"File Path", StringLeft("C:\user\xxx\bbb\aaa", StringInStr("C:\user\xxx\bbb\aaa", "\", 0, -1) - 1)) Or in a function MsgBox(4096, "File Path", _Get_ParentDir("C:\user\xxx\bbb\aaa\")) ;~ $i_End = 0 - Exclude End Backslash ;~ $i_End = 1 - Include End Backslash Func _Get_ParentDir($s_Dir, $i_End = 0) If StringRight($s_Dir, 1) = "\" Then $s_Dir = StringTrimRight($s_Dir, 1) Return StringLeft($s_Dir, StringInStr($s_Dir, "\", 0, -1) - $i_End) EndFunc Link to comment Share on other sites More sharing options...
iamtheky Posted December 4, 2018 Share Posted December 4, 2018 ;~ $str = "C:\user\xxx\bbb\aaa" $str = "C:\user\xxx\bbb\aaa\" msgbox(0, '' , stringright($str , 1) = "\" ? stringtrimright($str , stringlen($str) - stringinstr($str , "\" , 0 , -2)) : stringtrimright($str , stringlen($str) - stringinstr($str , "\" , 0 , -1))) ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
mikell Posted December 4, 2018 Share Posted December 4, 2018 (edited) A regex way... $s1 = "C:\user\xxx\bbb\aaa" $s2 = "C:\user\xxx\bbb\aaa\" $pattern = '[^\\]+\\?$' Msgbox(0,"", StringRegExpReplace($s1, $pattern, "")) Msgbox(0,"", StringRegExpReplace($s2, $pattern, "")) means : replace "one or more non-backslash and a backslash if exists, at the end of the string" with nothing Edited December 4, 2018 by mikell comment added iamtheky, pixelsearch and FrancescoDiMuro 3 Link to comment Share on other sites More sharing options...
pixelsearch Posted December 4, 2018 Share Posted December 4, 2018 @mikell I like it when you explain us what means the (reg)expression you used. Even if it seems obvious to you (and it takes a bit of your time), it's very helpful to others. Thanks for that FrancescoDiMuro 1 Link to comment Share on other sites More sharing options...
mikell Posted December 4, 2018 Share Posted December 4, 2018 pixelsearch, You're absolutely right. While I have fun regexing I often omit to provide explanations and I apologize for that From now on Ill try to be more carefull Link to comment Share on other sites More sharing options...
FrancescoDiMuro Posted December 4, 2018 Share Posted December 4, 2018 (edited) @pixelsearch I'd create a dedicated thread where everyone can talk about RegEx... You know, there are a lot of RegEx experts/fans around here! And, thanks @mikell for your explanations... They are really well appreciated Edited December 4, 2018 by FrancescoDiMuro 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...
pixelsearch Posted December 4, 2018 Share Posted December 4, 2018 Thx mikell & Francesco 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