butterfly Posted December 4, 2018 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!
FrancescoDiMuro Posted December 4, 2018 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
water Posted December 4, 2018 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
Earthshine Posted December 4, 2018 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
Subz Posted December 4, 2018 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
iamtheky Posted December 4, 2018 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))) ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
mikell Posted December 4, 2018 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 FrancescoDiMuro, iamtheky and pixelsearch 3
pixelsearch Posted December 4, 2018 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 "I think you are searching a bug where there is no bug... don't listen to bad advice."
mikell Posted December 4, 2018 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
FrancescoDiMuro Posted December 4, 2018 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
pixelsearch Posted December 4, 2018 Posted December 4, 2018 Thx mikell & Francesco "I think you are searching a bug where there is no bug... don't listen to bad advice."
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